blob: 879d36e6fddc432ee1d16c514e4003a9917ea58c [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
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000102// Minimum timestamp separation between subsequent input events from a Bluetooth device.
103static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
104// Maximum smoothing time delta so that we don't generate events too far into the future.
105constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
106
Michael Wrightd02c5b62014-02-10 15:10:22 -0800107template<typename T>
108static inline T min(T a, T b) {
109 return a < b ? a : b;
110}
111
112static inline float avg(float x, float y) {
113 return (x + y) / 2;
114}
115
Chris Ye3fdbfef2021-01-06 18:45:18 -0800116// Mapping for light color name and the light color
117const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
118 {"green", LightColor::GREEN},
119 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800120
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700121static int32_t getInverseRotation(int32_t orientation) {
122 switch (orientation) {
123 case DISPLAY_ORIENTATION_90:
124 return DISPLAY_ORIENTATION_270;
125 case DISPLAY_ORIENTATION_270:
126 return DISPLAY_ORIENTATION_90;
127 default:
128 return orientation;
129 }
130}
131
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800132static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
133 InputDeviceInfo info;
134 mapper.populateDeviceInfo(&info);
135
136 const InputDeviceInfo::MotionRange* motionRange =
137 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
138 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
139}
140
141static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
142 InputDeviceInfo info;
143 mapper.populateDeviceInfo(&info);
144
145 const InputDeviceInfo::MotionRange* motionRange =
146 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
147 ASSERT_EQ(nullptr, motionRange);
148}
149
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700150[[maybe_unused]] static void dumpReader(InputReader& reader) {
151 std::string dump;
152 reader.dump(dump);
153 std::istringstream iss(dump);
154 for (std::string line; std::getline(iss, line);) {
155 ALOGE("%s", line.c_str());
156 std::this_thread::sleep_for(std::chrono::milliseconds(1));
157 }
158}
159
Michael Wrightd02c5b62014-02-10 15:10:22 -0800160// --- FakePointerController ---
161
162class FakePointerController : public PointerControllerInterface {
163 bool mHaveBounds;
164 float mMinX, mMinY, mMaxX, mMaxY;
165 float mX, mY;
166 int32_t mButtonState;
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800167 int32_t mDisplayId;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169public:
170 FakePointerController() :
171 mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0),
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800172 mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173 }
174
Michael Wright17db18e2020-06-26 20:51:44 +0100175 virtual ~FakePointerController() {}
176
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177 void setBounds(float minX, float minY, float maxX, float maxY) {
178 mHaveBounds = true;
179 mMinX = minX;
180 mMinY = minY;
181 mMaxX = maxX;
182 mMaxY = maxY;
183 }
184
Chris Yea52ade12020-08-27 16:49:20 -0700185 void setPosition(float x, float y) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 mX = x;
187 mY = y;
188 }
189
Chris Yea52ade12020-08-27 16:49:20 -0700190 void setButtonState(int32_t buttonState) override { mButtonState = buttonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191
Chris Yea52ade12020-08-27 16:49:20 -0700192 int32_t getButtonState() const override { return mButtonState; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800193
Chris Yea52ade12020-08-27 16:49:20 -0700194 void getPosition(float* outX, float* outY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800195 *outX = mX;
196 *outY = mY;
197 }
198
Chris Yea52ade12020-08-27 16:49:20 -0700199 int32_t getDisplayId() const override { return mDisplayId; }
Arthur Hungc7ad2d02018-12-18 17:41:29 +0800200
Chris Yea52ade12020-08-27 16:49:20 -0700201 void setDisplayViewport(const DisplayViewport& viewport) override {
Garfield Tan888a6a42020-01-09 11:39:16 -0800202 mDisplayId = viewport.displayId;
203 }
204
Arthur Hung7c645402019-01-25 17:45:42 +0800205 const std::map<int32_t, std::vector<int32_t>>& getSpots() {
206 return mSpotsByDisplay;
207 }
208
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209private:
Chris Yea52ade12020-08-27 16:49:20 -0700210 bool getBounds(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800211 *outMinX = mMinX;
212 *outMinY = mMinY;
213 *outMaxX = mMaxX;
214 *outMaxY = mMaxY;
215 return mHaveBounds;
216 }
217
Chris Yea52ade12020-08-27 16:49:20 -0700218 void move(float deltaX, float deltaY) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 mX += deltaX;
220 if (mX < mMinX) mX = mMinX;
221 if (mX > mMaxX) mX = mMaxX;
222 mY += deltaY;
223 if (mY < mMinY) mY = mMinY;
224 if (mY > mMaxY) mY = mMaxY;
225 }
226
Chris Yea52ade12020-08-27 16:49:20 -0700227 void fade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228
Chris Yea52ade12020-08-27 16:49:20 -0700229 void unfade(Transition) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230
Chris Yea52ade12020-08-27 16:49:20 -0700231 void setPresentation(Presentation) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232
Chris Yea52ade12020-08-27 16:49:20 -0700233 void setSpots(const PointerCoords*, const uint32_t*, BitSet32 spotIdBits,
234 int32_t displayId) override {
Arthur Hung7c645402019-01-25 17:45:42 +0800235 std::vector<int32_t> newSpots;
236 // Add spots for fingers that are down.
237 for (BitSet32 idBits(spotIdBits); !idBits.isEmpty(); ) {
238 uint32_t id = idBits.clearFirstMarkedBit();
239 newSpots.push_back(id);
240 }
241
242 mSpotsByDisplay[displayId] = newSpots;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800243 }
244
Prabir Pradhan197e0862022-07-01 14:28:00 +0000245 void clearSpots() override { mSpotsByDisplay.clear(); }
Arthur Hung7c645402019-01-25 17:45:42 +0800246
247 std::map<int32_t, std::vector<int32_t>> mSpotsByDisplay;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248};
249
250
251// --- FakeInputReaderPolicy ---
252
253class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700254 std::mutex mLock;
255 std::condition_variable mDevicesChangedCondition;
256
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000258 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700259 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
260 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100261 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700262 TouchAffineTransformation transform;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000263 std::optional<int32_t /*deviceId*/> mStylusGestureNotified GUARDED_BY(mLock){};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264
265protected:
Chris Yea52ade12020-08-27 16:49:20 -0700266 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800267
268public:
269 FakeInputReaderPolicy() {
270 }
271
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700272 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800273 waitForInputDevices([](bool devicesChanged) {
274 if (!devicesChanged) {
275 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
276 }
277 });
278 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700279
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800280 void assertInputDevicesNotChanged() {
281 waitForInputDevices([](bool devicesChanged) {
282 if (devicesChanged) {
283 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
284 }
285 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700286 }
287
Prabir Pradhanda20b172022-09-26 17:01:18 +0000288 void assertStylusGestureNotified(int32_t deviceId) {
289 std::scoped_lock lock(mLock);
290 ASSERT_TRUE(mStylusGestureNotified);
291 ASSERT_EQ(deviceId, *mStylusGestureNotified);
292 mStylusGestureNotified.reset();
293 }
294
295 void assertStylusGestureNotNotified() {
296 std::scoped_lock lock(mLock);
297 ASSERT_FALSE(mStylusGestureNotified);
298 }
299
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700300 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100301 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100302 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700303 }
304
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700305 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
306 return mConfig.getDisplayViewportByUniqueId(uniqueId);
307 }
308 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
309 return mConfig.getDisplayViewportByType(type);
310 }
311
312 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
313 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700314 }
315
Prabir Pradhan5632d622021-09-06 07:57:20 -0700316 void addDisplayViewport(DisplayViewport viewport) {
317 mViewports.push_back(std::move(viewport));
318 mConfig.setDisplayViewports(mViewports);
319 }
320
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700321 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000322 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700323 std::optional<uint8_t> physicalPort, ViewportType type) {
324 const bool isRotated =
325 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
326 DisplayViewport v;
327 v.displayId = displayId;
328 v.orientation = orientation;
329 v.logicalLeft = 0;
330 v.logicalTop = 0;
331 v.logicalRight = isRotated ? height : width;
332 v.logicalBottom = isRotated ? width : height;
333 v.physicalLeft = 0;
334 v.physicalTop = 0;
335 v.physicalRight = isRotated ? height : width;
336 v.physicalBottom = isRotated ? width : height;
337 v.deviceWidth = isRotated ? height : width;
338 v.deviceHeight = isRotated ? width : height;
339 v.isActive = isActive;
340 v.uniqueId = uniqueId;
341 v.physicalPort = physicalPort;
342 v.type = type;
343
344 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 }
346
Arthur Hung6cd19a42019-08-30 19:04:12 +0800347 bool updateViewport(const DisplayViewport& viewport) {
348 size_t count = mViewports.size();
349 for (size_t i = 0; i < count; i++) {
350 const DisplayViewport& currentViewport = mViewports[i];
351 if (currentViewport.displayId == viewport.displayId) {
352 mViewports[i] = viewport;
353 mConfig.setDisplayViewports(mViewports);
354 return true;
355 }
356 }
357 // no viewport found.
358 return false;
359 }
360
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100361 void addExcludedDeviceName(const std::string& deviceName) {
362 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800363 }
364
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700365 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
366 mConfig.portAssociations.insert({inputPort, displayPort});
367 }
368
Christine Franks1ba71cc2021-04-07 14:37:42 -0700369 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
370 const std::string& displayUniqueId) {
371 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
372 }
373
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000374 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700375
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000376 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700377
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000378 void setPointerController(std::shared_ptr<FakePointerController> controller) {
379 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380 }
381
382 const InputReaderConfiguration* getReaderConfiguration() const {
383 return &mConfig;
384 }
385
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800386 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387 return mInputDevices;
388 }
389
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100390 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700391 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700392 return transform;
393 }
394
395 void setTouchAffineTransformation(const TouchAffineTransformation t) {
396 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800397 }
398
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000399 PointerCaptureRequest setPointerCapture(bool enabled) {
400 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
401 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800402 }
403
Arthur Hung7c645402019-01-25 17:45:42 +0800404 void setShowTouches(bool enabled) {
405 mConfig.showTouches = enabled;
406 }
407
Garfield Tan888a6a42020-01-09 11:39:16 -0800408 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
409 mConfig.defaultPointerDisplayId = pointerDisplayId;
410 }
411
HQ Liue6983c72022-04-19 22:14:56 +0000412 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
413
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800414 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
415
HQ Liue6983c72022-04-19 22:14:56 +0000416 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
417
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000418 void setVelocityControlParams(const VelocityControlParameters& params) {
419 mConfig.pointerVelocityControlParameters = params;
420 mConfig.wheelVelocityControlParameters = params;
421 }
422
Michael Wrightd02c5b62014-02-10 15:10:22 -0800423private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000424 uint32_t mNextPointerCaptureSequenceNumber = 0;
425
Chris Yea52ade12020-08-27 16:49:20 -0700426 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800427 *outConfig = mConfig;
428 }
429
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000430 std::shared_ptr<PointerControllerInterface> obtainPointerController(
431 int32_t /*deviceId*/) override {
432 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800433 }
434
Chris Yea52ade12020-08-27 16:49:20 -0700435 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700436 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800437 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700438 mInputDevicesChanged = true;
439 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800440 }
441
Chris Yea52ade12020-08-27 16:49:20 -0700442 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
443 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700444 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 }
446
Chris Yea52ade12020-08-27 16:49:20 -0700447 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800448
449 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
450 std::unique_lock<std::mutex> lock(mLock);
451 base::ScopedLockAssertion assumeLocked(mLock);
452
453 const bool devicesChanged =
454 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
455 return mInputDevicesChanged;
456 });
457 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
458 mInputDevicesChanged = false;
459 }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000460
461 void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) override {
462 std::scoped_lock<std::mutex> lock(mLock);
463 mStylusGestureNotified = deviceId;
464 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465};
466
Michael Wrightd02c5b62014-02-10 15:10:22 -0800467// --- FakeEventHub ---
468
469class FakeEventHub : public EventHubInterface {
470 struct KeyInfo {
471 int32_t keyCode;
472 uint32_t flags;
473 };
474
Chris Yef59a2f42020-10-16 12:55:26 -0700475 struct SensorInfo {
476 InputDeviceSensorType sensorType;
477 int32_t sensorDataIndex;
478 };
479
Michael Wrightd02c5b62014-02-10 15:10:22 -0800480 struct Device {
481 InputDeviceIdentifier identifier;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700482 ftl::Flags<InputDeviceClass> classes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800483 PropertyMap configuration;
484 KeyedVector<int, RawAbsoluteAxisInfo> absoluteAxes;
485 KeyedVector<int, bool> relativeAxes;
486 KeyedVector<int32_t, int32_t> keyCodeStates;
487 KeyedVector<int32_t, int32_t> scanCodeStates;
488 KeyedVector<int32_t, int32_t> switchStates;
489 KeyedVector<int32_t, int32_t> absoluteAxisValue;
490 KeyedVector<int32_t, KeyInfo> keysByScanCode;
491 KeyedVector<int32_t, KeyInfo> keysByUsageCode;
492 KeyedVector<int32_t, bool> leds;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100493 // fake mapping which would normally come from keyCharacterMap
494 std::unordered_map<int32_t, int32_t> keyCodeMapping;
Chris Yef59a2f42020-10-16 12:55:26 -0700495 std::unordered_map<int32_t, SensorInfo> sensorsByAbsCode;
496 BitArray<MSC_MAX> mscBitmask;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800497 std::vector<VirtualKeyDefinition> virtualKeys;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700498 bool enabled;
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000499 InputDeviceCountryCode countryCode;
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700500
501 status_t enable() {
502 enabled = true;
503 return OK;
504 }
505
506 status_t disable() {
507 enabled = false;
508 return OK;
509 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700511 explicit Device(ftl::Flags<InputDeviceClass> classes) : classes(classes), enabled(true) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800512 };
513
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700514 std::mutex mLock;
515 std::condition_variable mEventsCondition;
516
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517 KeyedVector<int32_t, Device*> mDevices;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100518 std::vector<std::string> mExcludedDevices;
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000519 std::vector<RawEvent> mEvents GUARDED_BY(mLock);
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600520 std::unordered_map<int32_t /*deviceId*/, std::vector<TouchVideoFrame>> mVideoFrames;
Chris Ye87143712020-11-10 05:05:58 +0000521 std::vector<int32_t> mVibrators = {0, 1};
Chris Ye3fdbfef2021-01-06 18:45:18 -0800522 std::unordered_map<int32_t, RawLightInfo> mRawLightInfos;
523 // Simulates a device light brightness, from light id to light brightness.
524 std::unordered_map<int32_t /* lightId */, int32_t /* brightness*/> mLightBrightness;
525 // Simulates a device light intensities, from light id to light intensities map.
526 std::unordered_map<int32_t /* lightId */, std::unordered_map<LightColor, int32_t>>
527 mLightIntensities;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700529public:
Michael Wrightd02c5b62014-02-10 15:10:22 -0800530 virtual ~FakeEventHub() {
531 for (size_t i = 0; i < mDevices.size(); i++) {
532 delete mDevices.valueAt(i);
533 }
534 }
535
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 FakeEventHub() { }
537
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000538 void addDevice(int32_t deviceId, const std::string& name, ftl::Flags<InputDeviceClass> classes,
539 int bus = 0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800540 Device* device = new Device(classes);
541 device->identifier.name = name;
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000542 device->identifier.bus = bus;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543 mDevices.add(deviceId, device);
544
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000545 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_ADDED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800546 }
547
548 void removeDevice(int32_t deviceId) {
549 delete mDevices.valueFor(deviceId);
550 mDevices.removeItem(deviceId);
551
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000552 enqueueEvent(ARBITRARY_TIME, READ_TIME, deviceId, EventHubInterface::DEVICE_REMOVED, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800553 }
554
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000555 bool isDeviceEnabled(int32_t deviceId) const override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700556 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700557 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700558 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
559 return false;
560 }
561 return device->enabled;
562 }
563
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000564 status_t enableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700565 status_t result;
566 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700567 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700568 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
569 return BAD_VALUE;
570 }
571 if (device->enabled) {
572 ALOGW("Duplicate call to %s, device %" PRId32 " already enabled", __func__, deviceId);
573 return OK;
574 }
575 result = device->enable();
576 return result;
577 }
578
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000579 status_t disableDevice(int32_t deviceId) override {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700580 Device* device = getDevice(deviceId);
Yi Kong9b14ac62018-07-17 13:48:38 -0700581 if (device == nullptr) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700582 ALOGE("Incorrect device id=%" PRId32 " provided to %s", deviceId, __func__);
583 return BAD_VALUE;
584 }
585 if (!device->enabled) {
586 ALOGW("Duplicate call to %s, device %" PRId32 " already disabled", __func__, deviceId);
587 return OK;
588 }
589 return device->disable();
590 }
591
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 void finishDeviceScan() {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000593 enqueueEvent(ARBITRARY_TIME, READ_TIME, 0, EventHubInterface::FINISHED_DEVICE_SCAN, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 }
595
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -0700596 void addConfigurationProperty(int32_t deviceId, const char* key, const char* value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597 Device* device = getDevice(deviceId);
598 device->configuration.addProperty(key, value);
599 }
600
601 void addConfigurationMap(int32_t deviceId, const PropertyMap* configuration) {
602 Device* device = getDevice(deviceId);
603 device->configuration.addAll(configuration);
604 }
605
606 void addAbsoluteAxis(int32_t deviceId, int axis,
607 int32_t minValue, int32_t maxValue, int flat, int fuzz, int resolution = 0) {
608 Device* device = getDevice(deviceId);
609
610 RawAbsoluteAxisInfo info;
611 info.valid = true;
612 info.minValue = minValue;
613 info.maxValue = maxValue;
614 info.flat = flat;
615 info.fuzz = fuzz;
616 info.resolution = resolution;
617 device->absoluteAxes.add(axis, info);
618 }
619
620 void addRelativeAxis(int32_t deviceId, int32_t axis) {
621 Device* device = getDevice(deviceId);
622 device->relativeAxes.add(axis, true);
623 }
624
625 void setKeyCodeState(int32_t deviceId, int32_t keyCode, int32_t state) {
626 Device* device = getDevice(deviceId);
627 device->keyCodeStates.replaceValueFor(keyCode, state);
628 }
629
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000630 void setCountryCode(int32_t deviceId, InputDeviceCountryCode countryCode) {
631 Device* device = getDevice(deviceId);
632 device->countryCode = countryCode;
633 }
634
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635 void setScanCodeState(int32_t deviceId, int32_t scanCode, int32_t state) {
636 Device* device = getDevice(deviceId);
637 device->scanCodeStates.replaceValueFor(scanCode, state);
638 }
639
640 void setSwitchState(int32_t deviceId, int32_t switchCode, int32_t state) {
641 Device* device = getDevice(deviceId);
642 device->switchStates.replaceValueFor(switchCode, state);
643 }
644
645 void setAbsoluteAxisValue(int32_t deviceId, int32_t axis, int32_t value) {
646 Device* device = getDevice(deviceId);
647 device->absoluteAxisValue.replaceValueFor(axis, value);
648 }
649
650 void addKey(int32_t deviceId, int32_t scanCode, int32_t usageCode,
651 int32_t keyCode, uint32_t flags) {
652 Device* device = getDevice(deviceId);
653 KeyInfo info;
654 info.keyCode = keyCode;
655 info.flags = flags;
656 if (scanCode) {
657 device->keysByScanCode.add(scanCode, info);
658 }
659 if (usageCode) {
660 device->keysByUsageCode.add(usageCode, info);
661 }
662 }
663
Philip Junker4af3b3d2021-12-14 10:36:55 +0100664 void addKeyCodeMapping(int32_t deviceId, int32_t fromKeyCode, int32_t toKeyCode) {
665 Device* device = getDevice(deviceId);
666 device->keyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
667 }
668
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 void addLed(int32_t deviceId, int32_t led, bool initialState) {
670 Device* device = getDevice(deviceId);
671 device->leds.add(led, initialState);
672 }
673
Chris Yef59a2f42020-10-16 12:55:26 -0700674 void addSensorAxis(int32_t deviceId, int32_t absCode, InputDeviceSensorType sensorType,
675 int32_t sensorDataIndex) {
676 Device* device = getDevice(deviceId);
677 SensorInfo info;
678 info.sensorType = sensorType;
679 info.sensorDataIndex = sensorDataIndex;
680 device->sensorsByAbsCode.emplace(absCode, info);
681 }
682
683 void setMscEvent(int32_t deviceId, int32_t mscEvent) {
684 Device* device = getDevice(deviceId);
685 typename BitArray<MSC_MAX>::Buffer buffer;
686 buffer[mscEvent / 32] = 1 << mscEvent % 32;
687 device->mscBitmask.loadFromBuffer(buffer);
688 }
689
Chris Ye3fdbfef2021-01-06 18:45:18 -0800690 void addRawLightInfo(int32_t rawId, RawLightInfo&& info) {
691 mRawLightInfos.emplace(rawId, std::move(info));
692 }
693
694 void fakeLightBrightness(int32_t rawId, int32_t brightness) {
695 mLightBrightness.emplace(rawId, brightness);
696 }
697
698 void fakeLightIntensities(int32_t rawId,
699 const std::unordered_map<LightColor, int32_t> intensities) {
700 mLightIntensities.emplace(rawId, std::move(intensities));
701 }
702
Michael Wrightd02c5b62014-02-10 15:10:22 -0800703 bool getLedState(int32_t deviceId, int32_t led) {
704 Device* device = getDevice(deviceId);
705 return device->leds.valueFor(led);
706 }
707
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100708 std::vector<std::string>& getExcludedDevices() {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 return mExcludedDevices;
710 }
711
712 void addVirtualKeyDefinition(int32_t deviceId, const VirtualKeyDefinition& definition) {
713 Device* device = getDevice(deviceId);
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800714 device->virtualKeys.push_back(definition);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715 }
716
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000717 void enqueueEvent(nsecs_t when, nsecs_t readTime, int32_t deviceId, int32_t type, int32_t code,
718 int32_t value) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700719 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800720 RawEvent event;
721 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000722 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800723 event.deviceId = deviceId;
724 event.type = type;
725 event.code = code;
726 event.value = value;
727 mEvents.push_back(event);
728
729 if (type == EV_ABS) {
730 setAbsoluteAxisValue(deviceId, code, value);
731 }
732 }
733
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600734 void setVideoFrames(std::unordered_map<int32_t /*deviceId*/,
735 std::vector<TouchVideoFrame>> videoFrames) {
736 mVideoFrames = std::move(videoFrames);
737 }
738
Michael Wrightd02c5b62014-02-10 15:10:22 -0800739 void assertQueueIsEmpty() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700740 std::unique_lock<std::mutex> lock(mLock);
741 base::ScopedLockAssertion assumeLocked(mLock);
742 const bool queueIsEmpty =
743 mEventsCondition.wait_for(lock, WAIT_TIMEOUT,
744 [this]() REQUIRES(mLock) { return mEvents.size() == 0; });
745 if (!queueIsEmpty) {
746 FAIL() << "Timed out waiting for EventHub queue to be emptied.";
747 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800748 }
749
750private:
751 Device* getDevice(int32_t deviceId) const {
752 ssize_t index = mDevices.indexOfKey(deviceId);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100753 return index >= 0 ? mDevices.valueAt(index) : nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800754 }
755
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700756 ftl::Flags<InputDeviceClass> getDeviceClasses(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800757 Device* device = getDevice(deviceId);
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700758 return device ? device->classes : ftl::Flags<InputDeviceClass>(0);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800759 }
760
Chris Yea52ade12020-08-27 16:49:20 -0700761 InputDeviceIdentifier getDeviceIdentifier(int32_t deviceId) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762 Device* device = getDevice(deviceId);
763 return device ? device->identifier : InputDeviceIdentifier();
764 }
765
Chris Yea52ade12020-08-27 16:49:20 -0700766 int32_t getDeviceControllerNumber(int32_t) const override { return 0; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800767
Chris Yea52ade12020-08-27 16:49:20 -0700768 void getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800769 Device* device = getDevice(deviceId);
770 if (device) {
771 *outConfiguration = device->configuration;
772 }
773 }
774
Chris Yea52ade12020-08-27 16:49:20 -0700775 status_t getAbsoluteAxisInfo(int32_t deviceId, int axis,
776 RawAbsoluteAxisInfo* outAxisInfo) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800777 Device* device = getDevice(deviceId);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700778 if (device) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779 ssize_t index = device->absoluteAxes.indexOfKey(axis);
780 if (index >= 0) {
781 *outAxisInfo = device->absoluteAxes.valueAt(index);
782 return OK;
783 }
784 }
785 outAxisInfo->clear();
786 return -1;
787 }
788
Chris Yea52ade12020-08-27 16:49:20 -0700789 bool hasRelativeAxis(int32_t deviceId, int axis) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800790 Device* device = getDevice(deviceId);
791 if (device) {
792 return device->relativeAxes.indexOfKey(axis) >= 0;
793 }
794 return false;
795 }
796
Chris Yea52ade12020-08-27 16:49:20 -0700797 bool hasInputProperty(int32_t, int) const override { return false; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798
Chris Yef59a2f42020-10-16 12:55:26 -0700799 bool hasMscEvent(int32_t deviceId, int mscEvent) const override final {
800 Device* device = getDevice(deviceId);
801 if (device) {
802 return mscEvent >= 0 && mscEvent <= MSC_MAX ? device->mscBitmask.test(mscEvent) : false;
803 }
804 return false;
805 }
806
Chris Yea52ade12020-08-27 16:49:20 -0700807 status_t mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
808 int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800809 Device* device = getDevice(deviceId);
810 if (device) {
811 const KeyInfo* key = getKey(device, scanCode, usageCode);
812 if (key) {
813 if (outKeycode) {
814 *outKeycode = key->keyCode;
815 }
816 if (outFlags) {
817 *outFlags = key->flags;
818 }
Dmitry Torokhov0faaa0b2015-09-24 13:13:55 -0700819 if (outMetaState) {
820 *outMetaState = metaState;
821 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800822 return OK;
823 }
824 }
825 return NAME_NOT_FOUND;
826 }
827
828 const KeyInfo* getKey(Device* device, int32_t scanCode, int32_t usageCode) const {
829 if (usageCode) {
830 ssize_t index = device->keysByUsageCode.indexOfKey(usageCode);
831 if (index >= 0) {
832 return &device->keysByUsageCode.valueAt(index);
833 }
834 }
835 if (scanCode) {
836 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
837 if (index >= 0) {
838 return &device->keysByScanCode.valueAt(index);
839 }
840 }
Yi Kong9b14ac62018-07-17 13:48:38 -0700841 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800842 }
843
Chris Yea52ade12020-08-27 16:49:20 -0700844 status_t mapAxis(int32_t, int32_t, AxisInfo*) const override { return NAME_NOT_FOUND; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845
Prabir Pradhanae4ff282022-08-23 16:21:39 +0000846 base::Result<std::pair<InputDeviceSensorType, int32_t>> mapSensor(
847 int32_t deviceId, int32_t absCode) const override {
Chris Yef59a2f42020-10-16 12:55:26 -0700848 Device* device = getDevice(deviceId);
849 if (!device) {
850 return Errorf("Sensor device not found.");
851 }
852 auto it = device->sensorsByAbsCode.find(absCode);
853 if (it == device->sensorsByAbsCode.end()) {
854 return Errorf("Sensor map not found.");
855 }
856 const SensorInfo& info = it->second;
857 return std::make_pair(info.sensorType, info.sensorDataIndex);
858 }
859
Chris Yea52ade12020-08-27 16:49:20 -0700860 void setExcludedDevices(const std::vector<std::string>& devices) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861 mExcludedDevices = devices;
862 }
863
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700864 std::vector<RawEvent> getEvents(int) override {
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000865 std::scoped_lock lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700867 std::vector<RawEvent> buffer;
868 std::swap(buffer, mEvents);
Siarhei Vishniakou370039c2021-02-04 22:09:01 +0000869
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700870 mEventsCondition.notify_all();
Siarhei Vishniakou7b3ea0b2022-09-16 14:23:20 -0700871 return buffer;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 }
873
Chris Yea52ade12020-08-27 16:49:20 -0700874 std::vector<TouchVideoFrame> getVideoFrames(int32_t deviceId) override {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -0600875 auto it = mVideoFrames.find(deviceId);
876 if (it != mVideoFrames.end()) {
877 std::vector<TouchVideoFrame> frames = std::move(it->second);
878 mVideoFrames.erase(deviceId);
879 return frames;
880 }
Siarhei Vishniakouadd89292018-12-13 19:23:36 -0800881 return {};
882 }
883
Chris Yea52ade12020-08-27 16:49:20 -0700884 int32_t getScanCodeState(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885 Device* device = getDevice(deviceId);
886 if (device) {
887 ssize_t index = device->scanCodeStates.indexOfKey(scanCode);
888 if (index >= 0) {
889 return device->scanCodeStates.valueAt(index);
890 }
891 }
892 return AKEY_STATE_UNKNOWN;
893 }
894
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +0000895 InputDeviceCountryCode getCountryCode(int32_t deviceId) const override {
896 Device* device = getDevice(deviceId);
897 if (device) {
898 return device->countryCode;
899 }
900 return InputDeviceCountryCode::INVALID;
901 }
902
Chris Yea52ade12020-08-27 16:49:20 -0700903 int32_t getKeyCodeState(int32_t deviceId, int32_t keyCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904 Device* device = getDevice(deviceId);
905 if (device) {
906 ssize_t index = device->keyCodeStates.indexOfKey(keyCode);
907 if (index >= 0) {
908 return device->keyCodeStates.valueAt(index);
909 }
910 }
911 return AKEY_STATE_UNKNOWN;
912 }
913
Chris Yea52ade12020-08-27 16:49:20 -0700914 int32_t getSwitchState(int32_t deviceId, int32_t sw) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800915 Device* device = getDevice(deviceId);
916 if (device) {
917 ssize_t index = device->switchStates.indexOfKey(sw);
918 if (index >= 0) {
919 return device->switchStates.valueAt(index);
920 }
921 }
922 return AKEY_STATE_UNKNOWN;
923 }
924
Chris Yea52ade12020-08-27 16:49:20 -0700925 status_t getAbsoluteAxisValue(int32_t deviceId, int32_t axis,
926 int32_t* outValue) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 Device* device = getDevice(deviceId);
928 if (device) {
929 ssize_t index = device->absoluteAxisValue.indexOfKey(axis);
930 if (index >= 0) {
931 *outValue = device->absoluteAxisValue.valueAt(index);
932 return OK;
933 }
934 }
935 *outValue = 0;
936 return -1;
937 }
938
Philip Junker4af3b3d2021-12-14 10:36:55 +0100939 int32_t getKeyCodeForKeyLocation(int32_t deviceId, int32_t locationKeyCode) const override {
940 Device* device = getDevice(deviceId);
941 if (!device) {
942 return AKEYCODE_UNKNOWN;
943 }
944 auto it = device->keyCodeMapping.find(locationKeyCode);
945 return it != device->keyCodeMapping.end() ? it->second : locationKeyCode;
946 }
947
Chris Yea52ade12020-08-27 16:49:20 -0700948 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 bool markSupportedKeyCodes(int32_t deviceId, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700950 uint8_t* outFlags) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951 bool result = false;
952 Device* device = getDevice(deviceId);
953 if (device) {
Chris Yea52ade12020-08-27 16:49:20 -0700954 result = device->keysByScanCode.size() > 0 || device->keysByUsageCode.size() > 0;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700955 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956 for (size_t j = 0; j < device->keysByScanCode.size(); j++) {
957 if (keyCodes[i] == device->keysByScanCode.valueAt(j).keyCode) {
958 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 }
960 }
961 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
962 if (keyCodes[i] == device->keysByUsageCode.valueAt(j).keyCode) {
963 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800964 }
965 }
966 }
967 }
968 return result;
969 }
970
Chris Yea52ade12020-08-27 16:49:20 -0700971 bool hasScanCode(int32_t deviceId, int32_t scanCode) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800972 Device* device = getDevice(deviceId);
973 if (device) {
974 ssize_t index = device->keysByScanCode.indexOfKey(scanCode);
975 return index >= 0;
976 }
977 return false;
978 }
979
Arthur Hungcb40a002021-08-03 14:31:01 +0000980 bool hasKeyCode(int32_t deviceId, int32_t keyCode) const override {
981 Device* device = getDevice(deviceId);
982 if (!device) {
983 return false;
984 }
985 for (size_t i = 0; i < device->keysByScanCode.size(); i++) {
986 if (keyCode == device->keysByScanCode.valueAt(i).keyCode) {
987 return true;
988 }
989 }
990 for (size_t j = 0; j < device->keysByUsageCode.size(); j++) {
991 if (keyCode == device->keysByUsageCode.valueAt(j).keyCode) {
992 return true;
993 }
994 }
995 return false;
996 }
997
Chris Yea52ade12020-08-27 16:49:20 -0700998 bool hasLed(int32_t deviceId, int32_t led) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800999 Device* device = getDevice(deviceId);
1000 return device && device->leds.indexOfKey(led) >= 0;
1001 }
1002
Chris Yea52ade12020-08-27 16:49:20 -07001003 void setLedState(int32_t deviceId, int32_t led, bool on) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001004 Device* device = getDevice(deviceId);
1005 if (device) {
1006 ssize_t index = device->leds.indexOfKey(led);
1007 if (index >= 0) {
1008 device->leds.replaceValueAt(led, on);
1009 } else {
1010 ADD_FAILURE()
1011 << "Attempted to set the state of an LED that the EventHub declared "
1012 "was not present. led=" << led;
1013 }
1014 }
1015 }
1016
Chris Yea52ade12020-08-27 16:49:20 -07001017 void getVirtualKeyDefinitions(
1018 int32_t deviceId, std::vector<VirtualKeyDefinition>& outVirtualKeys) const override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001019 outVirtualKeys.clear();
1020
1021 Device* device = getDevice(deviceId);
1022 if (device) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001023 outVirtualKeys = device->virtualKeys;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001024 }
1025 }
1026
Chris Yea52ade12020-08-27 16:49:20 -07001027 const std::shared_ptr<KeyCharacterMap> getKeyCharacterMap(int32_t) const override {
Yi Kong9b14ac62018-07-17 13:48:38 -07001028 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001029 }
1030
Chris Yea52ade12020-08-27 16:49:20 -07001031 bool setKeyboardLayoutOverlay(int32_t, std::shared_ptr<KeyCharacterMap>) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001032 return false;
1033 }
1034
Chris Yea52ade12020-08-27 16:49:20 -07001035 void vibrate(int32_t, const VibrationElement&) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001036
Chris Yea52ade12020-08-27 16:49:20 -07001037 void cancelVibrate(int32_t) override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001039 std::vector<int32_t> getVibratorIds(int32_t deviceId) const override { return mVibrators; };
Chris Ye87143712020-11-10 05:05:58 +00001040
Chris Yee2b1e5c2021-03-10 22:45:12 -08001041 std::optional<int32_t> getBatteryCapacity(int32_t, int32_t) const override {
1042 return BATTERY_CAPACITY;
1043 }
Kim Low03ea0352020-11-06 12:45:07 -08001044
Chris Yee2b1e5c2021-03-10 22:45:12 -08001045 std::optional<int32_t> getBatteryStatus(int32_t, int32_t) const override {
1046 return BATTERY_STATUS;
1047 }
1048
Andy Chenf9f1a022022-08-29 20:07:10 -04001049 std::vector<int32_t> getRawBatteryIds(int32_t deviceId) const override {
1050 return {DEFAULT_BATTERY};
1051 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001052
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001053 std::optional<RawBatteryInfo> getRawBatteryInfo(int32_t deviceId,
1054 int32_t batteryId) const override {
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001055 if (batteryId != DEFAULT_BATTERY) return {};
1056 static const auto BATTERY_INFO = RawBatteryInfo{.id = DEFAULT_BATTERY,
1057 .name = "default battery",
1058 .flags = InputBatteryClass::CAPACITY,
1059 .path = BATTERY_DEVPATH};
1060 return BATTERY_INFO;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001061 }
Kim Low03ea0352020-11-06 12:45:07 -08001062
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001063 std::vector<int32_t> getRawLightIds(int32_t deviceId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001064 std::vector<int32_t> ids;
1065 for (const auto& [rawId, info] : mRawLightInfos) {
1066 ids.push_back(rawId);
1067 }
1068 return ids;
1069 }
1070
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001071 std::optional<RawLightInfo> getRawLightInfo(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001072 auto it = mRawLightInfos.find(lightId);
1073 if (it == mRawLightInfos.end()) {
1074 return std::nullopt;
1075 }
1076 return it->second;
1077 }
1078
1079 void setLightBrightness(int32_t deviceId, int32_t lightId, int32_t brightness) override {
1080 mLightBrightness.emplace(lightId, brightness);
1081 }
1082
1083 void setLightIntensities(int32_t deviceId, int32_t lightId,
1084 std::unordered_map<LightColor, int32_t> intensities) override {
1085 mLightIntensities.emplace(lightId, intensities);
1086 };
1087
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001088 std::optional<int32_t> getLightBrightness(int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001089 auto lightIt = mLightBrightness.find(lightId);
1090 if (lightIt == mLightBrightness.end()) {
1091 return std::nullopt;
1092 }
1093 return lightIt->second;
1094 }
1095
1096 std::optional<std::unordered_map<LightColor, int32_t>> getLightIntensities(
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001097 int32_t deviceId, int32_t lightId) const override {
Chris Ye3fdbfef2021-01-06 18:45:18 -08001098 auto lightIt = mLightIntensities.find(lightId);
1099 if (lightIt == mLightIntensities.end()) {
1100 return std::nullopt;
1101 }
1102 return lightIt->second;
1103 };
1104
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001105 void dump(std::string&) const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001106
Prabir Pradhanae4ff282022-08-23 16:21:39 +00001107 void monitor() const override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001108
Chris Yea52ade12020-08-27 16:49:20 -07001109 void requestReopenDevices() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110
Chris Yea52ade12020-08-27 16:49:20 -07001111 void wake() override {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001112};
1113
Michael Wrightd02c5b62014-02-10 15:10:22 -08001114// --- FakeInputMapper ---
1115
1116class FakeInputMapper : public InputMapper {
1117 uint32_t mSources;
1118 int32_t mKeyboardType;
1119 int32_t mMetaState;
1120 KeyedVector<int32_t, int32_t> mKeyCodeStates;
1121 KeyedVector<int32_t, int32_t> mScanCodeStates;
1122 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +01001123 // fake mapping which would normally come from keyCharacterMap
1124 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001125 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001126
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001127 std::mutex mLock;
1128 std::condition_variable mStateChangedCondition;
1129 bool mConfigureWasCalled GUARDED_BY(mLock);
1130 bool mResetWasCalled GUARDED_BY(mLock);
1131 bool mProcessWasCalled GUARDED_BY(mLock);
1132 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001133
Arthur Hungc23540e2018-11-29 20:42:11 +08001134 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001135public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001136 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1137 : InputMapper(deviceContext),
1138 mSources(sources),
1139 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -08001140 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001141 mConfigureWasCalled(false),
1142 mResetWasCalled(false),
1143 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001144
Chris Yea52ade12020-08-27 16:49:20 -07001145 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001146
1147 void setKeyboardType(int32_t keyboardType) {
1148 mKeyboardType = keyboardType;
1149 }
1150
1151 void setMetaState(int32_t metaState) {
1152 mMetaState = metaState;
1153 }
1154
1155 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001156 std::unique_lock<std::mutex> lock(mLock);
1157 base::ScopedLockAssertion assumeLocked(mLock);
1158 const bool configureCalled =
1159 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1160 return mConfigureWasCalled;
1161 });
1162 if (!configureCalled) {
1163 FAIL() << "Expected configure() to have been called.";
1164 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001165 mConfigureWasCalled = false;
1166 }
1167
1168 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001169 std::unique_lock<std::mutex> lock(mLock);
1170 base::ScopedLockAssertion assumeLocked(mLock);
1171 const bool resetCalled =
1172 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1173 return mResetWasCalled;
1174 });
1175 if (!resetCalled) {
1176 FAIL() << "Expected reset() to have been called.";
1177 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178 mResetWasCalled = false;
1179 }
1180
Yi Kong9b14ac62018-07-17 13:48:38 -07001181 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001182 std::unique_lock<std::mutex> lock(mLock);
1183 base::ScopedLockAssertion assumeLocked(mLock);
1184 const bool processCalled =
1185 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
1186 return mProcessWasCalled;
1187 });
1188 if (!processCalled) {
1189 FAIL() << "Expected process() to have been called.";
1190 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001191 if (outLastEvent) {
1192 *outLastEvent = mLastEvent;
1193 }
1194 mProcessWasCalled = false;
1195 }
1196
1197 void setKeyCodeState(int32_t keyCode, int32_t state) {
1198 mKeyCodeStates.replaceValueFor(keyCode, state);
1199 }
1200
1201 void setScanCodeState(int32_t scanCode, int32_t state) {
1202 mScanCodeStates.replaceValueFor(scanCode, state);
1203 }
1204
1205 void setSwitchState(int32_t switchCode, int32_t state) {
1206 mSwitchStates.replaceValueFor(switchCode, state);
1207 }
1208
1209 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08001210 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001211 }
1212
Philip Junker4af3b3d2021-12-14 10:36:55 +01001213 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
1214 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
1215 }
1216
Michael Wrightd02c5b62014-02-10 15:10:22 -08001217private:
Philip Junker4af3b3d2021-12-14 10:36:55 +01001218 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001219
Chris Yea52ade12020-08-27 16:49:20 -07001220 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001221 InputMapper::populateDeviceInfo(deviceInfo);
1222
1223 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
1224 deviceInfo->setKeyboardType(mKeyboardType);
1225 }
1226 }
1227
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001228 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
1229 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001230 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001231 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +08001232
1233 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -08001234 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +08001235 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
1236 mViewport = config->getDisplayViewportByPort(*displayPort);
1237 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001238
1239 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001240 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001241 }
1242
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001243 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001244 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001245 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001246 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001247 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001248 }
1249
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001250 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001251 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001252 mLastEvent = *rawEvent;
1253 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001254 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07001255 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001256 }
1257
Chris Yea52ade12020-08-27 16:49:20 -07001258 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001259 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
1260 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1261 }
1262
Philip Junker4af3b3d2021-12-14 10:36:55 +01001263 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
1264 auto it = mKeyCodeMapping.find(locationKeyCode);
1265 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
1266 }
1267
Chris Yea52ade12020-08-27 16:49:20 -07001268 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001269 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
1270 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1271 }
1272
Chris Yea52ade12020-08-27 16:49:20 -07001273 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001274 ssize_t index = mSwitchStates.indexOfKey(switchCode);
1275 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
1276 }
1277
Chris Yea52ade12020-08-27 16:49:20 -07001278 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001279 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -07001280 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001281 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001282 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
1283 if (keyCodes[i] == mSupportedKeyCodes[j]) {
1284 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001285 }
1286 }
1287 }
Chris Yea52ade12020-08-27 16:49:20 -07001288 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001289 return result;
1290 }
1291
1292 virtual int32_t getMetaState() {
1293 return mMetaState;
1294 }
1295
1296 virtual void fadePointer() {
1297 }
Arthur Hungc23540e2018-11-29 20:42:11 +08001298
1299 virtual std::optional<int32_t> getAssociatedDisplay() {
1300 if (mViewport) {
1301 return std::make_optional(mViewport->displayId);
1302 }
1303 return std::nullopt;
1304 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001305};
1306
1307
1308// --- InstrumentedInputReader ---
1309
1310class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001311 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001312
1313public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001314 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
1315 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001316 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +08001317 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001318
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001319 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001320
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001321 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001322
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001323 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00001324 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -08001325 InputDeviceIdentifier identifier;
1326 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +08001327 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001328 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08001329 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001330 }
1331
Prabir Pradhan28efc192019-11-05 01:10:04 +00001332 // Make the protected loopOnce method accessible to tests.
1333 using InputReader::loopOnce;
1334
Michael Wrightd02c5b62014-02-10 15:10:22 -08001335protected:
Chris Ye1c2e0892020-11-30 21:41:44 -08001336 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
1337 const InputDeviceIdentifier& identifier)
1338 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001339 if (!mNextDevices.empty()) {
1340 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
1341 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001342 return device;
1343 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001344 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001345 }
1346
arthurhungdcef2dc2020-08-11 14:47:50 +08001347 // --- FakeInputReaderContext ---
1348 class FakeInputReaderContext : public ContextImpl {
1349 int32_t mGlobalMetaState;
1350 bool mUpdateGlobalMetaStateWasCalled;
1351 int32_t mGeneration;
1352
1353 public:
1354 FakeInputReaderContext(InputReader* reader)
1355 : ContextImpl(reader),
1356 mGlobalMetaState(0),
1357 mUpdateGlobalMetaStateWasCalled(false),
1358 mGeneration(1) {}
1359
1360 virtual ~FakeInputReaderContext() {}
1361
1362 void assertUpdateGlobalMetaStateWasCalled() {
1363 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1364 << "Expected updateGlobalMetaState() to have been called.";
1365 mUpdateGlobalMetaStateWasCalled = false;
1366 }
1367
1368 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1369
1370 uint32_t getGeneration() { return mGeneration; }
1371
1372 void updateGlobalMetaState() override {
1373 mUpdateGlobalMetaStateWasCalled = true;
1374 ContextImpl::updateGlobalMetaState();
1375 }
1376
1377 int32_t getGlobalMetaState() override {
1378 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1379 }
1380
1381 int32_t bumpGeneration() override {
1382 mGeneration = ContextImpl::bumpGeneration();
1383 return mGeneration;
1384 }
1385 } mFakeContext;
1386
Michael Wrightd02c5b62014-02-10 15:10:22 -08001387 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001388
1389public:
1390 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001391};
1392
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001393// --- InputReaderPolicyTest ---
1394class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001395protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001396 sp<FakeInputReaderPolicy> mFakePolicy;
1397
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001398 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001399 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001400};
1401
1402/**
1403 * Check that empty set of viewports is an acceptable configuration.
1404 * Also try to get internal viewport two different ways - by type and by uniqueId.
1405 *
1406 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1407 * Such configuration is not currently allowed.
1408 */
1409TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001410 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001411
1412 // We didn't add any viewports yet, so there shouldn't be any.
1413 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001414 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001415 ASSERT_FALSE(internalViewport);
1416
1417 // Add an internal viewport, then clear it
1418 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001419 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001420 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421
1422 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001423 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001424 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001425 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001426
1427 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001428 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001429 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001430 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001431
1432 mFakePolicy->clearViewports();
1433 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001434 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001435 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001436 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001437 ASSERT_FALSE(internalViewport);
1438}
1439
1440TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1441 const std::string internalUniqueId = "local:0";
1442 const std::string externalUniqueId = "local:1";
1443 const std::string virtualUniqueId1 = "virtual:2";
1444 const std::string virtualUniqueId2 = "virtual:3";
1445 constexpr int32_t virtualDisplayId1 = 2;
1446 constexpr int32_t virtualDisplayId2 = 3;
1447
1448 // Add an internal viewport
1449 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001450 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1451 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001452 // Add an external viewport
1453 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001454 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1455 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456 // Add an virtual viewport
1457 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001458 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1459 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001460 // Add another virtual viewport
1461 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001462 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1463 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001464
1465 // Check matching by type for internal
1466 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001467 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001468 ASSERT_TRUE(internalViewport);
1469 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1470
1471 // Check matching by type for external
1472 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001473 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001474 ASSERT_TRUE(externalViewport);
1475 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1476
1477 // Check matching by uniqueId for virtual viewport #1
1478 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001479 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001480 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001481 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001482 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1483 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1484
1485 // Check matching by uniqueId for virtual viewport #2
1486 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001487 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001488 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001489 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001490 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1491 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1492}
1493
1494
1495/**
1496 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1497 * that lookup works by checking display id.
1498 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1499 */
1500TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1501 const std::string uniqueId1 = "uniqueId1";
1502 const std::string uniqueId2 = "uniqueId2";
1503 constexpr int32_t displayId1 = 2;
1504 constexpr int32_t displayId2 = 3;
1505
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001506 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1507 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001508 for (const ViewportType& type : types) {
1509 mFakePolicy->clearViewports();
1510 // Add a viewport
1511 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001512 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1513 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001514 // Add another viewport
1515 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001516 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1517 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001518
1519 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001520 std::optional<DisplayViewport> viewport1 =
1521 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001522 ASSERT_TRUE(viewport1);
1523 ASSERT_EQ(displayId1, viewport1->displayId);
1524 ASSERT_EQ(type, viewport1->type);
1525
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001526 std::optional<DisplayViewport> viewport2 =
1527 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001528 ASSERT_TRUE(viewport2);
1529 ASSERT_EQ(displayId2, viewport2->displayId);
1530 ASSERT_EQ(type, viewport2->type);
1531
1532 // When there are multiple viewports of the same kind, and uniqueId is not specified
1533 // in the call to getDisplayViewport, then that situation is not supported.
1534 // The viewports can be stored in any order, so we cannot rely on the order, since that
1535 // is just implementation detail.
1536 // However, we can check that it still returns *a* viewport, we just cannot assert
1537 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001538 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001539 ASSERT_TRUE(someViewport);
1540 }
1541}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001542
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001543/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001544 * When we have multiple internal displays make sure we always return the default display when
1545 * querying by type.
1546 */
1547TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1548 const std::string uniqueId1 = "uniqueId1";
1549 const std::string uniqueId2 = "uniqueId2";
1550 constexpr int32_t nonDefaultDisplayId = 2;
1551 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1552 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1553
1554 // Add the default display first and ensure it gets returned.
1555 mFakePolicy->clearViewports();
1556 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001557 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001558 ViewportType::INTERNAL);
1559 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001560 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001561 ViewportType::INTERNAL);
1562
1563 std::optional<DisplayViewport> viewport =
1564 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1565 ASSERT_TRUE(viewport);
1566 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1567 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1568
1569 // Add the default display second to make sure order doesn't matter.
1570 mFakePolicy->clearViewports();
1571 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001572 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001573 ViewportType::INTERNAL);
1574 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001575 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001576 ViewportType::INTERNAL);
1577
1578 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1579 ASSERT_TRUE(viewport);
1580 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1581 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1582}
1583
1584/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001585 * Check getDisplayViewportByPort
1586 */
1587TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001588 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001589 const std::string uniqueId1 = "uniqueId1";
1590 const std::string uniqueId2 = "uniqueId2";
1591 constexpr int32_t displayId1 = 1;
1592 constexpr int32_t displayId2 = 2;
1593 const uint8_t hdmi1 = 0;
1594 const uint8_t hdmi2 = 1;
1595 const uint8_t hdmi3 = 2;
1596
1597 mFakePolicy->clearViewports();
1598 // Add a viewport that's associated with some display port that's not of interest.
1599 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001600 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1601 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001602 // Add another viewport, connected to HDMI1 port
1603 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001604 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1605 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001606
1607 // Check that correct display viewport was returned by comparing the display ports.
1608 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1609 ASSERT_TRUE(hdmi1Viewport);
1610 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1611 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1612
1613 // Check that we can still get the same viewport using the uniqueId
1614 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1615 ASSERT_TRUE(hdmi1Viewport);
1616 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1617 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1618 ASSERT_EQ(type, hdmi1Viewport->type);
1619
1620 // Check that we cannot find a port with "HDMI2", because we never added one
1621 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1622 ASSERT_FALSE(hdmi2Viewport);
1623}
1624
Michael Wrightd02c5b62014-02-10 15:10:22 -08001625// --- InputReaderTest ---
1626
1627class InputReaderTest : public testing::Test {
1628protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001629 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001630 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001631 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001632 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001633
Chris Yea52ade12020-08-27 16:49:20 -07001634 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001635 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001636 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001637 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001638
Prabir Pradhan28efc192019-11-05 01:10:04 +00001639 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001640 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001641 }
1642
Chris Yea52ade12020-08-27 16:49:20 -07001643 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001644 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001645 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001646 }
1647
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001648 void addDevice(int32_t eventHubId, const std::string& name,
1649 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001650 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001651
1652 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001653 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001654 }
1655 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001656 mReader->loopOnce();
1657 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001658 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1659 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001660 }
1661
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001662 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001663 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001664 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001665 }
1666
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001667 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001668 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001669 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001670 }
1671
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001672 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001673 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001674 ftl::Flags<InputDeviceClass> classes,
1675 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001676 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001677 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1678 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001679 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001680 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001681 return mapper;
1682 }
1683};
1684
Chris Ye98d3f532020-10-01 21:48:59 -07001685TEST_F(InputReaderTest, PolicyGetInputDevices) {
1686 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001687 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001688 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001689
1690 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001691 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001692 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001693 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001694 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001695 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1696 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001697 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001698}
1699
Chris Yee7310032020-09-22 15:36:28 -07001700TEST_F(InputReaderTest, GetMergedInputDevices) {
1701 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1702 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1703 // Add two subdevices to device
1704 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1705 // Must add at least one mapper or the device will be ignored!
1706 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1707 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1708
1709 // Push same device instance for next device to be added, so they'll have same identifier.
1710 mReader->pushNextDevice(device);
1711 mReader->pushNextDevice(device);
1712 ASSERT_NO_FATAL_FAILURE(
1713 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1714 ASSERT_NO_FATAL_FAILURE(
1715 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1716
1717 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001718 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001719}
1720
Chris Yee14523a2020-12-19 13:46:00 -08001721TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1723 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1724 // Add two subdevices to device
1725 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1726 // Must add at least one mapper or the device will be ignored!
1727 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1728 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1729
1730 // Push same device instance for next device to be added, so they'll have same identifier.
1731 mReader->pushNextDevice(device);
1732 mReader->pushNextDevice(device);
1733 // Sensor device is initially disabled
1734 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1735 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1736 nullptr));
1737 // Device is disabled because the only sub device is a sensor device and disabled initially.
1738 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1739 ASSERT_FALSE(device->isEnabled());
1740 ASSERT_NO_FATAL_FAILURE(
1741 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1742 // The merged device is enabled if any sub device is enabled
1743 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1744 ASSERT_TRUE(device->isEnabled());
1745}
1746
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001747TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001748 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001749 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001750 constexpr int32_t eventHubId = 1;
1751 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001752 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001753 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001754 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001755 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001756
Yi Kong9b14ac62018-07-17 13:48:38 -07001757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001758
1759 NotifyDeviceResetArgs resetArgs;
1760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001761 ASSERT_EQ(deviceId, resetArgs.deviceId);
1762
1763 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001764 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001765 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001766
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001768 ASSERT_EQ(deviceId, resetArgs.deviceId);
1769 ASSERT_EQ(device->isEnabled(), false);
1770
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001771 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001772 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001775 ASSERT_EQ(device->isEnabled(), false);
1776
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001777 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001778 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001780 ASSERT_EQ(deviceId, resetArgs.deviceId);
1781 ASSERT_EQ(device->isEnabled(), true);
1782}
1783
Michael Wrightd02c5b62014-02-10 15:10:22 -08001784TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001785 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001786 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001787 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001788 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001789 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001790 AINPUT_SOURCE_KEYBOARD, nullptr);
1791 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001792
1793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1794 AINPUT_SOURCE_ANY, AKEYCODE_A))
1795 << "Should return unknown when the device id is >= 0 but unknown.";
1796
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001797 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1798 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1799 << "Should return unknown when the device id is valid but the sources are not "
1800 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001801
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001802 ASSERT_EQ(AKEY_STATE_DOWN,
1803 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1804 AKEYCODE_A))
1805 << "Should return value provided by mapper when device id is valid and the device "
1806 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001807
1808 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1809 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1810 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1811
1812 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1813 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1814 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1815}
1816
Philip Junker4af3b3d2021-12-14 10:36:55 +01001817TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1818 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1819 constexpr int32_t eventHubId = 1;
1820 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1821 InputDeviceClass::KEYBOARD,
1822 AINPUT_SOURCE_KEYBOARD, nullptr);
1823 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1824
1825 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1826 << "Should return unknown when the device with the specified id is not found.";
1827
1828 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1829 << "Should return correct mapping when device id is valid and mapping exists.";
1830
1831 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1832 << "Should return the location key code when device id is valid and there's no "
1833 "mapping.";
1834}
1835
1836TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1837 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1838 constexpr int32_t eventHubId = 1;
1839 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1840 InputDeviceClass::JOYSTICK,
1841 AINPUT_SOURCE_GAMEPAD, nullptr);
1842 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1843
1844 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1845 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1846}
1847
Michael Wrightd02c5b62014-02-10 15:10:22 -08001848TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001849 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001850 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001851 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001852 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001853 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001854 AINPUT_SOURCE_KEYBOARD, nullptr);
1855 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001856
1857 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1858 AINPUT_SOURCE_ANY, KEY_A))
1859 << "Should return unknown when the device id is >= 0 but unknown.";
1860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001861 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1862 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1863 << "Should return unknown when the device id is valid but the sources are not "
1864 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001865
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001866 ASSERT_EQ(AKEY_STATE_DOWN,
1867 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1868 KEY_A))
1869 << "Should return value provided by mapper when device id is valid and the device "
1870 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001871
1872 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1873 AINPUT_SOURCE_TRACKBALL, KEY_A))
1874 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1875
1876 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1877 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1878 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1879}
1880
1881TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001882 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001883 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001884 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001885 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001886 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001887 AINPUT_SOURCE_KEYBOARD, nullptr);
1888 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001889
1890 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1891 AINPUT_SOURCE_ANY, SW_LID))
1892 << "Should return unknown when the device id is >= 0 but unknown.";
1893
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001894 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1895 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1896 << "Should return unknown when the device id is valid but the sources are not "
1897 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001898
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001899 ASSERT_EQ(AKEY_STATE_DOWN,
1900 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1901 SW_LID))
1902 << "Should return value provided by mapper when device id is valid and the device "
1903 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001904
1905 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1906 AINPUT_SOURCE_TRACKBALL, SW_LID))
1907 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1908
1909 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1910 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1911 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1912}
1913
1914TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001915 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001916 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001917 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001918 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001919 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001920 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001921
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001922 mapper.addSupportedKeyCode(AKEYCODE_A);
1923 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001924
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001925 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001926 uint8_t flags[4] = { 0, 0, 0, 1 };
1927
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001928 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001929 << "Should return false when device id is >= 0 but unknown.";
1930 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1931
1932 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001933 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001934 << "Should return false when device id is valid but the sources are not supported by "
1935 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001936 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1937
1938 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001939 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001940 keyCodes, flags))
1941 << "Should return value provided by mapper when device id is valid and the device "
1942 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001943 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1944
1945 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001946 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1947 << "Should return false when the device id is < 0 but the sources are not supported by "
1948 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001949 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1950
1951 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001952 ASSERT_TRUE(
1953 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1954 << "Should return value provided by mapper when device id is < 0 and one of the "
1955 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001956 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1957}
1958
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001959TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001960 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001961 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001962
1963 NotifyConfigurationChangedArgs args;
1964
1965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1966 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1967}
1968
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001969TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001970 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001971 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001972 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001973 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001974 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001975 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001976 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001977 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001978
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001979 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001980 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001981 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1982
1983 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001984 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001985 ASSERT_EQ(when, event.when);
1986 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001987 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001988 ASSERT_EQ(EV_KEY, event.type);
1989 ASSERT_EQ(KEY_A, event.code);
1990 ASSERT_EQ(1, event.value);
1991}
1992
Garfield Tan1c7bc862020-01-28 13:24:04 -08001993TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001994 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001995 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001996 constexpr int32_t eventHubId = 1;
1997 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001998 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001999 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002000 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002001 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08002002
2003 NotifyDeviceResetArgs resetArgs;
2004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002005 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002007 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002008 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002010 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002011 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002013 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002014 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002016 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002017 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002019 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002020 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002022 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002023 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002024}
2025
Garfield Tan1c7bc862020-01-28 13:24:04 -08002026TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
2027 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002028 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002029 constexpr int32_t eventHubId = 1;
2030 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2031 // Must add at least one mapper or the device will be ignored!
2032 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002033 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002034 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2035
2036 NotifyDeviceResetArgs resetArgs;
2037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2038 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2039}
2040
Arthur Hungc23540e2018-11-29 20:42:11 +08002041TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002042 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002043 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002044 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002045 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002046 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2047 FakeInputMapper& mapper =
2048 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002049 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002050
2051 const uint8_t hdmi1 = 1;
2052
2053 // Associated touch screen with second display.
2054 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2055
2056 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002057 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002058 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002059 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002060 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002061 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002062 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002063 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002064 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002065 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002066
2067 // Add the device, and make sure all of the callbacks are triggered.
2068 // The device is added after the input port associations are processed since
2069 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002070 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002073 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002074
Arthur Hung2c9a3342019-07-23 14:18:59 +08002075 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002076 ASSERT_EQ(deviceId, device->getId());
2077 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2078 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002079
2080 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002081 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002082 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002083 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002084}
2085
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002086TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2087 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002088 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002089 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2090 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2091 // Must add at least one mapper or the device will be ignored!
2092 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2093 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2094 mReader->pushNextDevice(device);
2095 mReader->pushNextDevice(device);
2096 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2097 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2098
2099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2100
2101 NotifyDeviceResetArgs resetArgs;
2102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2103 ASSERT_EQ(deviceId, resetArgs.deviceId);
2104 ASSERT_TRUE(device->isEnabled());
2105 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2106 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2107
2108 disableDevice(deviceId);
2109 mReader->loopOnce();
2110
2111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2112 ASSERT_EQ(deviceId, resetArgs.deviceId);
2113 ASSERT_FALSE(device->isEnabled());
2114 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2115 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2116
2117 enableDevice(deviceId);
2118 mReader->loopOnce();
2119
2120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2121 ASSERT_EQ(deviceId, resetArgs.deviceId);
2122 ASSERT_TRUE(device->isEnabled());
2123 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2124 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2125}
2126
2127TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2128 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002129 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002130 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2131 // Add two subdevices to device
2132 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2133 FakeInputMapper& mapperDevice1 =
2134 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2135 FakeInputMapper& mapperDevice2 =
2136 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2137 mReader->pushNextDevice(device);
2138 mReader->pushNextDevice(device);
2139 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2140 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2141
2142 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2143 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2144
2145 ASSERT_EQ(AKEY_STATE_DOWN,
2146 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2147 ASSERT_EQ(AKEY_STATE_DOWN,
2148 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2149 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2150 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2151}
2152
Prabir Pradhan7e186182020-11-10 13:56:45 -08002153TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2154 NotifyPointerCaptureChangedArgs args;
2155
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002156 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002157 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2158 mReader->loopOnce();
2159 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002160 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2161 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002162
2163 mFakePolicy->setPointerCapture(false);
2164 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2165 mReader->loopOnce();
2166 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002167 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002168
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002169 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002170 // does not change.
2171 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2172 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002173 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002174}
2175
Chris Ye87143712020-11-10 05:05:58 +00002176class FakeVibratorInputMapper : public FakeInputMapper {
2177public:
2178 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2179 : FakeInputMapper(deviceContext, sources) {}
2180
2181 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2182};
2183
2184TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2185 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002186 ftl::Flags<InputDeviceClass> deviceClass =
2187 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002188 constexpr int32_t eventHubId = 1;
2189 const char* DEVICE_LOCATION = "BLUETOOTH";
2190 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2191 FakeVibratorInputMapper& mapper =
2192 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2193 mReader->pushNextDevice(device);
2194
2195 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2196 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2197
2198 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2199 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2200}
2201
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002202// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002203
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002204class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002205public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002206 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002207
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002208 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002209
Andy Chenf9f1a022022-08-29 20:07:10 -04002210 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2211
Chris Yee2b1e5c2021-03-10 22:45:12 -08002212 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2213
2214 void dump(std::string& dump) override {}
2215
2216 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2217 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002218 }
2219
Chris Yee2b1e5c2021-03-10 22:45:12 -08002220 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2221 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002222 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002223
2224 bool setLightColor(int32_t lightId, int32_t color) override {
2225 getDeviceContext().setLightBrightness(lightId, color >> 24);
2226 return true;
2227 }
2228
2229 std::optional<int32_t> getLightColor(int32_t lightId) override {
2230 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2231 if (!result.has_value()) {
2232 return std::nullopt;
2233 }
2234 return result.value() << 24;
2235 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002236
2237 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2238
2239 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2240
2241private:
2242 InputDeviceContext& mDeviceContext;
2243 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2244 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002245 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002246};
2247
Chris Yee2b1e5c2021-03-10 22:45:12 -08002248TEST_F(InputReaderTest, BatteryGetCapacity) {
2249 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002250 ftl::Flags<InputDeviceClass> deviceClass =
2251 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002252 constexpr int32_t eventHubId = 1;
2253 const char* DEVICE_LOCATION = "BLUETOOTH";
2254 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002255 FakePeripheralController& controller =
2256 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002257 mReader->pushNextDevice(device);
2258
2259 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2260
2261 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2262 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2263}
2264
2265TEST_F(InputReaderTest, BatteryGetStatus) {
2266 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002267 ftl::Flags<InputDeviceClass> deviceClass =
2268 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002269 constexpr int32_t eventHubId = 1;
2270 const char* DEVICE_LOCATION = "BLUETOOTH";
2271 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002272 FakePeripheralController& controller =
2273 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002274 mReader->pushNextDevice(device);
2275
2276 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2277
2278 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2279 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2280}
2281
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002282TEST_F(InputReaderTest, BatteryGetDevicePath) {
2283 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2284 ftl::Flags<InputDeviceClass> deviceClass =
2285 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2286 constexpr int32_t eventHubId = 1;
2287 const char* DEVICE_LOCATION = "BLUETOOTH";
2288 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2289 device->addController<FakePeripheralController>(eventHubId);
2290 mReader->pushNextDevice(device);
2291
2292 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2293
2294 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2295}
2296
Chris Ye3fdbfef2021-01-06 18:45:18 -08002297TEST_F(InputReaderTest, LightGetColor) {
2298 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002299 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002300 constexpr int32_t eventHubId = 1;
2301 const char* DEVICE_LOCATION = "BLUETOOTH";
2302 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002303 FakePeripheralController& controller =
2304 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002305 mReader->pushNextDevice(device);
2306 RawLightInfo info = {.id = 1,
2307 .name = "Mono",
2308 .maxBrightness = 255,
2309 .flags = InputLightClass::BRIGHTNESS,
2310 .path = ""};
2311 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2312 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2313
2314 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002315
Chris Yee2b1e5c2021-03-10 22:45:12 -08002316 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2317 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002318 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2319 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2320}
2321
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002322// --- InputReaderIntegrationTest ---
2323
2324// These tests create and interact with the InputReader only through its interface.
2325// The InputReader is started during SetUp(), which starts its processing in its own
2326// thread. The tests use linux uinput to emulate input devices.
2327// NOTE: Interacting with the physical device while these tests are running may cause
2328// the tests to fail.
2329class InputReaderIntegrationTest : public testing::Test {
2330protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002331 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002332 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002333 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002334
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002335 std::shared_ptr<FakePointerController> mFakePointerController;
2336
Chris Yea52ade12020-08-27 16:49:20 -07002337 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002338#if !defined(__ANDROID__)
2339 GTEST_SKIP();
2340#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002341 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002342 mFakePointerController = std::make_shared<FakePointerController>();
2343 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002344 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2345 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002346
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002347 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2348 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002349 ASSERT_EQ(mReader->start(), OK);
2350
2351 // Since this test is run on a real device, all the input devices connected
2352 // to the test device will show up in mReader. We wait for those input devices to
2353 // show up before beginning the tests.
2354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2356 }
2357
Chris Yea52ade12020-08-27 16:49:20 -07002358 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002359#if !defined(__ANDROID__)
2360 return;
2361#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002362 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002363 mReader.reset();
2364 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002365 mFakePolicy.clear();
2366 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00002367
2368 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
2369 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
2370 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
2371 [&name](const InputDeviceInfo& info) {
2372 return info.getIdentifier().name == name;
2373 });
2374 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
2375 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002376};
2377
2378TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2379 // An invalid input device that is only used for this test.
2380 class InvalidUinputDevice : public UinputDevice {
2381 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00002382 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002383
2384 private:
2385 void configureDevice(int fd, uinput_user_dev* device) override {}
2386 };
2387
2388 const size_t numDevices = mFakePolicy->getInputDevices().size();
2389
2390 // UinputDevice does not set any event or key bits, so InputReader should not
2391 // consider it as a valid device.
2392 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2393 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2394 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2395 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2396
2397 invalidDevice.reset();
2398 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2399 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2400 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2401}
2402
2403TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2404 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2405
2406 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2407 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2408 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2409 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2410
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002411 const auto device = findDeviceByName(keyboard->getName());
2412 ASSERT_TRUE(device.has_value());
2413 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2414 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
2415 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002416
2417 keyboard.reset();
2418 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2420 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2421}
2422
2423TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2424 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2425 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2426
2427 NotifyConfigurationChangedArgs configChangedArgs;
2428 ASSERT_NO_FATAL_FAILURE(
2429 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002430 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002431 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2432
2433 NotifyKeyArgs keyArgs;
2434 keyboard->pressAndReleaseHomeKey();
2435 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2436 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002437 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002438 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002439 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002440 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002441 prevTimestamp = keyArgs.eventTime;
2442
2443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2444 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002445 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002446 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002447 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002448}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002450TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
2451 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2452 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2453
2454 const auto device = findDeviceByName(stylus->getName());
2455 ASSERT_TRUE(device.has_value());
2456
Prabir Pradhana3621852022-10-14 18:57:23 +00002457 // An external stylus with buttons should also be recognized as a keyboard.
2458 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002459 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
2460 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2461
2462 const auto DOWN =
2463 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
2464 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
2465
2466 stylus->pressAndReleaseKey(BTN_STYLUS);
2467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2468 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2470 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2471
2472 stylus->pressAndReleaseKey(BTN_STYLUS2);
2473 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2474 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2475 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2476 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2477
2478 stylus->pressAndReleaseKey(BTN_STYLUS3);
2479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2480 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2482 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2483}
2484
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002485/**
2486 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2487 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2488 * are passed to the listener.
2489 */
2490static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2491TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2492 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2493 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2494 NotifyKeyArgs keyArgs;
2495
2496 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2497 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2499 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2500
2501 controller->pressAndReleaseKey(BTN_GEAR_UP);
2502 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2503 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2504 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2505}
2506
Arthur Hungaab25622020-01-16 11:22:11 +08002507// --- TouchProcessTest ---
2508class TouchIntegrationTest : public InputReaderIntegrationTest {
2509protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002510 const std::string UNIQUE_ID = "local:0";
2511
Chris Yea52ade12020-08-27 16:49:20 -07002512 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002513#if !defined(__ANDROID__)
2514 GTEST_SKIP();
2515#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002516 InputReaderIntegrationTest::SetUp();
2517 // At least add an internal display.
2518 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2519 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002520 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002521
2522 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2523 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2524 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002525 const auto info = findDeviceByName(mDevice->getName());
2526 ASSERT_TRUE(info);
2527 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002528 }
2529
2530 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2531 int32_t orientation, const std::string& uniqueId,
2532 std::optional<uint8_t> physicalPort,
2533 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002534 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2535 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002536 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2537 }
2538
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002539 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2540 NotifyMotionArgs args;
2541 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2542 EXPECT_EQ(action, args.action);
2543 ASSERT_EQ(points.size(), args.pointerCount);
2544 for (size_t i = 0; i < args.pointerCount; i++) {
2545 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2546 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2547 }
2548 }
2549
Arthur Hungaab25622020-01-16 11:22:11 +08002550 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002551 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002552};
2553
2554TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2555 NotifyMotionArgs args;
2556 const Point centerPoint = mDevice->getCenterPoint();
2557
2558 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002559 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002560 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002561 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002562 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2563 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2564
2565 // ACTION_MOVE
2566 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002567 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002568 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2570
2571 // ACTION_UP
2572 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002573 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002574 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2575 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2576}
2577
2578TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2579 NotifyMotionArgs args;
2580 const Point centerPoint = mDevice->getCenterPoint();
2581
2582 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002583 mDevice->sendSlot(FIRST_SLOT);
2584 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002585 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002586 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002587 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2588 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2589
2590 // ACTION_POINTER_DOWN (Second slot)
2591 const Point secondPoint = centerPoint + Point(100, 100);
2592 mDevice->sendSlot(SECOND_SLOT);
2593 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002594 mDevice->sendDown(secondPoint);
2595 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002596 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002597 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002598
2599 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002600 mDevice->sendMove(secondPoint + Point(1, 1));
2601 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002602 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2604
2605 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002606 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002607 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002608 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002609 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002610
2611 // ACTION_UP
2612 mDevice->sendSlot(FIRST_SLOT);
2613 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002614 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002615 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2616 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2617}
2618
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002619/**
2620 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2621 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2622 * data?
2623 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2624 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2625 * for Pointer 0 only is generated after.
2626 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2627 * events, we will not miss any information.
2628 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2629 * event generated afterwards that contains the newest movement of pointer 0.
2630 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2631 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2632 * losing information about non-palm pointers.
2633 */
2634TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2635 NotifyMotionArgs args;
2636 const Point centerPoint = mDevice->getCenterPoint();
2637
2638 // ACTION_DOWN
2639 mDevice->sendSlot(FIRST_SLOT);
2640 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2641 mDevice->sendDown(centerPoint);
2642 mDevice->sendSync();
2643 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2644
2645 // ACTION_POINTER_DOWN (Second slot)
2646 const Point secondPoint = centerPoint + Point(100, 100);
2647 mDevice->sendSlot(SECOND_SLOT);
2648 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2649 mDevice->sendDown(secondPoint);
2650 mDevice->sendSync();
2651 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2652
2653 // ACTION_MOVE (First slot)
2654 mDevice->sendSlot(FIRST_SLOT);
2655 mDevice->sendMove(centerPoint + Point(5, 5));
2656 // ACTION_POINTER_UP (Second slot)
2657 mDevice->sendSlot(SECOND_SLOT);
2658 mDevice->sendPointerUp();
2659 // Send a single sync for the above 2 pointer updates
2660 mDevice->sendSync();
2661
2662 // First, we should get POINTER_UP for the second pointer
2663 assertReceivedMotion(ACTION_POINTER_1_UP,
2664 {/*first pointer */ centerPoint + Point(5, 5),
2665 /*second pointer*/ secondPoint});
2666
2667 // Next, the MOVE event for the first pointer
2668 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2669}
2670
2671/**
2672 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2673 * move, and then it will go up, all in the same frame.
2674 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2675 * gets sent to the listener.
2676 */
2677TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2678 NotifyMotionArgs args;
2679 const Point centerPoint = mDevice->getCenterPoint();
2680
2681 // ACTION_DOWN
2682 mDevice->sendSlot(FIRST_SLOT);
2683 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2684 mDevice->sendDown(centerPoint);
2685 mDevice->sendSync();
2686 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2687
2688 // ACTION_POINTER_DOWN (Second slot)
2689 const Point secondPoint = centerPoint + Point(100, 100);
2690 mDevice->sendSlot(SECOND_SLOT);
2691 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2692 mDevice->sendDown(secondPoint);
2693 mDevice->sendSync();
2694 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2695
2696 // ACTION_MOVE (First slot)
2697 mDevice->sendSlot(FIRST_SLOT);
2698 mDevice->sendMove(centerPoint + Point(5, 5));
2699 // ACTION_POINTER_UP (Second slot)
2700 mDevice->sendSlot(SECOND_SLOT);
2701 mDevice->sendMove(secondPoint + Point(6, 6));
2702 mDevice->sendPointerUp();
2703 // Send a single sync for the above 2 pointer updates
2704 mDevice->sendSync();
2705
2706 // First, we should get POINTER_UP for the second pointer
2707 // The movement of the second pointer during the liftoff frame is ignored.
2708 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2709 assertReceivedMotion(ACTION_POINTER_1_UP,
2710 {/*first pointer */ centerPoint + Point(5, 5),
2711 /*second pointer*/ secondPoint});
2712
2713 // Next, the MOVE event for the first pointer
2714 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2715}
2716
Arthur Hungaab25622020-01-16 11:22:11 +08002717TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2718 NotifyMotionArgs args;
2719 const Point centerPoint = mDevice->getCenterPoint();
2720
2721 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002722 mDevice->sendSlot(FIRST_SLOT);
2723 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002724 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002725 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002726 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2727 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2728
arthurhungcc7f9802020-04-30 17:55:40 +08002729 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002730 const Point secondPoint = centerPoint + Point(100, 100);
2731 mDevice->sendSlot(SECOND_SLOT);
2732 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2733 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002734 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002735 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002736 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002737
arthurhungcc7f9802020-04-30 17:55:40 +08002738 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002739 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002740 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002741 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2743
arthurhungcc7f9802020-04-30 17:55:40 +08002744 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2745 // a palm event.
2746 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002747 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002748 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002749 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002750 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002751 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002752
arthurhungcc7f9802020-04-30 17:55:40 +08002753 // Send up to second slot, expect first slot send moving.
2754 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002755 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002756 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002758
arthurhungcc7f9802020-04-30 17:55:40 +08002759 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002760 mDevice->sendSlot(FIRST_SLOT);
2761 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002762 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002763
arthurhungcc7f9802020-04-30 17:55:40 +08002764 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2765 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002766}
2767
Prabir Pradhanda20b172022-09-26 17:01:18 +00002768TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2769 const Point centerPoint = mDevice->getCenterPoint();
2770
2771 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2772 mDevice->sendSlot(FIRST_SLOT);
2773 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2774 mDevice->sendToolType(MT_TOOL_PEN);
2775 mDevice->sendDown(centerPoint);
2776 mDevice->sendSync();
2777 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2778 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2779 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2780
2781 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2782
2783 // Release the stylus touch.
2784 mDevice->sendUp();
2785 mDevice->sendSync();
2786 ASSERT_NO_FATAL_FAILURE(
2787 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2788
2789 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2790
2791 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2792 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2793 mDevice->sendToolType(MT_TOOL_FINGER);
2794 mDevice->sendDown(centerPoint);
2795 mDevice->sendSync();
2796 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2797 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2798 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2799
2800 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2801
2802 mDevice->sendUp();
2803 mDevice->sendSync();
2804 ASSERT_NO_FATAL_FAILURE(
2805 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2806
2807 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2808 // The policy should be notified of the stylus presence.
2809 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2810 mDevice->sendToolType(MT_TOOL_PEN);
2811 mDevice->sendMove(centerPoint);
2812 mDevice->sendSync();
2813 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2814 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2815 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2816
2817 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2818}
2819
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002820TEST_F(TouchIntegrationTest, StylusButtonsGenerateKeyEvents) {
2821 mDevice->sendKey(BTN_STYLUS, 1);
2822 mDevice->sendSync();
2823 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2824 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2825 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2826
2827 mDevice->sendKey(BTN_STYLUS, 0);
2828 mDevice->sendSync();
2829 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2830 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2831 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2832}
2833
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002834TEST_F(TouchIntegrationTest, StylusButtonsSurroundingTouchGesture) {
2835 const Point centerPoint = mDevice->getCenterPoint();
2836
2837 // Press the stylus button.
2838 mDevice->sendKey(BTN_STYLUS, 1);
2839 mDevice->sendSync();
2840 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2841 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2842 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2843
2844 // Start and finish a stylus gesture.
2845 mDevice->sendSlot(FIRST_SLOT);
2846 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2847 mDevice->sendToolType(MT_TOOL_PEN);
2848 mDevice->sendDown(centerPoint);
2849 mDevice->sendSync();
2850 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2851 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2852 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2853 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2854 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2855 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2856 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2857 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2858
2859 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2860 mDevice->sendSync();
2861 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2862 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2863 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2864 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2865 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2866 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2867
2868 // Release the stylus button.
2869 mDevice->sendKey(BTN_STYLUS, 0);
2870 mDevice->sendSync();
2871 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2872 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2873 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2874}
2875
2876TEST_F(TouchIntegrationTest, StylusButtonsWithinTouchGesture) {
2877 const Point centerPoint = mDevice->getCenterPoint();
2878
2879 // Start a stylus gesture.
2880 mDevice->sendSlot(FIRST_SLOT);
2881 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2882 mDevice->sendToolType(MT_TOOL_PEN);
2883 mDevice->sendDown(centerPoint);
2884 mDevice->sendSync();
2885 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2886 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2887 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2888
2889 // Press and release a stylus button. Each change in button state also generates a MOVE event.
2890 mDevice->sendKey(BTN_STYLUS, 1);
2891 mDevice->sendSync();
2892 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2893 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2894 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2895 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2896 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2897 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2898 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2899 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2900 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2901 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2902 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2903
2904 mDevice->sendKey(BTN_STYLUS, 0);
2905 mDevice->sendSync();
2906 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2907 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2908 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2909 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2910 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2911 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2912 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2913 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2914 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2915
2916 // Finish the stylus gesture.
2917 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2918 mDevice->sendSync();
2919 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2920 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2921 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2922}
2923
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002925class InputDeviceTest : public testing::Test {
2926protected:
2927 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002928 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002929 static const int32_t DEVICE_ID;
2930 static const int32_t DEVICE_GENERATION;
2931 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002932 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002933 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002934 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002935
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002936 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002937 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002938 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002939 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002940 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002941
Chris Yea52ade12020-08-27 16:49:20 -07002942 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002943 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002944 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002945 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002946 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002947 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002948 InputDeviceIdentifier identifier;
2949 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002950 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002951 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08002952 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002953 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002954 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002955 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002956 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957 }
2958
Chris Yea52ade12020-08-27 16:49:20 -07002959 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002960 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002961 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962 }
2963};
2964
2965const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002966const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002967const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002968const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2969const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002970const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002971 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002972const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002973const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974
2975TEST_F(InputDeviceTest, ImmutableProperties) {
2976 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002977 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002978 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979}
2980
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002981TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2982 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2983
2984 // Configuration
2985 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2986 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002987 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002988
2989 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2990}
2991
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002992TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2993 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002994}
2995
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2997 // Configuration.
2998 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002999 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000
3001 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003002 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003003
3004 NotifyDeviceResetArgs resetArgs;
3005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3006 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3007 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3008
3009 // Metadata.
3010 ASSERT_TRUE(mDevice->isIgnored());
3011 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
3012
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003013 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003015 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
3017 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
3018
3019 // State queries.
3020 ASSERT_EQ(0, mDevice->getMetaState());
3021
3022 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3023 << "Ignored device should return unknown key code state.";
3024 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
3025 << "Ignored device should return unknown scan code state.";
3026 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
3027 << "Ignored device should return unknown switch state.";
3028
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003029 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003030 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003031 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003032 << "Ignored device should never mark any key codes.";
3033 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
3034 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
3035}
3036
3037TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
3038 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003039 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003041 FakeInputMapper& mapper1 =
3042 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003043 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3044 mapper1.setMetaState(AMETA_ALT_ON);
3045 mapper1.addSupportedKeyCode(AKEYCODE_A);
3046 mapper1.addSupportedKeyCode(AKEYCODE_B);
3047 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
3048 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
3049 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
3050 mapper1.setScanCodeState(3, AKEY_STATE_UP);
3051 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003053 FakeInputMapper& mapper2 =
3054 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003055 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003056
3057 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003058 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003060 std::string propertyValue;
3061 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003063 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003064
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003065 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
3066 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067
3068 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003069 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003070 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
3071 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003072
3073 NotifyDeviceResetArgs resetArgs;
3074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3075 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3076 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3077
3078 // Metadata.
3079 ASSERT_FALSE(mDevice->isIgnored());
3080 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
3081
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003082 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003084 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
3086 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
3087
3088 // State queries.
3089 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
3090 << "Should query mappers and combine meta states.";
3091
3092 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3093 << "Should return unknown key code state when source not supported.";
3094 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3095 << "Should return unknown scan code state when source not supported.";
3096 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3097 << "Should return unknown switch state when source not supported.";
3098
3099 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
3100 << "Should query mapper when source is supported.";
3101 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
3102 << "Should query mapper when source is supported.";
3103 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
3104 << "Should query mapper when source is supported.";
3105
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003106 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003108 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109 << "Should do nothing when source is unsupported.";
3110 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
3111 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3112 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
3113 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3114
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003115 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003116 << "Should query mapper when source is supported.";
3117 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3118 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
3119 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3120 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3121
3122 // Event handling.
3123 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003124 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003125 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003127 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3128 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129}
3130
Arthur Hung2c9a3342019-07-23 14:18:59 +08003131// A single input device is associated with a specific display. Check that:
3132// 1. Device is disabled if the viewport corresponding to the associated display is not found
3133// 2. Device is disabled when setEnabled API is called
3134TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003135 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003136
3137 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003138 std::list<NotifyArgs> unused =
3139 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003140
3141 // Device should be enabled by default.
3142 ASSERT_TRUE(mDevice->isEnabled());
3143
3144 // Prepare associated info.
3145 constexpr uint8_t hdmi = 1;
3146 const std::string UNIQUE_ID = "local:1";
3147
3148 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003149 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3150 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003151 // Device should be disabled because it is associated with a specific display via
3152 // input port <-> display port association, but the corresponding display is not found
3153 ASSERT_FALSE(mDevice->isEnabled());
3154
3155 // Prepare displays.
3156 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003157 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3158 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003159 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3160 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003161 ASSERT_TRUE(mDevice->isEnabled());
3162
3163 // Device should be disabled after set disable.
3164 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003165 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3166 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003167 ASSERT_FALSE(mDevice->isEnabled());
3168
3169 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003170 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3171 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003172 ASSERT_FALSE(mDevice->isEnabled());
3173}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003174
Christine Franks1ba71cc2021-04-07 14:37:42 -07003175TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3176 // Device should be enabled by default.
3177 mFakePolicy->clearViewports();
3178 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003179 std::list<NotifyArgs> unused =
3180 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003181 ASSERT_TRUE(mDevice->isEnabled());
3182
3183 // Device should be disabled because it is associated with a specific display, but the
3184 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003185 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003186 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3187 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003188 ASSERT_FALSE(mDevice->isEnabled());
3189
3190 // Device should be enabled when a display is found.
3191 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3192 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3193 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003194 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3195 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003196 ASSERT_TRUE(mDevice->isEnabled());
3197
3198 // Device should be disabled after set disable.
3199 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003200 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3201 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003202 ASSERT_FALSE(mDevice->isEnabled());
3203
3204 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003205 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3206 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003207 ASSERT_FALSE(mDevice->isEnabled());
3208}
3209
Christine Franks2a2293c2022-01-18 11:51:16 -08003210TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3211 mFakePolicy->clearViewports();
3212 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003213 std::list<NotifyArgs> unused =
3214 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003215
Christine Franks2a2293c2022-01-18 11:51:16 -08003216 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3217 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3218 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3219 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003220 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3221 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003222 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3223}
3224
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003225/**
3226 * This test reproduces a crash caused by a dangling reference that remains after device is added
3227 * and removed. The reference is accessed in InputDevice::dump(..);
3228 */
3229TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3230 constexpr int32_t TEST_EVENTHUB_ID = 10;
3231 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3232
3233 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3234 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3235 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3236 std::string dumpStr, eventHubDevStr;
3237 device.dump(dumpStr, eventHubDevStr);
3238}
3239
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003240TEST_F(InputDeviceTest, GetBluetoothAddress) {
3241 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3242 ASSERT_TRUE(address);
3243 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3244}
3245
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246// --- InputMapperTest ---
3247
3248class InputMapperTest : public testing::Test {
3249protected:
3250 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003251 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252 static const int32_t DEVICE_ID;
3253 static const int32_t DEVICE_GENERATION;
3254 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003255 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003256 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003258 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003259 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003260 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003261 std::unique_ptr<InstrumentedInputReader> mReader;
3262 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003264 virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003265 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003266 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003267 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003268 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003269 *mFakeListener);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003270 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes, bus);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003271 // Consume the device reset notification generated when adding a new device.
3272 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 }
3274
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003275 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003276 SetUp(DEVICE_CLASSES);
3277 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003278
Chris Yea52ade12020-08-27 16:49:20 -07003279 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003280 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282 }
3283
3284 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003285 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003286 }
3287
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003288 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003289 if (!changes ||
3290 (changes &
3291 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3292 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003293 mReader->requestRefreshConfiguration(changes);
3294 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003295 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003296 std::list<NotifyArgs> out =
3297 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003298 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003299 for (const NotifyArgs& args : out) {
3300 mFakeListener->notify(args);
3301 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003302 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003303 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003304 }
3305
arthurhungdcef2dc2020-08-11 14:47:50 +08003306 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3307 const std::string& location, int32_t eventHubId,
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003308 ftl::Flags<InputDeviceClass> classes, int bus = 0) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003309 InputDeviceIdentifier identifier;
3310 identifier.name = name;
3311 identifier.location = location;
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003312 identifier.bus = bus;
arthurhungdcef2dc2020-08-11 14:47:50 +08003313 std::shared_ptr<InputDevice> device =
3314 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3315 identifier);
3316 mReader->pushNextDevice(device);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003317 mFakeEventHub->addDevice(eventHubId, name, classes, bus);
arthurhungdcef2dc2020-08-11 14:47:50 +08003318 mReader->loopOnce();
3319 return device;
3320 }
3321
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003322 template <class T, typename... Args>
3323 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003324 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003325 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003326 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3327 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003328 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003329 for (const NotifyArgs& loopArgs : resetArgList) {
3330 mFakeListener->notify(loopArgs);
3331 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003332 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003333 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334 }
3335
3336 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003337 int32_t orientation, const std::string& uniqueId,
3338 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003339 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3340 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003341 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3342 }
3343
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003344 void clearViewports() {
3345 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346 }
3347
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003348 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3349 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350 RawEvent event;
3351 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003352 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003353 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354 event.type = type;
3355 event.code = code;
3356 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003357 std::list<NotifyArgs> processArgList = mapper.process(&event);
3358 for (const NotifyArgs& args : processArgList) {
3359 mFakeListener->notify(args);
3360 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003361 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003362 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003363 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364 }
3365
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003366 void resetMapper(InputMapper& mapper, nsecs_t when) {
3367 const auto resetArgs = mapper.reset(when);
3368 for (const auto args : resetArgs) {
3369 mFakeListener->notify(args);
3370 }
3371 // Loop the reader to flush the input listener queue.
3372 mReader->loopOnce();
3373 }
3374
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375 static void assertMotionRange(const InputDeviceInfo& info,
3376 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3377 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003378 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3380 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3381 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3382 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3383 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3384 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3385 }
3386
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003387 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3388 float size, float touchMajor, float touchMinor, float toolMajor,
3389 float toolMinor, float orientation, float distance,
3390 float scaledAxisEpsilon = 1.f) {
3391 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3392 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3394 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003395 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3396 scaledAxisEpsilon);
3397 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3398 scaledAxisEpsilon);
3399 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3400 scaledAxisEpsilon);
3401 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3402 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003403 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3404 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3405 }
3406
Michael Wright17db18e2020-06-26 20:51:44 +01003407 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003409 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 ASSERT_NEAR(x, actualX, 1);
3411 ASSERT_NEAR(y, actualY, 1);
3412 }
3413};
3414
3415const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003416const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003417const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003418const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3419const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003420const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3421 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003422const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003423
3424// --- SwitchInputMapperTest ---
3425
3426class SwitchInputMapperTest : public InputMapperTest {
3427protected:
3428};
3429
3430TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003431 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003432
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003433 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434}
3435
3436TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003437 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003438
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003439 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003440 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003442 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003443 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444}
3445
3446TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003447 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003448 std::list<NotifyArgs> out;
3449 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3450 ASSERT_TRUE(out.empty());
3451 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3452 ASSERT_TRUE(out.empty());
3453 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3454 ASSERT_TRUE(out.empty());
3455 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003457 ASSERT_EQ(1u, out.size());
3458 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003460 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3461 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462 args.switchMask);
3463 ASSERT_EQ(uint32_t(0), args.policyFlags);
3464}
3465
Chris Ye87143712020-11-10 05:05:58 +00003466// --- VibratorInputMapperTest ---
3467class VibratorInputMapperTest : public InputMapperTest {
3468protected:
3469 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3470};
3471
3472TEST_F(VibratorInputMapperTest, GetSources) {
3473 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3474
3475 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3476}
3477
3478TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3479 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3480
3481 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3482}
3483
3484TEST_F(VibratorInputMapperTest, Vibrate) {
3485 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003486 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003487 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3488
3489 VibrationElement pattern(2);
3490 VibrationSequence sequence(2);
3491 pattern.duration = std::chrono::milliseconds(200);
3492 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3493 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3494 sequence.addElement(pattern);
3495 pattern.duration = std::chrono::milliseconds(500);
3496 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3497 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3498 sequence.addElement(pattern);
3499
3500 std::vector<int64_t> timings = {0, 1};
3501 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3502
3503 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003504 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003505 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003506 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003507 // Verify vibrator state listener was notified.
3508 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003509 ASSERT_EQ(1u, out.size());
3510 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3511 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3512 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003513 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003514 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003515 ASSERT_FALSE(mapper.isVibrating());
3516 // Verify vibrator state listener was notified.
3517 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003518 ASSERT_EQ(1u, out.size());
3519 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3520 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3521 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003522}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523
Chris Yef59a2f42020-10-16 12:55:26 -07003524// --- SensorInputMapperTest ---
3525
3526class SensorInputMapperTest : public InputMapperTest {
3527protected:
3528 static const int32_t ACCEL_RAW_MIN;
3529 static const int32_t ACCEL_RAW_MAX;
3530 static const int32_t ACCEL_RAW_FUZZ;
3531 static const int32_t ACCEL_RAW_FLAT;
3532 static const int32_t ACCEL_RAW_RESOLUTION;
3533
3534 static const int32_t GYRO_RAW_MIN;
3535 static const int32_t GYRO_RAW_MAX;
3536 static const int32_t GYRO_RAW_FUZZ;
3537 static const int32_t GYRO_RAW_FLAT;
3538 static const int32_t GYRO_RAW_RESOLUTION;
3539
3540 static const float GRAVITY_MS2_UNIT;
3541 static const float DEGREE_RADIAN_UNIT;
3542
3543 void prepareAccelAxes();
3544 void prepareGyroAxes();
3545 void setAccelProperties();
3546 void setGyroProperties();
3547 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3548};
3549
3550const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3551const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3552const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3553const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3554const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3555
3556const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3557const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3558const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3559const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3560const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3561
3562const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3563const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3564
3565void SensorInputMapperTest::prepareAccelAxes() {
3566 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3567 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3568 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3569 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3570 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3571 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3572}
3573
3574void SensorInputMapperTest::prepareGyroAxes() {
3575 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3576 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3577 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3578 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3579 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3580 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3581}
3582
3583void SensorInputMapperTest::setAccelProperties() {
3584 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3585 /* sensorDataIndex */ 0);
3586 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3587 /* sensorDataIndex */ 1);
3588 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3589 /* sensorDataIndex */ 2);
3590 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3591 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3592 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3593 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3594 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3595}
3596
3597void SensorInputMapperTest::setGyroProperties() {
3598 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3599 /* sensorDataIndex */ 0);
3600 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3601 /* sensorDataIndex */ 1);
3602 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3603 /* sensorDataIndex */ 2);
3604 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3605 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3606 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3607 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3608 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3609}
3610
3611TEST_F(SensorInputMapperTest, GetSources) {
3612 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3613
3614 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3615}
3616
3617TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3618 setAccelProperties();
3619 prepareAccelAxes();
3620 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3621
3622 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3623 std::chrono::microseconds(10000),
3624 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003625 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003631
3632 NotifySensorArgs args;
3633 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3634 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3635 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3636
3637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3638 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3639 ASSERT_EQ(args.deviceId, DEVICE_ID);
3640 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3641 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3642 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3643 ASSERT_EQ(args.values, values);
3644 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3645}
3646
3647TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3648 setGyroProperties();
3649 prepareGyroAxes();
3650 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3651
3652 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3653 std::chrono::microseconds(10000),
3654 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003655 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003661
3662 NotifySensorArgs args;
3663 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3664 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3665 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3666
3667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3668 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3669 ASSERT_EQ(args.deviceId, DEVICE_ID);
3670 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3671 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3672 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3673 ASSERT_EQ(args.values, values);
3674 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3675}
3676
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677// --- KeyboardInputMapperTest ---
3678
3679class KeyboardInputMapperTest : public InputMapperTest {
3680protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003681 const std::string UNIQUE_ID = "local:0";
3682
3683 void prepareDisplay(int32_t orientation);
3684
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003685 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003686 int32_t originalKeyCode, int32_t rotatedKeyCode,
3687 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688};
3689
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003690/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3691 * orientation.
3692 */
3693void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003694 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3695 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003696}
3697
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003698void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003699 int32_t originalScanCode, int32_t originalKeyCode,
3700 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701 NotifyKeyArgs args;
3702
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3705 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3706 ASSERT_EQ(originalScanCode, args.scanCode);
3707 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003708 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3712 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3713 ASSERT_EQ(originalScanCode, args.scanCode);
3714 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003715 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716}
3717
Michael Wrightd02c5b62014-02-10 15:10:22 -08003718TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003719 KeyboardInputMapper& mapper =
3720 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3721 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003723 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724}
3725
3726TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3727 const int32_t USAGE_A = 0x070004;
3728 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003729 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3730 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003731 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3732 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3733 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003734
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003735 KeyboardInputMapper& mapper =
3736 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3737 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003738 // Initial metastate is AMETA_NONE.
3739 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740
3741 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743 NotifyKeyArgs args;
3744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3745 ASSERT_EQ(DEVICE_ID, args.deviceId);
3746 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3747 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3748 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3749 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3750 ASSERT_EQ(KEY_HOME, args.scanCode);
3751 ASSERT_EQ(AMETA_NONE, args.metaState);
3752 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3753 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3754 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3755
3756 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003757 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3759 ASSERT_EQ(DEVICE_ID, args.deviceId);
3760 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3761 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3762 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3763 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3764 ASSERT_EQ(KEY_HOME, args.scanCode);
3765 ASSERT_EQ(AMETA_NONE, args.metaState);
3766 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3767 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3768 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3769
3770 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003771 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3772 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3774 ASSERT_EQ(DEVICE_ID, args.deviceId);
3775 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3776 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3777 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3778 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3779 ASSERT_EQ(0, args.scanCode);
3780 ASSERT_EQ(AMETA_NONE, args.metaState);
3781 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3782 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3783 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3784
3785 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3787 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3789 ASSERT_EQ(DEVICE_ID, args.deviceId);
3790 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3791 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3792 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3793 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3794 ASSERT_EQ(0, args.scanCode);
3795 ASSERT_EQ(AMETA_NONE, args.metaState);
3796 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3797 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3798 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3799
3800 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3804 ASSERT_EQ(DEVICE_ID, args.deviceId);
3805 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3806 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3807 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3808 ASSERT_EQ(0, args.keyCode);
3809 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3810 ASSERT_EQ(AMETA_NONE, args.metaState);
3811 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3812 ASSERT_EQ(0U, args.policyFlags);
3813 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3814
3815 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3817 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3819 ASSERT_EQ(DEVICE_ID, args.deviceId);
3820 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3821 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3822 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3823 ASSERT_EQ(0, args.keyCode);
3824 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3825 ASSERT_EQ(AMETA_NONE, args.metaState);
3826 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3827 ASSERT_EQ(0U, args.policyFlags);
3828 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3829}
3830
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003831/**
3832 * Ensure that the readTime is set to the time when the EV_KEY is received.
3833 */
3834TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3835 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3836
3837 KeyboardInputMapper& mapper =
3838 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3839 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3840 NotifyKeyArgs args;
3841
3842 // Key down
3843 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3845 ASSERT_EQ(12, args.readTime);
3846
3847 // Key up
3848 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3850 ASSERT_EQ(15, args.readTime);
3851}
3852
Michael Wrightd02c5b62014-02-10 15:10:22 -08003853TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003854 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3855 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003856 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3857 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3858 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003860 KeyboardInputMapper& mapper =
3861 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3862 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003863
Arthur Hung95f68612022-04-07 14:08:22 +08003864 // Initial metastate is AMETA_NONE.
3865 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866
3867 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003869 NotifyKeyArgs args;
3870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3871 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003872 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003873 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874
3875 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003876 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3878 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003879 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003880
3881 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003882 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3884 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886
3887 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003888 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3890 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003891 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003892 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003893}
3894
3895TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003896 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3897 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3898 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3899 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003901 KeyboardInputMapper& mapper =
3902 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3903 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003905 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3907 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3908 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3909 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3910 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3911 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3912 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3913 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3914}
3915
3916TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003917 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3918 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3919 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3920 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003923 KeyboardInputMapper& mapper =
3924 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3925 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003926
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003927 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003928 ASSERT_NO_FATAL_FAILURE(
3929 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3930 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3931 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3932 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3933 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3934 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3935 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003937 clearViewports();
3938 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003939 ASSERT_NO_FATAL_FAILURE(
3940 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3941 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3942 AKEYCODE_DPAD_UP, DISPLAY_ID));
3943 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3944 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3945 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3946 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003947
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003948 clearViewports();
3949 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003950 ASSERT_NO_FATAL_FAILURE(
3951 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3952 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3953 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3954 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3955 AKEYCODE_DPAD_UP, DISPLAY_ID));
3956 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3957 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003959 clearViewports();
3960 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003961 ASSERT_NO_FATAL_FAILURE(
3962 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3963 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3964 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3965 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3966 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3967 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3968 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969
3970 // Special case: if orientation changes while key is down, we still emit the same keycode
3971 // in the key up as we did in the key down.
3972 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003973 clearViewports();
3974 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3977 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3978 ASSERT_EQ(KEY_UP, args.scanCode);
3979 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3980
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003981 clearViewports();
3982 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3985 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3986 ASSERT_EQ(KEY_UP, args.scanCode);
3987 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3988}
3989
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003990TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3991 // If the keyboard is not orientation aware,
3992 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003993 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003994
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003995 KeyboardInputMapper& mapper =
3996 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3997 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003998 NotifyKeyArgs args;
3999
4000 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4005 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4006
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004007 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4012 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4013}
4014
4015TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
4016 // If the keyboard is orientation aware,
4017 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004018 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004019
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004020 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004021 KeyboardInputMapper& mapper =
4022 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4023 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004024 NotifyKeyArgs args;
4025
4026 // Display id should be ADISPLAY_ID_NONE without any display configuration.
4027 // ^--- already checked by the previous test
4028
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004029 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004030 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004033 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4035 ASSERT_EQ(DISPLAY_ID, args.displayId);
4036
4037 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004038 clearViewports();
4039 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004040 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4045 ASSERT_EQ(newDisplayId, args.displayId);
4046}
4047
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004049 KeyboardInputMapper& mapper =
4050 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4051 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004053 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004054 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004055
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004056 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004057 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058}
4059
Philip Junker4af3b3d2021-12-14 10:36:55 +01004060TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
4061 KeyboardInputMapper& mapper =
4062 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4063 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4064
4065 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
4066 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
4067 << "If a mapping is available, the result is equal to the mapping";
4068
4069 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
4070 << "If no mapping is available, the result is the key location";
4071}
4072
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004074 KeyboardInputMapper& mapper =
4075 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4076 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004077
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004078 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004079 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004081 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004082 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004083}
4084
4085TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004086 KeyboardInputMapper& mapper =
4087 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4088 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004090 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004093 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004094 ASSERT_TRUE(flags[0]);
4095 ASSERT_FALSE(flags[1]);
4096}
4097
4098TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004099 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4100 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4101 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4102 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4103 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4104 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004106 KeyboardInputMapper& mapper =
4107 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4108 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004109 // Initial metastate is AMETA_NONE.
4110 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111
4112 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004113 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4114 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4115 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116
4117 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004120 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4121 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4122 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004123 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124
4125 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004128 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4129 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4130 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004131 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132
4133 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004136 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4137 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4138 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004139 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140
4141 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004144 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4145 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4146 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004147 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004148
4149 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004152 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4153 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4154 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004155 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156
4157 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004160 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4161 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4162 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004163 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004164}
4165
Chris Yea52ade12020-08-27 16:49:20 -07004166TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4167 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4168 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4169 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4170 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4171
4172 KeyboardInputMapper& mapper =
4173 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4174 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4175
Chris Yea52ade12020-08-27 16:49:20 -07004176 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004177 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004178 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4179 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4180 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4181 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4182
4183 NotifyKeyArgs args;
4184 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4187 ASSERT_EQ(AMETA_NONE, args.metaState);
4188 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4189 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4190 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4191
4192 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004193 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4195 ASSERT_EQ(AMETA_NONE, args.metaState);
4196 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4197 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4198 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4199}
4200
Arthur Hung2c9a3342019-07-23 14:18:59 +08004201TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4202 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004203 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4204 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4205 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4206 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004207
4208 // keyboard 2.
4209 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004210 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004211 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004212 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004213 std::shared_ptr<InputDevice> device2 =
4214 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004215 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004216
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004217 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4218 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4219 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4220 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004222 KeyboardInputMapper& mapper =
4223 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4224 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004226 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004227 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004228 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004229 std::list<NotifyArgs> unused =
4230 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4231 0 /*changes*/);
4232 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004233
4234 // Prepared displays and associated info.
4235 constexpr uint8_t hdmi1 = 0;
4236 constexpr uint8_t hdmi2 = 1;
4237 const std::string SECONDARY_UNIQUE_ID = "local:1";
4238
4239 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4240 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4241
4242 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004243 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4244 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004245 ASSERT_FALSE(device2->isEnabled());
4246
4247 // Prepare second display.
4248 constexpr int32_t newDisplayId = 2;
4249 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004250 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004251 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004252 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004253 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004254 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4255 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004256
4257 // Device should be enabled after the associated display is found.
4258 ASSERT_TRUE(mDevice->isEnabled());
4259 ASSERT_TRUE(device2->isEnabled());
4260
4261 // Test pad key events
4262 ASSERT_NO_FATAL_FAILURE(
4263 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4264 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4265 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4266 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4267 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4268 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4269 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4270
4271 ASSERT_NO_FATAL_FAILURE(
4272 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4273 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4274 AKEYCODE_DPAD_RIGHT, newDisplayId));
4275 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4276 AKEYCODE_DPAD_DOWN, newDisplayId));
4277 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4278 AKEYCODE_DPAD_LEFT, newDisplayId));
4279}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
arthurhungc903df12020-08-11 15:08:42 +08004281TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4282 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4283 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4284 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4285 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4287 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4288
4289 KeyboardInputMapper& mapper =
4290 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4291 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004292 // Initial metastate is AMETA_NONE.
4293 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004294
4295 // Initialization should have turned all of the lights off.
4296 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4297 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4298 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4299
4300 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004303 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4304 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4305
4306 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004309 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4310 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4311
4312 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004315 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4316 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4317
4318 mFakeEventHub->removeDevice(EVENTHUB_ID);
4319 mReader->loopOnce();
4320
4321 // keyboard 2 should default toggle keys.
4322 const std::string USB2 = "USB2";
4323 const std::string DEVICE_NAME2 = "KEYBOARD2";
4324 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4325 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4326 std::shared_ptr<InputDevice> device2 =
4327 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004328 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004329 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4330 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4331 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4332 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4333 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4334 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4335
arthurhung6fe95782020-10-05 22:41:16 +08004336 KeyboardInputMapper& mapper2 =
4337 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4338 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004339 std::list<NotifyArgs> unused =
4340 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4341 0 /*changes*/);
4342 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004343
4344 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4345 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4346 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004347 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4348 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004349}
4350
Arthur Hungcb40a002021-08-03 14:31:01 +00004351TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4352 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4353 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4354 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4355
4356 // Suppose we have two mappers. (DPAD + KEYBOARD)
4357 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4358 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4359 KeyboardInputMapper& mapper =
4360 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4361 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004362 // Initial metastate is AMETA_NONE.
4363 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004364
4365 mReader->toggleCapsLockState(DEVICE_ID);
4366 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4367}
4368
Arthur Hungfb3cc112022-04-13 07:39:50 +00004369TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4370 // keyboard 1.
4371 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4372 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4373 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4374 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4375 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4376 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4377
4378 KeyboardInputMapper& mapper1 =
4379 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4380 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4381
4382 // keyboard 2.
4383 const std::string USB2 = "USB2";
4384 const std::string DEVICE_NAME2 = "KEYBOARD2";
4385 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4386 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4387 std::shared_ptr<InputDevice> device2 =
4388 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4389 ftl::Flags<InputDeviceClass>(0));
4390 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4391 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4392 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4393 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4394 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4395 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4396
4397 KeyboardInputMapper& mapper2 =
4398 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4399 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004400 std::list<NotifyArgs> unused =
4401 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4402 0 /*changes*/);
4403 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004404
Arthur Hung95f68612022-04-07 14:08:22 +08004405 // Initial metastate is AMETA_NONE.
4406 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4407 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4408
4409 // Toggle num lock on and off.
4410 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4411 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004412 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4413 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4414 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4415
4416 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4417 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4418 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4419 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4420 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4421
4422 // Toggle caps lock on and off.
4423 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4424 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4425 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4426 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4427 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4428
4429 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4430 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4431 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4432 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4433 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4434
4435 // Toggle scroll lock on and off.
4436 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4437 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4438 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4439 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4440 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4441
4442 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4443 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4444 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4445 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4446 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4447}
4448
Arthur Hung2141d542022-08-23 07:45:21 +00004449TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4450 const int32_t USAGE_A = 0x070004;
4451 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4452 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4453
4454 KeyboardInputMapper& mapper =
4455 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4456 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4457 // Key down by scan code.
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4459 NotifyKeyArgs args;
4460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4461 ASSERT_EQ(DEVICE_ID, args.deviceId);
4462 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4463 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4464 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4465 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4466 ASSERT_EQ(KEY_HOME, args.scanCode);
4467 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4468
4469 // Disable device, it should synthesize cancellation events for down events.
4470 mFakePolicy->addDisabledDevice(DEVICE_ID);
4471 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4472
4473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4474 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4475 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4476 ASSERT_EQ(KEY_HOME, args.scanCode);
4477 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4478}
4479
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004480// --- KeyboardInputMapperTest_ExternalDevice ---
4481
4482class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4483protected:
Chris Yea52ade12020-08-27 16:49:20 -07004484 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004485};
4486
4487TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004488 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4489 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004490
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004491 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4492 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4494 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004495
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004496 KeyboardInputMapper& mapper =
4497 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4498 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004499
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004501 NotifyKeyArgs args;
4502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4503 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4504
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004505 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4507 ASSERT_EQ(uint32_t(0), args.policyFlags);
4508
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4511 ASSERT_EQ(uint32_t(0), args.policyFlags);
4512
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004513 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4515 ASSERT_EQ(uint32_t(0), args.policyFlags);
4516
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4519 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4520
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004521 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4523 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4524}
4525
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004526TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004527 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004528
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004529 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4530 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4531 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004532
Powei Fengd041c5d2019-05-03 17:11:33 -07004533 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004534 KeyboardInputMapper& mapper =
4535 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4536 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004537
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004539 NotifyKeyArgs args;
4540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4541 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4542
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004543 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4545 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4546
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4549 ASSERT_EQ(uint32_t(0), args.policyFlags);
4550
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004551 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4553 ASSERT_EQ(uint32_t(0), args.policyFlags);
4554
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4557 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4558
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004559 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4561 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4562}
4563
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564// --- CursorInputMapperTest ---
4565
4566class CursorInputMapperTest : public InputMapperTest {
4567protected:
4568 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4569
Michael Wright17db18e2020-06-26 20:51:44 +01004570 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571
Chris Yea52ade12020-08-27 16:49:20 -07004572 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 InputMapperTest::SetUp();
4574
Michael Wright17db18e2020-06-26 20:51:44 +01004575 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004576 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577 }
4578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004579 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4580 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004581
4582 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004583 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4584 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4585 }
4586
4587 void prepareSecondaryDisplay() {
4588 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4589 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4590 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004591 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004592
4593 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4594 float pressure) {
4595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4596 0.0f, 0.0f, 0.0f, EPSILON));
4597 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598};
4599
4600const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4601
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004602void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4603 int32_t originalY, int32_t rotatedX,
4604 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 NotifyMotionArgs args;
4606
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4611 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004612 ASSERT_NO_FATAL_FAILURE(
4613 assertCursorPointerCoords(args.pointerCoords[0],
4614 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4615 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616}
4617
4618TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004620 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004622 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623}
4624
4625TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004627 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004629 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630}
4631
4632TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004634 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635
4636 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004637 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638
4639 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004640 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4641 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4643 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4644
4645 // When the bounds are set, then there should be a valid motion range.
4646 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4647
4648 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004649 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650
4651 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4652 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4653 1, 800 - 1, 0.0f, 0.0f));
4654 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4655 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4656 2, 480 - 1, 0.0f, 0.0f));
4657 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4658 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4659 0.0f, 1.0f, 0.0f, 0.0f));
4660}
4661
4662TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004663 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004664 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665
4666 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004667 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668
4669 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4670 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4671 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4672 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4673 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4674 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4675 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4676 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4677 0.0f, 1.0f, 0.0f, 0.0f));
4678}
4679
4680TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004682 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683
arthurhungdcef2dc2020-08-11 14:47:50 +08004684 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685
4686 NotifyMotionArgs args;
4687
4688 // Button press.
4689 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4693 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4694 ASSERT_EQ(DEVICE_ID, args.deviceId);
4695 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4696 ASSERT_EQ(uint32_t(0), args.policyFlags);
4697 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4698 ASSERT_EQ(0, args.flags);
4699 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4700 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4701 ASSERT_EQ(0, args.edgeFlags);
4702 ASSERT_EQ(uint32_t(1), args.pointerCount);
4703 ASSERT_EQ(0, args.pointerProperties[0].id);
4704 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004705 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4707 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4708 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4709
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4711 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4712 ASSERT_EQ(DEVICE_ID, args.deviceId);
4713 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4714 ASSERT_EQ(uint32_t(0), args.policyFlags);
4715 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4716 ASSERT_EQ(0, args.flags);
4717 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4718 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4719 ASSERT_EQ(0, args.edgeFlags);
4720 ASSERT_EQ(uint32_t(1), args.pointerCount);
4721 ASSERT_EQ(0, args.pointerProperties[0].id);
4722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004723 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004724 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4725 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4726 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4727
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004729 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4730 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4732 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4733 ASSERT_EQ(DEVICE_ID, args.deviceId);
4734 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4735 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004736 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4737 ASSERT_EQ(0, args.flags);
4738 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4739 ASSERT_EQ(0, args.buttonState);
4740 ASSERT_EQ(0, args.edgeFlags);
4741 ASSERT_EQ(uint32_t(1), args.pointerCount);
4742 ASSERT_EQ(0, args.pointerProperties[0].id);
4743 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004744 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004745 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4746 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4747 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4748
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4750 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4751 ASSERT_EQ(DEVICE_ID, args.deviceId);
4752 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4753 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4755 ASSERT_EQ(0, args.flags);
4756 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4757 ASSERT_EQ(0, args.buttonState);
4758 ASSERT_EQ(0, args.edgeFlags);
4759 ASSERT_EQ(uint32_t(1), args.pointerCount);
4760 ASSERT_EQ(0, args.pointerProperties[0].id);
4761 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004762 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4764 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4765 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4766}
4767
4768TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004770 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771
4772 NotifyMotionArgs args;
4773
4774 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004779 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4780 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4781 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782
4783 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004788 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4789 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004790}
4791
4792TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004793 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004794 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004795
4796 NotifyMotionArgs args;
4797
4798 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4802 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004803 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4806 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004807 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004808
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004813 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004814 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004815
4816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004818 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819}
4820
4821TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004823 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824
4825 NotifyMotionArgs args;
4826
4827 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4833 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004834 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4835 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4836 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4839 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004840 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4841 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4842 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004843
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4846 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004850 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4851 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4852 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853
4854 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004859 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004860
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004862 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004863 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004864}
4865
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004866TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004867 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004868 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004869 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4870 // need to be rotated.
4871 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004872 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004874 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4876 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4877 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4878 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4879 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4880 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4881 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4882 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4883}
4884
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004885TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004886 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004887 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004888 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4889 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004890 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004891
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004892 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004893 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4895 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4896 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4897 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4898 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4899 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4900 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4901 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4902
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004903 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004904 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004905 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4906 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4907 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4908 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4909 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4910 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4911 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4912 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004913
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004914 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004915 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4917 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4918 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4919 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4920 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4921 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4922 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4923 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4924
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004925 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004926 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004927 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4928 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4929 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4930 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4931 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4932 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4933 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4934 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935}
4936
4937TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004939 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940
4941 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4942 mFakePointerController->setPosition(100, 200);
4943 mFakePointerController->setButtonState(0);
4944
4945 NotifyMotionArgs motionArgs;
4946 NotifyKeyArgs keyArgs;
4947
4948 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4952 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4953 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4954 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004955 ASSERT_NO_FATAL_FAILURE(
4956 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004957
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4960 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4961 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004962 ASSERT_NO_FATAL_FAILURE(
4963 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004964
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004965 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004968 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969 ASSERT_EQ(0, motionArgs.buttonState);
4970 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004971 ASSERT_NO_FATAL_FAILURE(
4972 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973
4974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004975 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 ASSERT_EQ(0, motionArgs.buttonState);
4977 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004978 ASSERT_NO_FATAL_FAILURE(
4979 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004980
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004983 ASSERT_EQ(0, motionArgs.buttonState);
4984 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004985 ASSERT_NO_FATAL_FAILURE(
4986 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987
4988 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004989 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4993 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4994 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4995 motionArgs.buttonState);
4996 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4997 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004998 ASSERT_NO_FATAL_FAILURE(
4999 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5002 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5003 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5004 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5005 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005006 ASSERT_NO_FATAL_FAILURE(
5007 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005008
5009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5011 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5012 motionArgs.buttonState);
5013 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5014 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005015 ASSERT_NO_FATAL_FAILURE(
5016 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005017
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5023 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005024 ASSERT_NO_FATAL_FAILURE(
5025 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005026
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005029 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5030 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005031 ASSERT_NO_FATAL_FAILURE(
5032 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005037 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5038 ASSERT_EQ(0, motionArgs.buttonState);
5039 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005040 ASSERT_NO_FATAL_FAILURE(
5041 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5043 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005044
5045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046 ASSERT_EQ(0, motionArgs.buttonState);
5047 ASSERT_EQ(0, mFakePointerController->getButtonState());
5048 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005049 ASSERT_NO_FATAL_FAILURE(
5050 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005051
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5053 ASSERT_EQ(0, motionArgs.buttonState);
5054 ASSERT_EQ(0, mFakePointerController->getButtonState());
5055 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005056 ASSERT_NO_FATAL_FAILURE(
5057 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058
5059 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5063 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5064 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005065
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005067 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5069 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005070 ASSERT_NO_FATAL_FAILURE(
5071 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005072
5073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5074 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5075 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5076 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005077 ASSERT_NO_FATAL_FAILURE(
5078 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005083 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005084 ASSERT_EQ(0, motionArgs.buttonState);
5085 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005086 ASSERT_NO_FATAL_FAILURE(
5087 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005088
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005090 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005091 ASSERT_EQ(0, motionArgs.buttonState);
5092 ASSERT_EQ(0, mFakePointerController->getButtonState());
5093
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005094 ASSERT_NO_FATAL_FAILURE(
5095 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5097 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5098 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5099
5100 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5104 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5105 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005106
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005108 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5110 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005111 ASSERT_NO_FATAL_FAILURE(
5112 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005113
5114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5116 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5117 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005118 ASSERT_NO_FATAL_FAILURE(
5119 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005120
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005121 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125 ASSERT_EQ(0, motionArgs.buttonState);
5126 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005127 ASSERT_NO_FATAL_FAILURE(
5128 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005129
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5131 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5132 ASSERT_EQ(0, motionArgs.buttonState);
5133 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005134 ASSERT_NO_FATAL_FAILURE(
5135 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005136
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5138 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5139 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5140
5141 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5146 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005147
Michael Wrightd02c5b62014-02-10 15:10:22 -08005148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005149 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005150 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5151 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005152 ASSERT_NO_FATAL_FAILURE(
5153 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005154
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5157 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5158 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005159 ASSERT_NO_FATAL_FAILURE(
5160 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005161
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005165 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166 ASSERT_EQ(0, motionArgs.buttonState);
5167 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005168 ASSERT_NO_FATAL_FAILURE(
5169 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005170
5171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5172 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5173 ASSERT_EQ(0, motionArgs.buttonState);
5174 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005175 ASSERT_NO_FATAL_FAILURE(
5176 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005177
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5179 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5180 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5181
5182 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5186 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5187 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005188
Michael Wrightd02c5b62014-02-10 15:10:22 -08005189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005190 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5192 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005193 ASSERT_NO_FATAL_FAILURE(
5194 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005195
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5198 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5199 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005200 ASSERT_NO_FATAL_FAILURE(
5201 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005202
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005207 ASSERT_EQ(0, motionArgs.buttonState);
5208 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005209 ASSERT_NO_FATAL_FAILURE(
5210 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005211
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5214 ASSERT_EQ(0, motionArgs.buttonState);
5215 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005216 ASSERT_NO_FATAL_FAILURE(
5217 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005218
Michael Wrightd02c5b62014-02-10 15:10:22 -08005219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5220 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5221 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5222}
5223
5224TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005225 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005226 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005227
5228 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5229 mFakePointerController->setPosition(100, 200);
5230 mFakePointerController->setButtonState(0);
5231
5232 NotifyMotionArgs args;
5233
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005238 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5239 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5240 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5241 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 +01005242 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005243}
5244
5245TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005246 addConfigurationProperty("cursor.mode", "pointer");
5247 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005248 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005249
5250 NotifyDeviceResetArgs resetArgs;
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5252 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5253 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5254
5255 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5256 mFakePointerController->setPosition(100, 200);
5257 mFakePointerController->setButtonState(0);
5258
5259 NotifyMotionArgs args;
5260
5261 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5266 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5269 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 +01005270 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005271
5272 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005273 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5276 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5279 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5281 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5282 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5284 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5285
5286 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005287 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5288 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5290 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5291 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5293 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5295 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5296 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5298 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5299
5300 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5305 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5308 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 +01005309 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005310
5311 // Disable pointer capture and check that the device generation got bumped
5312 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005313 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005314 mFakePolicy->setPointerCapture(false);
5315 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005316 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005317
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005319 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5320
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5325 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005326 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5328 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 +01005329 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330}
5331
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005332/**
5333 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5334 * pointer acceleration or speed processing should not be applied.
5335 */
5336TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5337 addConfigurationProperty("cursor.mode", "pointer");
5338 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5339 100.f /*high threshold*/, 10.f /*acceleration*/);
5340 mFakePolicy->setVelocityControlParams(testParams);
5341 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5342
5343 NotifyDeviceResetArgs resetArgs;
5344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5345 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5346 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5347
5348 NotifyMotionArgs args;
5349
5350 // Move and verify scale is applied.
5351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5352 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5355 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5356 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5357 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5358 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5359 ASSERT_GT(relX, 10);
5360 ASSERT_GT(relY, 20);
5361
5362 // Enable Pointer Capture
5363 mFakePolicy->setPointerCapture(true);
5364 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5365 NotifyPointerCaptureChangedArgs captureArgs;
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5367 ASSERT_TRUE(captureArgs.request.enable);
5368
5369 // Move and verify scale is not applied.
5370 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5374 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5376 ASSERT_EQ(10, args.pointerCoords[0].getX());
5377 ASSERT_EQ(20, args.pointerCoords[0].getY());
5378}
5379
Prabir Pradhan208360b2022-06-24 18:37:04 +00005380TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5381 addConfigurationProperty("cursor.mode", "pointer");
5382 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5383
5384 NotifyDeviceResetArgs resetArgs;
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5386 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5387 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5388
5389 // Ensure the display is rotated.
5390 prepareDisplay(DISPLAY_ORIENTATION_90);
5391
5392 NotifyMotionArgs args;
5393
5394 // Verify that the coordinates are rotated.
5395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5399 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5400 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5401 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5402 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5403
5404 // Enable Pointer Capture.
5405 mFakePolicy->setPointerCapture(true);
5406 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5407 NotifyPointerCaptureChangedArgs captureArgs;
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5409 ASSERT_TRUE(captureArgs.request.enable);
5410
5411 // Move and verify rotation is not applied.
5412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5416 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5418 ASSERT_EQ(10, args.pointerCoords[0].getX());
5419 ASSERT_EQ(20, args.pointerCoords[0].getY());
5420}
5421
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005422TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005423 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005424
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005425 // Set up the default display.
5426 prepareDisplay(DISPLAY_ORIENTATION_90);
5427
5428 // Set up the secondary display as the display on which the pointer should be shown.
5429 // The InputDevice is not associated with any display.
5430 prepareSecondaryDisplay();
5431 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005432 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5433
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005434 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005435 mFakePointerController->setPosition(100, 200);
5436 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005437
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005438 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005443 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5444 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5445 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005446 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005447}
5448
5449TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5450 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5451
5452 // Set up the default display.
5453 prepareDisplay(DISPLAY_ORIENTATION_90);
5454
5455 // Set up the secondary display as the display on which the pointer should be shown,
5456 // and associate the InputDevice with the secondary display.
5457 prepareSecondaryDisplay();
5458 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5459 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5460 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5461
5462 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5463 mFakePointerController->setPosition(100, 200);
5464 mFakePointerController->setButtonState(0);
5465
5466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005470 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5471 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5472 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005473 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5474}
5475
5476TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5477 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5478
5479 // Set up the default display as the display on which the pointer should be shown.
5480 prepareDisplay(DISPLAY_ORIENTATION_90);
5481 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5482
5483 // Associate the InputDevice with the secondary display.
5484 prepareSecondaryDisplay();
5485 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5486 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5487
5488 // The mapper should not generate any events because it is associated with a display that is
5489 // different from the pointer display.
5490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005494}
5495
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005496// --- BluetoothCursorInputMapperTest ---
5497
5498class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
5499protected:
5500 void SetUp() override {
5501 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
5502
5503 mFakePointerController = std::make_shared<FakePointerController>();
5504 mFakePolicy->setPointerController(mFakePointerController);
5505 }
5506};
5507
5508TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
5509 addConfigurationProperty("cursor.mode", "pointer");
5510 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5511
5512 nsecs_t kernelEventTime = ARBITRARY_TIME;
5513 nsecs_t expectedEventTime = ARBITRARY_TIME;
5514 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5515 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5517 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5518 WithEventTime(expectedEventTime))));
5519
5520 // Process several events that come in quick succession, according to their timestamps.
5521 for (int i = 0; i < 3; i++) {
5522 constexpr static nsecs_t delta = ms2ns(1);
5523 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5524 kernelEventTime += delta;
5525 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5526
5527 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5528 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5530 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5531 WithEventTime(expectedEventTime))));
5532 }
5533}
5534
5535TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
5536 addConfigurationProperty("cursor.mode", "pointer");
5537 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5538
5539 nsecs_t expectedEventTime = ARBITRARY_TIME;
5540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5543 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5544 WithEventTime(expectedEventTime))));
5545
5546 // Process several events with the same timestamp from the kernel.
5547 // Ensure that we do not generate events too far into the future.
5548 constexpr static int32_t numEvents =
5549 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5550 for (int i = 0; i < numEvents; i++) {
5551 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5552
5553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5556 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5557 WithEventTime(expectedEventTime))));
5558 }
5559
5560 // By processing more events with the same timestamp, we should not generate events with a
5561 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5562 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5563 for (int i = 0; i < 3; i++) {
5564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5567 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5568 WithEventTime(cappedEventTime))));
5569 }
5570}
5571
5572TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
5573 addConfigurationProperty("cursor.mode", "pointer");
5574 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5575
5576 nsecs_t kernelEventTime = ARBITRARY_TIME;
5577 nsecs_t expectedEventTime = ARBITRARY_TIME;
5578 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5579 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5581 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5582 WithEventTime(expectedEventTime))));
5583
5584 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5585 // smoothening is not needed, its timestamp is not affected.
5586 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5587 expectedEventTime = kernelEventTime;
5588
5589 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5590 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5592 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5593 WithEventTime(expectedEventTime))));
5594}
5595
Michael Wrightd02c5b62014-02-10 15:10:22 -08005596// --- TouchInputMapperTest ---
5597
5598class TouchInputMapperTest : public InputMapperTest {
5599protected:
5600 static const int32_t RAW_X_MIN;
5601 static const int32_t RAW_X_MAX;
5602 static const int32_t RAW_Y_MIN;
5603 static const int32_t RAW_Y_MAX;
5604 static const int32_t RAW_TOUCH_MIN;
5605 static const int32_t RAW_TOUCH_MAX;
5606 static const int32_t RAW_TOOL_MIN;
5607 static const int32_t RAW_TOOL_MAX;
5608 static const int32_t RAW_PRESSURE_MIN;
5609 static const int32_t RAW_PRESSURE_MAX;
5610 static const int32_t RAW_ORIENTATION_MIN;
5611 static const int32_t RAW_ORIENTATION_MAX;
5612 static const int32_t RAW_DISTANCE_MIN;
5613 static const int32_t RAW_DISTANCE_MAX;
5614 static const int32_t RAW_TILT_MIN;
5615 static const int32_t RAW_TILT_MAX;
5616 static const int32_t RAW_ID_MIN;
5617 static const int32_t RAW_ID_MAX;
5618 static const int32_t RAW_SLOT_MIN;
5619 static const int32_t RAW_SLOT_MAX;
5620 static const float X_PRECISION;
5621 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005622 static const float X_PRECISION_VIRTUAL;
5623 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624
5625 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005626 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005627
5628 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5629
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005630 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005631 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005632
Michael Wrightd02c5b62014-02-10 15:10:22 -08005633 enum Axes {
5634 POSITION = 1 << 0,
5635 TOUCH = 1 << 1,
5636 TOOL = 1 << 2,
5637 PRESSURE = 1 << 3,
5638 ORIENTATION = 1 << 4,
5639 MINOR = 1 << 5,
5640 ID = 1 << 6,
5641 DISTANCE = 1 << 7,
5642 TILT = 1 << 8,
5643 SLOT = 1 << 9,
5644 TOOL_TYPE = 1 << 10,
5645 };
5646
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005647 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5648 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005649 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005650 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005651 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652 int32_t toRawX(float displayX);
5653 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005654 int32_t toRotatedRawX(float displayX);
5655 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005656 float toCookedX(float rawX, float rawY);
5657 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005659 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005661 float toDisplayY(int32_t rawY, int32_t displayHeight);
5662
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663};
5664
5665const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5666const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5667const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5668const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5669const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5670const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5671const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5672const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005673const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5674const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5676const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5677const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5678const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5679const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5680const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5681const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5682const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5683const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5684const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5685const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5686const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005687const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5688 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5689const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5690 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005691const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5692 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693
5694const float TouchInputMapperTest::GEOMETRIC_SCALE =
5695 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5696 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5697
5698const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5699 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5700 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5701};
5702
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005703void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005704 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5705 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005706}
5707
5708void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5709 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5710 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005711}
5712
Santos Cordonfa5cf462017-04-05 10:37:00 -07005713void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005714 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5715 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5716 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005717}
5718
Michael Wrightd02c5b62014-02-10 15:10:22 -08005719void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005720 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5721 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5722 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5723 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724}
5725
Jason Gerecke489fda82012-09-07 17:19:40 -07005726void TouchInputMapperTest::prepareLocationCalibration() {
5727 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5728}
5729
Michael Wrightd02c5b62014-02-10 15:10:22 -08005730int32_t TouchInputMapperTest::toRawX(float displayX) {
5731 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5732}
5733
5734int32_t TouchInputMapperTest::toRawY(float displayY) {
5735 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5736}
5737
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005738int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5739 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5740}
5741
5742int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5743 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5744}
5745
Jason Gerecke489fda82012-09-07 17:19:40 -07005746float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5747 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5748 return rawX;
5749}
5750
5751float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5752 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5753 return rawY;
5754}
5755
Michael Wrightd02c5b62014-02-10 15:10:22 -08005756float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005757 return toDisplayX(rawX, DISPLAY_WIDTH);
5758}
5759
5760float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5761 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762}
5763
5764float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005765 return toDisplayY(rawY, DISPLAY_HEIGHT);
5766}
5767
5768float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5769 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770}
5771
5772
5773// --- SingleTouchInputMapperTest ---
5774
5775class SingleTouchInputMapperTest : public TouchInputMapperTest {
5776protected:
5777 void prepareButtons();
5778 void prepareAxes(int axes);
5779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005780 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5781 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5782 void processUp(SingleTouchInputMapper& mappery);
5783 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5784 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5785 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5786 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5787 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5788 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789};
5790
5791void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005792 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005793}
5794
5795void SingleTouchInputMapperTest::prepareAxes(int axes) {
5796 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005797 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5798 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799 }
5800 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005801 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5802 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005803 }
5804 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005805 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5806 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 }
5808 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005809 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5810 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811 }
5812 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005813 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5814 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815 }
5816}
5817
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005818void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822}
5823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005824void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005827}
5828
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005829void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831}
5832
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005833void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835}
5836
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005837void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5838 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005840}
5841
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005842void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005844}
5845
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005846void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5847 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850}
5851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005852void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5853 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005854 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005855}
5856
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005857void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005858 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859}
5860
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862 prepareButtons();
5863 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005864 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865
Harry Cutts16a24cc2022-10-26 15:22:19 +00005866 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867}
5868
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 prepareButtons();
5871 prepareAxes(POSITION);
5872 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005873 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005875 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005876}
5877
5878TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879 addConfigurationProperty("touch.deviceType", "touchScreen");
5880 prepareDisplay(DISPLAY_ORIENTATION_0);
5881 prepareButtons();
5882 prepareAxes(POSITION);
5883 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005884 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885
5886 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005887 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888
5889 // Virtual key is down.
5890 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5891 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5892 processDown(mapper, x, y);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5895
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005896 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897
5898 // Virtual key is up.
5899 processUp(mapper);
5900 processSync(mapper);
5901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5902
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005903 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904}
5905
5906TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005907 addConfigurationProperty("touch.deviceType", "touchScreen");
5908 prepareDisplay(DISPLAY_ORIENTATION_0);
5909 prepareButtons();
5910 prepareAxes(POSITION);
5911 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005912 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005913
5914 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005915 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916
5917 // Virtual key is down.
5918 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5919 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5920 processDown(mapper, x, y);
5921 processSync(mapper);
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5923
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005924 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005925
5926 // Virtual key is up.
5927 processUp(mapper);
5928 processSync(mapper);
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5930
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005931 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005932}
5933
5934TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005935 addConfigurationProperty("touch.deviceType", "touchScreen");
5936 prepareDisplay(DISPLAY_ORIENTATION_0);
5937 prepareButtons();
5938 prepareAxes(POSITION);
5939 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005940 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005943 ASSERT_TRUE(
5944 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005945 ASSERT_TRUE(flags[0]);
5946 ASSERT_FALSE(flags[1]);
5947}
5948
5949TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005950 addConfigurationProperty("touch.deviceType", "touchScreen");
5951 prepareDisplay(DISPLAY_ORIENTATION_0);
5952 prepareButtons();
5953 prepareAxes(POSITION);
5954 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005955 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956
arthurhungdcef2dc2020-08-11 14:47:50 +08005957 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958
5959 NotifyKeyArgs args;
5960
5961 // Press virtual key.
5962 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5963 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5964 processDown(mapper, x, y);
5965 processSync(mapper);
5966
5967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5968 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5969 ASSERT_EQ(DEVICE_ID, args.deviceId);
5970 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5971 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5972 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5973 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5974 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5975 ASSERT_EQ(KEY_HOME, args.scanCode);
5976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5977 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5978
5979 // Release virtual key.
5980 processUp(mapper);
5981 processSync(mapper);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5984 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5985 ASSERT_EQ(DEVICE_ID, args.deviceId);
5986 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5987 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5988 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5989 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5990 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5991 ASSERT_EQ(KEY_HOME, args.scanCode);
5992 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5993 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5994
5995 // Should not have sent any motions.
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5997}
5998
5999TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000 addConfigurationProperty("touch.deviceType", "touchScreen");
6001 prepareDisplay(DISPLAY_ORIENTATION_0);
6002 prepareButtons();
6003 prepareAxes(POSITION);
6004 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006005 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006006
arthurhungdcef2dc2020-08-11 14:47:50 +08006007 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008
6009 NotifyKeyArgs keyArgs;
6010
6011 // Press virtual key.
6012 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6013 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6014 processDown(mapper, x, y);
6015 processSync(mapper);
6016
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6018 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6019 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6020 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6021 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6022 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6023 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
6024 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6025 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6027 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6028
6029 // Move out of bounds. This should generate a cancel and a pointer down since we moved
6030 // into the display area.
6031 y -= 100;
6032 processMove(mapper, x, y);
6033 processSync(mapper);
6034
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6036 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6037 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6038 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6039 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6040 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6041 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
6042 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
6043 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6044 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6045 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6046 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6047
6048 NotifyMotionArgs motionArgs;
6049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6050 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6051 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6052 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6053 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6054 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6055 ASSERT_EQ(0, motionArgs.flags);
6056 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6057 ASSERT_EQ(0, motionArgs.buttonState);
6058 ASSERT_EQ(0, motionArgs.edgeFlags);
6059 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6060 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6063 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6064 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6065 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6066 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6067
6068 // Keep moving out of bounds. Should generate a pointer move.
6069 y -= 50;
6070 processMove(mapper, x, y);
6071 processSync(mapper);
6072
6073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6074 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6075 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6076 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6077 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6078 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6079 ASSERT_EQ(0, motionArgs.flags);
6080 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6081 ASSERT_EQ(0, motionArgs.buttonState);
6082 ASSERT_EQ(0, motionArgs.edgeFlags);
6083 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6084 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6087 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6088 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6089 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6090 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6091
6092 // Release out of bounds. Should generate a pointer up.
6093 processUp(mapper);
6094 processSync(mapper);
6095
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6097 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6098 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6099 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6100 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6102 ASSERT_EQ(0, motionArgs.flags);
6103 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6104 ASSERT_EQ(0, motionArgs.buttonState);
6105 ASSERT_EQ(0, motionArgs.edgeFlags);
6106 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6107 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6109 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6110 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6111 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6112 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6113 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6114
6115 // Should not have sent any more keys or motions.
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6118}
6119
6120TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006121 addConfigurationProperty("touch.deviceType", "touchScreen");
6122 prepareDisplay(DISPLAY_ORIENTATION_0);
6123 prepareButtons();
6124 prepareAxes(POSITION);
6125 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006126 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127
arthurhungdcef2dc2020-08-11 14:47:50 +08006128 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129
6130 NotifyMotionArgs motionArgs;
6131
6132 // Initially go down out of bounds.
6133 int32_t x = -10;
6134 int32_t y = -10;
6135 processDown(mapper, x, y);
6136 processSync(mapper);
6137
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6139
6140 // Move into the display area. Should generate a pointer down.
6141 x = 50;
6142 y = 75;
6143 processMove(mapper, x, y);
6144 processSync(mapper);
6145
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6147 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6148 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6149 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6150 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6151 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6152 ASSERT_EQ(0, motionArgs.flags);
6153 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6154 ASSERT_EQ(0, motionArgs.buttonState);
6155 ASSERT_EQ(0, motionArgs.edgeFlags);
6156 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6157 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6158 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6160 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6161 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6162 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6163 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6164
6165 // Release. Should generate a pointer up.
6166 processUp(mapper);
6167 processSync(mapper);
6168
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6171 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6172 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6173 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6174 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6175 ASSERT_EQ(0, motionArgs.flags);
6176 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6177 ASSERT_EQ(0, motionArgs.buttonState);
6178 ASSERT_EQ(0, motionArgs.edgeFlags);
6179 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6180 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6183 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6184 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6185 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6186 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6187
6188 // Should not have sent any more keys or motions.
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6191}
6192
Santos Cordonfa5cf462017-04-05 10:37:00 -07006193TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006194 addConfigurationProperty("touch.deviceType", "touchScreen");
6195 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6196
6197 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
6198 prepareButtons();
6199 prepareAxes(POSITION);
6200 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006201 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006202
arthurhungdcef2dc2020-08-11 14:47:50 +08006203 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006204
6205 NotifyMotionArgs motionArgs;
6206
6207 // Down.
6208 int32_t x = 100;
6209 int32_t y = 125;
6210 processDown(mapper, x, y);
6211 processSync(mapper);
6212
6213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6214 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6215 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6216 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6217 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6218 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6219 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6220 ASSERT_EQ(0, motionArgs.flags);
6221 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6222 ASSERT_EQ(0, motionArgs.buttonState);
6223 ASSERT_EQ(0, motionArgs.edgeFlags);
6224 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6225 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6228 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6229 1, 0, 0, 0, 0, 0, 0, 0));
6230 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6231 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6232 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6233
6234 // Move.
6235 x += 50;
6236 y += 75;
6237 processMove(mapper, x, y);
6238 processSync(mapper);
6239
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6241 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6242 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6243 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6244 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6245 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6247 ASSERT_EQ(0, motionArgs.flags);
6248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6249 ASSERT_EQ(0, motionArgs.buttonState);
6250 ASSERT_EQ(0, motionArgs.edgeFlags);
6251 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6252 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6255 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6256 1, 0, 0, 0, 0, 0, 0, 0));
6257 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6258 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6260
6261 // Up.
6262 processUp(mapper);
6263 processSync(mapper);
6264
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6266 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6267 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6268 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6269 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6270 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6272 ASSERT_EQ(0, motionArgs.flags);
6273 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6274 ASSERT_EQ(0, motionArgs.buttonState);
6275 ASSERT_EQ(0, motionArgs.edgeFlags);
6276 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6277 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6280 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6281 1, 0, 0, 0, 0, 0, 0, 0));
6282 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6283 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6284 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6285
6286 // Should not have sent any more keys or motions.
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6289}
6290
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292 addConfigurationProperty("touch.deviceType", "touchScreen");
6293 prepareDisplay(DISPLAY_ORIENTATION_0);
6294 prepareButtons();
6295 prepareAxes(POSITION);
6296 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006297 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298
arthurhungdcef2dc2020-08-11 14:47:50 +08006299 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006300
6301 NotifyMotionArgs motionArgs;
6302
6303 // Down.
6304 int32_t x = 100;
6305 int32_t y = 125;
6306 processDown(mapper, x, y);
6307 processSync(mapper);
6308
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6311 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6312 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6313 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6315 ASSERT_EQ(0, motionArgs.flags);
6316 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6317 ASSERT_EQ(0, motionArgs.buttonState);
6318 ASSERT_EQ(0, motionArgs.edgeFlags);
6319 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6320 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6321 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6323 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6324 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6325 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6327
6328 // Move.
6329 x += 50;
6330 y += 75;
6331 processMove(mapper, x, y);
6332 processSync(mapper);
6333
6334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6335 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6336 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6337 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6338 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6340 ASSERT_EQ(0, motionArgs.flags);
6341 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6342 ASSERT_EQ(0, motionArgs.buttonState);
6343 ASSERT_EQ(0, motionArgs.edgeFlags);
6344 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6345 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6348 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6349 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6350 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6351 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6352
6353 // Up.
6354 processUp(mapper);
6355 processSync(mapper);
6356
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6359 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6360 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6361 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6362 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6363 ASSERT_EQ(0, motionArgs.flags);
6364 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6365 ASSERT_EQ(0, motionArgs.buttonState);
6366 ASSERT_EQ(0, motionArgs.edgeFlags);
6367 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6368 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6369 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6371 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6372 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6373 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6374 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6375
6376 // Should not have sent any more keys or motions.
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6379}
6380
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006381TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006382 addConfigurationProperty("touch.deviceType", "touchScreen");
6383 prepareButtons();
6384 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006385 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6386 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006387 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006388
6389 NotifyMotionArgs args;
6390
6391 // Rotation 90.
6392 prepareDisplay(DISPLAY_ORIENTATION_90);
6393 processDown(mapper, toRawX(50), toRawY(75));
6394 processSync(mapper);
6395
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6397 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6398 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6399
6400 processUp(mapper);
6401 processSync(mapper);
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6403}
6404
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006405TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406 addConfigurationProperty("touch.deviceType", "touchScreen");
6407 prepareButtons();
6408 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006409 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6410 // orientation-aware are affected by display rotation.
6411 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006412 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413
6414 NotifyMotionArgs args;
6415
6416 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006417 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418 prepareDisplay(DISPLAY_ORIENTATION_0);
6419 processDown(mapper, toRawX(50), toRawY(75));
6420 processSync(mapper);
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6423 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6424 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6425
6426 processUp(mapper);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6429
6430 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006431 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006432 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006433 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006434 processSync(mapper);
6435
6436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6437 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6438 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6439
6440 processUp(mapper);
6441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6443
6444 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006445 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006446 prepareDisplay(DISPLAY_ORIENTATION_180);
6447 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6448 processSync(mapper);
6449
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6451 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6452 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6453
6454 processUp(mapper);
6455 processSync(mapper);
6456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6457
6458 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006459 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006461 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006462 processSync(mapper);
6463
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6465 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6466 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6467
6468 processUp(mapper);
6469 processSync(mapper);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6471}
6472
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006473TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6474 addConfigurationProperty("touch.deviceType", "touchScreen");
6475 prepareButtons();
6476 prepareAxes(POSITION);
6477 addConfigurationProperty("touch.orientationAware", "1");
6478 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6479 clearViewports();
6480 prepareDisplay(DISPLAY_ORIENTATION_0);
6481 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6482 NotifyMotionArgs args;
6483
6484 // Orientation 0.
6485 processDown(mapper, toRawX(50), toRawY(75));
6486 processSync(mapper);
6487
6488 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6489 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6490 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6491
6492 processUp(mapper);
6493 processSync(mapper);
6494 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6495}
6496
6497TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6498 addConfigurationProperty("touch.deviceType", "touchScreen");
6499 prepareButtons();
6500 prepareAxes(POSITION);
6501 addConfigurationProperty("touch.orientationAware", "1");
6502 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6503 clearViewports();
6504 prepareDisplay(DISPLAY_ORIENTATION_0);
6505 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6506 NotifyMotionArgs args;
6507
6508 // Orientation 90.
6509 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6510 processSync(mapper);
6511
6512 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6513 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6514 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6515
6516 processUp(mapper);
6517 processSync(mapper);
6518 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6519}
6520
6521TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6522 addConfigurationProperty("touch.deviceType", "touchScreen");
6523 prepareButtons();
6524 prepareAxes(POSITION);
6525 addConfigurationProperty("touch.orientationAware", "1");
6526 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6527 clearViewports();
6528 prepareDisplay(DISPLAY_ORIENTATION_0);
6529 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6530 NotifyMotionArgs args;
6531
6532 // Orientation 180.
6533 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6534 processSync(mapper);
6535
6536 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6537 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6538 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6539
6540 processUp(mapper);
6541 processSync(mapper);
6542 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6543}
6544
6545TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6546 addConfigurationProperty("touch.deviceType", "touchScreen");
6547 prepareButtons();
6548 prepareAxes(POSITION);
6549 addConfigurationProperty("touch.orientationAware", "1");
6550 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6551 clearViewports();
6552 prepareDisplay(DISPLAY_ORIENTATION_0);
6553 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6554 NotifyMotionArgs args;
6555
6556 // Orientation 270.
6557 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6558 processSync(mapper);
6559
6560 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6561 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6562 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6563
6564 processUp(mapper);
6565 processSync(mapper);
6566 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6567}
6568
6569TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6570 addConfigurationProperty("touch.deviceType", "touchScreen");
6571 prepareButtons();
6572 prepareAxes(POSITION);
6573 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6574 // orientation-aware are affected by display rotation.
6575 addConfigurationProperty("touch.orientationAware", "0");
6576 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6577 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6578
6579 NotifyMotionArgs args;
6580
6581 // Orientation 90, Rotation 0.
6582 clearViewports();
6583 prepareDisplay(DISPLAY_ORIENTATION_0);
6584 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6585 processSync(mapper);
6586
6587 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6588 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6589 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6590
6591 processUp(mapper);
6592 processSync(mapper);
6593 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6594
6595 // Orientation 90, Rotation 90.
6596 clearViewports();
6597 prepareDisplay(DISPLAY_ORIENTATION_90);
6598 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6599 processSync(mapper);
6600
6601 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6602 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6603 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6604
6605 processUp(mapper);
6606 processSync(mapper);
6607 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6608
6609 // Orientation 90, Rotation 180.
6610 clearViewports();
6611 prepareDisplay(DISPLAY_ORIENTATION_180);
6612 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6613 processSync(mapper);
6614
6615 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6616 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6617 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6618
6619 processUp(mapper);
6620 processSync(mapper);
6621 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6622
6623 // Orientation 90, Rotation 270.
6624 clearViewports();
6625 prepareDisplay(DISPLAY_ORIENTATION_270);
6626 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6627 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6628 processSync(mapper);
6629
6630 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6631 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6632 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6633
6634 processUp(mapper);
6635 processSync(mapper);
6636 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6637}
6638
Michael Wrightd02c5b62014-02-10 15:10:22 -08006639TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640 addConfigurationProperty("touch.deviceType", "touchScreen");
6641 prepareDisplay(DISPLAY_ORIENTATION_0);
6642 prepareButtons();
6643 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006644 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006645
6646 // These calculations are based on the input device calibration documentation.
6647 int32_t rawX = 100;
6648 int32_t rawY = 200;
6649 int32_t rawPressure = 10;
6650 int32_t rawToolMajor = 12;
6651 int32_t rawDistance = 2;
6652 int32_t rawTiltX = 30;
6653 int32_t rawTiltY = 110;
6654
6655 float x = toDisplayX(rawX);
6656 float y = toDisplayY(rawY);
6657 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6658 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6659 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6660 float distance = float(rawDistance);
6661
6662 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6663 float tiltScale = M_PI / 180;
6664 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6665 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6666 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6667 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6668
6669 processDown(mapper, rawX, rawY);
6670 processPressure(mapper, rawPressure);
6671 processToolMajor(mapper, rawToolMajor);
6672 processDistance(mapper, rawDistance);
6673 processTilt(mapper, rawTiltX, rawTiltY);
6674 processSync(mapper);
6675
6676 NotifyMotionArgs args;
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6679 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6680 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6681}
6682
Jason Gerecke489fda82012-09-07 17:19:40 -07006683TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006684 addConfigurationProperty("touch.deviceType", "touchScreen");
6685 prepareDisplay(DISPLAY_ORIENTATION_0);
6686 prepareLocationCalibration();
6687 prepareButtons();
6688 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006689 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006690
6691 int32_t rawX = 100;
6692 int32_t rawY = 200;
6693
6694 float x = toDisplayX(toCookedX(rawX, rawY));
6695 float y = toDisplayY(toCookedY(rawX, rawY));
6696
6697 processDown(mapper, rawX, rawY);
6698 processSync(mapper);
6699
6700 NotifyMotionArgs args;
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6703 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6704}
6705
Michael Wrightd02c5b62014-02-10 15:10:22 -08006706TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006707 addConfigurationProperty("touch.deviceType", "touchScreen");
6708 prepareDisplay(DISPLAY_ORIENTATION_0);
6709 prepareButtons();
6710 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006711 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006712
6713 NotifyMotionArgs motionArgs;
6714 NotifyKeyArgs keyArgs;
6715
6716 processDown(mapper, 100, 200);
6717 processSync(mapper);
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6719 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6720 ASSERT_EQ(0, motionArgs.buttonState);
6721
6722 // press BTN_LEFT, release BTN_LEFT
6723 processKey(mapper, BTN_LEFT, 1);
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6727 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6728
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6731 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6732
Michael Wrightd02c5b62014-02-10 15:10:22 -08006733 processKey(mapper, BTN_LEFT, 0);
6734 processSync(mapper);
6735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006736 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006737 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006738
6739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006741 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006742
6743 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6744 processKey(mapper, BTN_RIGHT, 1);
6745 processKey(mapper, BTN_MIDDLE, 1);
6746 processSync(mapper);
6747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6749 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6750 motionArgs.buttonState);
6751
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6753 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6754 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6755
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6757 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6758 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6759 motionArgs.buttonState);
6760
Michael Wrightd02c5b62014-02-10 15:10:22 -08006761 processKey(mapper, BTN_RIGHT, 0);
6762 processSync(mapper);
6763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006764 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006765 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006766
6767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006768 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006769 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006770
6771 processKey(mapper, BTN_MIDDLE, 0);
6772 processSync(mapper);
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006774 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006775 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006776
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006779 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006780
6781 // press BTN_BACK, release BTN_BACK
6782 processKey(mapper, BTN_BACK, 1);
6783 processSync(mapper);
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6785 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6786 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006787
Michael Wrightd02c5b62014-02-10 15:10:22 -08006788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006790 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6791
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6793 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6794 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006795
6796 processKey(mapper, BTN_BACK, 0);
6797 processSync(mapper);
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006799 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006801
6802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006804 ASSERT_EQ(0, motionArgs.buttonState);
6805
Michael Wrightd02c5b62014-02-10 15:10:22 -08006806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6807 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6808 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6809
6810 // press BTN_SIDE, release BTN_SIDE
6811 processKey(mapper, BTN_SIDE, 1);
6812 processSync(mapper);
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6814 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6815 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006816
Michael Wrightd02c5b62014-02-10 15:10:22 -08006817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006819 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6820
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6822 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6823 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006824
6825 processKey(mapper, BTN_SIDE, 0);
6826 processSync(mapper);
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006828 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006829 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006830
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006833 ASSERT_EQ(0, motionArgs.buttonState);
6834
Michael Wrightd02c5b62014-02-10 15:10:22 -08006835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6836 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6837 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6838
6839 // press BTN_FORWARD, release BTN_FORWARD
6840 processKey(mapper, BTN_FORWARD, 1);
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6843 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6844 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006845
Michael Wrightd02c5b62014-02-10 15:10:22 -08006846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006848 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6849
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6851 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6852 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006853
6854 processKey(mapper, BTN_FORWARD, 0);
6855 processSync(mapper);
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006857 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006858 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006859
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006861 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006862 ASSERT_EQ(0, motionArgs.buttonState);
6863
Michael Wrightd02c5b62014-02-10 15:10:22 -08006864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6865 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6866 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6867
6868 // press BTN_EXTRA, release BTN_EXTRA
6869 processKey(mapper, BTN_EXTRA, 1);
6870 processSync(mapper);
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6872 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6873 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006874
Michael Wrightd02c5b62014-02-10 15:10:22 -08006875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006877 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6878
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6880 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6881 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006882
6883 processKey(mapper, BTN_EXTRA, 0);
6884 processSync(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006887 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006888
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006891 ASSERT_EQ(0, motionArgs.buttonState);
6892
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6894 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6895 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6896
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6898
Michael Wrightd02c5b62014-02-10 15:10:22 -08006899 // press BTN_STYLUS, release BTN_STYLUS
6900 processKey(mapper, BTN_STYLUS, 1);
6901 processSync(mapper);
6902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006904 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6905
6906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6907 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6908 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006909
6910 processKey(mapper, BTN_STYLUS, 0);
6911 processSync(mapper);
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006914 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006915
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006918 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006919
6920 // press BTN_STYLUS2, release BTN_STYLUS2
6921 processKey(mapper, BTN_STYLUS2, 1);
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006925 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6926
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6928 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6929 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006930
6931 processKey(mapper, BTN_STYLUS2, 0);
6932 processSync(mapper);
6933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006935 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006936
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006939 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940
6941 // release touch
6942 processUp(mapper);
6943 processSync(mapper);
6944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6945 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6946 ASSERT_EQ(0, motionArgs.buttonState);
6947}
6948
6949TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006950 addConfigurationProperty("touch.deviceType", "touchScreen");
6951 prepareDisplay(DISPLAY_ORIENTATION_0);
6952 prepareButtons();
6953 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006954 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006955
6956 NotifyMotionArgs motionArgs;
6957
6958 // default tool type is finger
6959 processDown(mapper, 100, 200);
6960 processSync(mapper);
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6962 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6964
6965 // eraser
6966 processKey(mapper, BTN_TOOL_RUBBER, 1);
6967 processSync(mapper);
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6970 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6971
6972 // stylus
6973 processKey(mapper, BTN_TOOL_RUBBER, 0);
6974 processKey(mapper, BTN_TOOL_PEN, 1);
6975 processSync(mapper);
6976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6979
6980 // brush
6981 processKey(mapper, BTN_TOOL_PEN, 0);
6982 processKey(mapper, BTN_TOOL_BRUSH, 1);
6983 processSync(mapper);
6984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6986 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6987
6988 // pencil
6989 processKey(mapper, BTN_TOOL_BRUSH, 0);
6990 processKey(mapper, BTN_TOOL_PENCIL, 1);
6991 processSync(mapper);
6992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6995
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006996 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006997 processKey(mapper, BTN_TOOL_PENCIL, 0);
6998 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7002 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7003
7004 // mouse
7005 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7006 processKey(mapper, BTN_TOOL_MOUSE, 1);
7007 processSync(mapper);
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7010 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7011
7012 // lens
7013 processKey(mapper, BTN_TOOL_MOUSE, 0);
7014 processKey(mapper, BTN_TOOL_LENS, 1);
7015 processSync(mapper);
7016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7019
7020 // double-tap
7021 processKey(mapper, BTN_TOOL_LENS, 0);
7022 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7023 processSync(mapper);
7024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7026 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7027
7028 // triple-tap
7029 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7030 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7031 processSync(mapper);
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7033 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7034 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7035
7036 // quad-tap
7037 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7038 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7039 processSync(mapper);
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7043
7044 // finger
7045 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7046 processKey(mapper, BTN_TOOL_FINGER, 1);
7047 processSync(mapper);
7048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7051
7052 // stylus trumps finger
7053 processKey(mapper, BTN_TOOL_PEN, 1);
7054 processSync(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7057 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7058
7059 // eraser trumps stylus
7060 processKey(mapper, BTN_TOOL_RUBBER, 1);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7065
7066 // mouse trumps eraser
7067 processKey(mapper, BTN_TOOL_MOUSE, 1);
7068 processSync(mapper);
7069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7070 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7072
7073 // back to default tool type
7074 processKey(mapper, BTN_TOOL_MOUSE, 0);
7075 processKey(mapper, BTN_TOOL_RUBBER, 0);
7076 processKey(mapper, BTN_TOOL_PEN, 0);
7077 processKey(mapper, BTN_TOOL_FINGER, 0);
7078 processSync(mapper);
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7082}
7083
7084TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 addConfigurationProperty("touch.deviceType", "touchScreen");
7086 prepareDisplay(DISPLAY_ORIENTATION_0);
7087 prepareButtons();
7088 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007089 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007090 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091
7092 NotifyMotionArgs motionArgs;
7093
7094 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7095 processKey(mapper, BTN_TOOL_FINGER, 1);
7096 processMove(mapper, 100, 200);
7097 processSync(mapper);
7098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7099 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7101 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7102
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7104 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7105 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7106 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7107
7108 // move a little
7109 processMove(mapper, 150, 250);
7110 processSync(mapper);
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7112 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7114 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7115
7116 // down when BTN_TOUCH is pressed, pressure defaults to 1
7117 processKey(mapper, BTN_TOUCH, 1);
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7120 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7122 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7123
7124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7127 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7128
7129 // up when BTN_TOUCH is released, hover restored
7130 processKey(mapper, BTN_TOUCH, 0);
7131 processSync(mapper);
7132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7133 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7135 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7136
7137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7138 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7139 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7140 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7141
7142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7143 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7145 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7146
7147 // exit hover when pointer goes away
7148 processKey(mapper, BTN_TOOL_FINGER, 0);
7149 processSync(mapper);
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7151 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7153 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7154}
7155
7156TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157 addConfigurationProperty("touch.deviceType", "touchScreen");
7158 prepareDisplay(DISPLAY_ORIENTATION_0);
7159 prepareButtons();
7160 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007161 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162
7163 NotifyMotionArgs motionArgs;
7164
7165 // initially hovering because pressure is 0
7166 processDown(mapper, 100, 200);
7167 processPressure(mapper, 0);
7168 processSync(mapper);
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7170 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7172 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7173
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7175 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7176 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7177 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7178
7179 // move a little
7180 processMove(mapper, 150, 250);
7181 processSync(mapper);
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7183 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7185 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7186
7187 // down when pressure is non-zero
7188 processPressure(mapper, RAW_PRESSURE_MAX);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7191 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7193 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7194
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7196 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7198 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7199
7200 // up when pressure becomes 0, hover restored
7201 processPressure(mapper, 0);
7202 processSync(mapper);
7203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7204 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7206 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7207
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7211 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7212
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7214 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7216 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7217
7218 // exit hover when pointer goes away
7219 processUp(mapper);
7220 processSync(mapper);
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7222 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7224 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7225}
7226
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007227TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7228 addConfigurationProperty("touch.deviceType", "touchScreen");
7229 prepareDisplay(DISPLAY_ORIENTATION_0);
7230 prepareButtons();
7231 prepareAxes(POSITION | PRESSURE);
7232 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7233
7234 // Touch down.
7235 processDown(mapper, 100, 200);
7236 processPressure(mapper, 1);
7237 processSync(mapper);
7238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7239 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7240
7241 // Reset the mapper. This should cancel the ongoing gesture.
7242 resetMapper(mapper, ARBITRARY_TIME);
7243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7244 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7245
7246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7247}
7248
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007249TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7250 addConfigurationProperty("touch.deviceType", "touchScreen");
7251 prepareDisplay(DISPLAY_ORIENTATION_0);
7252 prepareButtons();
7253 prepareAxes(POSITION | PRESSURE);
7254 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7255
7256 // Set the initial state for the touch pointer.
7257 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7258 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7259 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7260 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7261
7262 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007263 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7264 // does not generate any events.
7265 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007266
7267 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7268 // the recreated touch state to generate a down event.
7269 processSync(mapper);
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7271 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7272
7273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7274}
7275
lilinnan687e58f2022-07-19 16:00:50 +08007276TEST_F(SingleTouchInputMapperTest,
7277 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7278 addConfigurationProperty("touch.deviceType", "touchScreen");
7279 prepareDisplay(DISPLAY_ORIENTATION_0);
7280 prepareButtons();
7281 prepareAxes(POSITION);
7282 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7283 NotifyMotionArgs motionArgs;
7284
7285 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007286 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007287 processSync(mapper);
7288
7289 // We should receive a down event
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7291 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7292
7293 // Change display id
7294 clearViewports();
7295 prepareSecondaryDisplay(ViewportType::INTERNAL);
7296
7297 // We should receive a cancel event
7298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7299 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7300 // Then receive reset called
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7302}
7303
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007304TEST_F(SingleTouchInputMapperTest,
7305 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7306 addConfigurationProperty("touch.deviceType", "touchScreen");
7307 prepareDisplay(DISPLAY_ORIENTATION_0);
7308 prepareButtons();
7309 prepareAxes(POSITION);
7310 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7312 NotifyMotionArgs motionArgs;
7313
7314 // Start a new gesture.
7315 processDown(mapper, 100, 200);
7316 processSync(mapper);
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7318 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7319
7320 // Make the viewport inactive. This will put the device in disabled mode.
7321 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7322 viewport->isActive = false;
7323 mFakePolicy->updateViewport(*viewport);
7324 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7325
7326 // We should receive a cancel event for the ongoing gesture.
7327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7328 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7329 // Then we should be notified that the device was reset.
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7331
7332 // No events are generated while the viewport is inactive.
7333 processMove(mapper, 101, 201);
7334 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007335 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007336 processSync(mapper);
7337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7338
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007339 // Start a new gesture while the viewport is still inactive.
7340 processDown(mapper, 300, 400);
7341 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7342 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7343 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7344 processSync(mapper);
7345
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007346 // Make the viewport active again. The device should resume processing events.
7347 viewport->isActive = true;
7348 mFakePolicy->updateViewport(*viewport);
7349 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7350
7351 // The device is reset because it changes back to direct mode, without generating any events.
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7354
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007355 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007356 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7358 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007359
7360 // No more events.
7361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7363}
7364
Prabir Pradhan211ba622022-10-31 21:09:21 +00007365TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7366 addConfigurationProperty("touch.deviceType", "touchScreen");
7367 prepareDisplay(DISPLAY_ORIENTATION_0);
7368 prepareButtons();
7369 prepareAxes(POSITION);
7370 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7372
7373 // Press a stylus button.
7374 processKey(mapper, BTN_STYLUS, 1);
7375 processSync(mapper);
7376
7377 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7378 processDown(mapper, 100, 200);
7379 processSync(mapper);
7380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7381 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7382 WithCoords(toDisplayX(100), toDisplayY(200)),
7383 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7385 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7386 WithCoords(toDisplayX(100), toDisplayY(200)),
7387 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7388
7389 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7390 // the button has not actually been released, since there will be no pointers through which the
7391 // button state can be reported. The event is generated at the location of the pointer before
7392 // it went up.
7393 processUp(mapper);
7394 processSync(mapper);
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7396 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7397 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7399 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7400 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7401}
7402
Prabir Pradhan5632d622021-09-06 07:57:20 -07007403// --- TouchDisplayProjectionTest ---
7404
7405class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7406public:
7407 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7408 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7409 // rotated equivalent of the given un-rotated physical display bounds.
7410 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7411 uint32_t inverseRotationFlags;
7412 auto width = DISPLAY_WIDTH;
7413 auto height = DISPLAY_HEIGHT;
7414 switch (orientation) {
7415 case DISPLAY_ORIENTATION_90:
7416 inverseRotationFlags = ui::Transform::ROT_270;
7417 std::swap(width, height);
7418 break;
7419 case DISPLAY_ORIENTATION_180:
7420 inverseRotationFlags = ui::Transform::ROT_180;
7421 break;
7422 case DISPLAY_ORIENTATION_270:
7423 inverseRotationFlags = ui::Transform::ROT_90;
7424 std::swap(width, height);
7425 break;
7426 case DISPLAY_ORIENTATION_0:
7427 inverseRotationFlags = ui::Transform::ROT_0;
7428 break;
7429 default:
7430 FAIL() << "Invalid orientation: " << orientation;
7431 }
7432
7433 const ui::Transform rotation(inverseRotationFlags, width, height);
7434 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7435
7436 std::optional<DisplayViewport> internalViewport =
7437 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7438 DisplayViewport& v = *internalViewport;
7439 v.displayId = DISPLAY_ID;
7440 v.orientation = orientation;
7441
7442 v.logicalLeft = 0;
7443 v.logicalTop = 0;
7444 v.logicalRight = 100;
7445 v.logicalBottom = 100;
7446
7447 v.physicalLeft = rotatedPhysicalDisplay.left;
7448 v.physicalTop = rotatedPhysicalDisplay.top;
7449 v.physicalRight = rotatedPhysicalDisplay.right;
7450 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7451
7452 v.deviceWidth = width;
7453 v.deviceHeight = height;
7454
7455 v.isActive = true;
7456 v.uniqueId = UNIQUE_ID;
7457 v.type = ViewportType::INTERNAL;
7458 mFakePolicy->updateViewport(v);
7459 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7460 }
7461
7462 void assertReceivedMove(const Point& point) {
7463 NotifyMotionArgs motionArgs;
7464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7466 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7468 1, 0, 0, 0, 0, 0, 0, 0));
7469 }
7470};
7471
7472TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7473 addConfigurationProperty("touch.deviceType", "touchScreen");
7474 prepareDisplay(DISPLAY_ORIENTATION_0);
7475
7476 prepareButtons();
7477 prepareAxes(POSITION);
7478 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7479
7480 NotifyMotionArgs motionArgs;
7481
7482 // Configure the DisplayViewport such that the logical display maps to a subsection of
7483 // the display panel called the physical display. Here, the physical display is bounded by the
7484 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7485 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7486 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7487 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7488
7489 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7490 DISPLAY_ORIENTATION_270}) {
7491 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7492
7493 // Touches outside the physical display should be ignored, and should not generate any
7494 // events. Ensure touches at the following points that lie outside of the physical display
7495 // area do not generate any events.
7496 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7497 processDown(mapper, toRawX(point.x), toRawY(point.y));
7498 processSync(mapper);
7499 processUp(mapper);
7500 processSync(mapper);
7501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7502 << "Unexpected event generated for touch outside physical display at point: "
7503 << point.x << ", " << point.y;
7504 }
7505 }
7506}
7507
7508TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7509 addConfigurationProperty("touch.deviceType", "touchScreen");
7510 prepareDisplay(DISPLAY_ORIENTATION_0);
7511
7512 prepareButtons();
7513 prepareAxes(POSITION);
7514 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7515
7516 NotifyMotionArgs motionArgs;
7517
7518 // Configure the DisplayViewport such that the logical display maps to a subsection of
7519 // the display panel called the physical display. Here, the physical display is bounded by the
7520 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7521 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7522
7523 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7524 DISPLAY_ORIENTATION_270}) {
7525 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7526
7527 // Touches that start outside the physical display should be ignored until it enters the
7528 // physical display bounds, at which point it should generate a down event. Start a touch at
7529 // the point (5, 100), which is outside the physical display bounds.
7530 static const Point kOutsidePoint{5, 100};
7531 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7532 processSync(mapper);
7533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7534
7535 // Move the touch into the physical display area. This should generate a pointer down.
7536 processMove(mapper, toRawX(11), toRawY(21));
7537 processSync(mapper);
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7539 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7540 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7541 ASSERT_NO_FATAL_FAILURE(
7542 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7543
7544 // Move the touch inside the physical display area. This should generate a pointer move.
7545 processMove(mapper, toRawX(69), toRawY(159));
7546 processSync(mapper);
7547 assertReceivedMove({69, 159});
7548
7549 // Move outside the physical display area. Since the pointer is already down, this should
7550 // now continue generating events.
7551 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7552 processSync(mapper);
7553 assertReceivedMove(kOutsidePoint);
7554
7555 // Release. This should generate a pointer up.
7556 processUp(mapper);
7557 processSync(mapper);
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7559 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7561 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7562
7563 // Ensure no more events were generated.
7564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7566 }
7567}
7568
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569// --- MultiTouchInputMapperTest ---
7570
7571class MultiTouchInputMapperTest : public TouchInputMapperTest {
7572protected:
7573 void prepareAxes(int axes);
7574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007575 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7576 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7577 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7578 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7579 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7580 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7581 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7582 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7583 void processId(MultiTouchInputMapper& mapper, int32_t id);
7584 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7585 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7586 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007587 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007588 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007589 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7590 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007591};
7592
7593void MultiTouchInputMapperTest::prepareAxes(int axes) {
7594 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007595 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7596 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007597 }
7598 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007599 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7600 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007601 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007602 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7603 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007604 }
7605 }
7606 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007607 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7608 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007609 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007610 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007611 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007612 }
7613 }
7614 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007615 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7616 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007617 }
7618 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007619 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7620 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007621 }
7622 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007623 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7624 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007625 }
7626 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007627 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7628 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007629 }
7630 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007631 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7632 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007633 }
7634 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007635 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007636 }
7637}
7638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007639void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7640 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007643}
7644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007645void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7646 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648}
7649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007650void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7651 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007653}
7654
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007655void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007657}
7658
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007659void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007661}
7662
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007663void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7664 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007666}
7667
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007668void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007670}
7671
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007672void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007674}
7675
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007676void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007678}
7679
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007680void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007682}
7683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007684void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007686}
7687
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007688void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7689 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007691}
7692
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007693void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7694 int32_t value) {
7695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7697}
7698
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007699void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007701}
7702
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007703void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7704 nsecs_t readTime) {
7705 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007706}
7707
Michael Wrightd02c5b62014-02-10 15:10:22 -08007708TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007709 addConfigurationProperty("touch.deviceType", "touchScreen");
7710 prepareDisplay(DISPLAY_ORIENTATION_0);
7711 prepareAxes(POSITION);
7712 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007713 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007714
arthurhungdcef2dc2020-08-11 14:47:50 +08007715 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007716
7717 NotifyMotionArgs motionArgs;
7718
7719 // Two fingers down at once.
7720 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7721 processPosition(mapper, x1, y1);
7722 processMTSync(mapper);
7723 processPosition(mapper, x2, y2);
7724 processMTSync(mapper);
7725 processSync(mapper);
7726
7727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7728 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7729 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7730 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7731 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7732 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7733 ASSERT_EQ(0, motionArgs.flags);
7734 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7735 ASSERT_EQ(0, motionArgs.buttonState);
7736 ASSERT_EQ(0, motionArgs.edgeFlags);
7737 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7738 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7739 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7741 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7742 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7743 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7745
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7747 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7748 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7749 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7750 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007751 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007752 ASSERT_EQ(0, motionArgs.flags);
7753 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7754 ASSERT_EQ(0, motionArgs.buttonState);
7755 ASSERT_EQ(0, motionArgs.edgeFlags);
7756 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7757 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7759 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7762 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7764 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7765 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7766 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7767 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7768
7769 // Move.
7770 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7771 processPosition(mapper, x1, y1);
7772 processMTSync(mapper);
7773 processPosition(mapper, x2, y2);
7774 processMTSync(mapper);
7775 processSync(mapper);
7776
7777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7778 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7779 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7780 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7781 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7783 ASSERT_EQ(0, motionArgs.flags);
7784 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7785 ASSERT_EQ(0, motionArgs.buttonState);
7786 ASSERT_EQ(0, motionArgs.edgeFlags);
7787 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7788 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7790 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7793 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7795 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7796 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7797 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7798 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7799
7800 // First finger up.
7801 x2 += 15; y2 -= 20;
7802 processPosition(mapper, x2, y2);
7803 processMTSync(mapper);
7804 processSync(mapper);
7805
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7808 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7809 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7810 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007811 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007812 ASSERT_EQ(0, motionArgs.flags);
7813 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7814 ASSERT_EQ(0, motionArgs.buttonState);
7815 ASSERT_EQ(0, motionArgs.edgeFlags);
7816 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7817 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7819 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7820 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7821 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7822 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7824 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7825 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7826 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7827 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7828
7829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7830 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7831 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7832 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7833 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7835 ASSERT_EQ(0, motionArgs.flags);
7836 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7837 ASSERT_EQ(0, motionArgs.buttonState);
7838 ASSERT_EQ(0, motionArgs.edgeFlags);
7839 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7840 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7841 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7843 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7844 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7845 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7846 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7847
7848 // Move.
7849 x2 += 20; y2 -= 25;
7850 processPosition(mapper, x2, y2);
7851 processMTSync(mapper);
7852 processSync(mapper);
7853
7854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7855 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7856 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7857 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7858 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7860 ASSERT_EQ(0, motionArgs.flags);
7861 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7862 ASSERT_EQ(0, motionArgs.buttonState);
7863 ASSERT_EQ(0, motionArgs.edgeFlags);
7864 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7865 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7868 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7869 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7870 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7871 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7872
7873 // New finger down.
7874 int32_t x3 = 700, y3 = 300;
7875 processPosition(mapper, x2, y2);
7876 processMTSync(mapper);
7877 processPosition(mapper, x3, y3);
7878 processMTSync(mapper);
7879 processSync(mapper);
7880
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7882 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7883 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7884 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7885 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007886 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887 ASSERT_EQ(0, motionArgs.flags);
7888 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7889 ASSERT_EQ(0, motionArgs.buttonState);
7890 ASSERT_EQ(0, motionArgs.edgeFlags);
7891 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7892 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7894 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7897 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7899 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7900 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7901 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7902 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7903
7904 // Second finger up.
7905 x3 += 30; y3 -= 20;
7906 processPosition(mapper, x3, y3);
7907 processMTSync(mapper);
7908 processSync(mapper);
7909
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7911 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7912 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7913 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7914 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007915 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007916 ASSERT_EQ(0, motionArgs.flags);
7917 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7918 ASSERT_EQ(0, motionArgs.buttonState);
7919 ASSERT_EQ(0, motionArgs.edgeFlags);
7920 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7921 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7922 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7923 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7926 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7928 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7929 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7930 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7931 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7932
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7934 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7935 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7936 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7937 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7939 ASSERT_EQ(0, motionArgs.flags);
7940 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7941 ASSERT_EQ(0, motionArgs.buttonState);
7942 ASSERT_EQ(0, motionArgs.edgeFlags);
7943 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7944 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7947 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7948 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7949 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7950 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7951
7952 // Last finger up.
7953 processMTSync(mapper);
7954 processSync(mapper);
7955
7956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7957 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7958 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7959 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7960 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7961 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7962 ASSERT_EQ(0, motionArgs.flags);
7963 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7964 ASSERT_EQ(0, motionArgs.buttonState);
7965 ASSERT_EQ(0, motionArgs.edgeFlags);
7966 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7967 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7968 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7970 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7971 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7972 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7973 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7974
7975 // Should not have sent any more keys or motions.
7976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7978}
7979
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007980TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7981 addConfigurationProperty("touch.deviceType", "touchScreen");
7982 prepareDisplay(DISPLAY_ORIENTATION_0);
7983
7984 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7985 /*fuzz*/ 0, /*resolution*/ 10);
7986 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7987 /*fuzz*/ 0, /*resolution*/ 11);
7988 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7989 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7990 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7991 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7992 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7993 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7994 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7995 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7996
7997 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7998
7999 // X and Y axes
8000 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8001 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8002 // Touch major and minor
8003 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8004 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8005 // Tool major and minor
8006 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8007 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8008}
8009
8010TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8011 addConfigurationProperty("touch.deviceType", "touchScreen");
8012 prepareDisplay(DISPLAY_ORIENTATION_0);
8013
8014 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8015 /*fuzz*/ 0, /*resolution*/ 10);
8016 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8017 /*fuzz*/ 0, /*resolution*/ 11);
8018
8019 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8020
8021 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8022
8023 // Touch major and minor
8024 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8025 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8026 // Tool major and minor
8027 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8028 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8029}
8030
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008032 addConfigurationProperty("touch.deviceType", "touchScreen");
8033 prepareDisplay(DISPLAY_ORIENTATION_0);
8034 prepareAxes(POSITION | ID);
8035 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008036 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008037
arthurhungdcef2dc2020-08-11 14:47:50 +08008038 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039
8040 NotifyMotionArgs motionArgs;
8041
8042 // Two fingers down at once.
8043 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8044 processPosition(mapper, x1, y1);
8045 processId(mapper, 1);
8046 processMTSync(mapper);
8047 processPosition(mapper, x2, y2);
8048 processId(mapper, 2);
8049 processMTSync(mapper);
8050 processSync(mapper);
8051
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8053 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8054 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8055 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8056 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8058 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8059
8060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008061 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8063 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8065 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8066 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8068 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8070 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8071
8072 // Move.
8073 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8074 processPosition(mapper, x1, y1);
8075 processId(mapper, 1);
8076 processMTSync(mapper);
8077 processPosition(mapper, x2, y2);
8078 processId(mapper, 2);
8079 processMTSync(mapper);
8080 processSync(mapper);
8081
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8084 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8085 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8087 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8088 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8090 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8092 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8093
8094 // First finger up.
8095 x2 += 15; y2 -= 20;
8096 processPosition(mapper, x2, y2);
8097 processId(mapper, 2);
8098 processMTSync(mapper);
8099 processSync(mapper);
8100
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008102 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008103 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8104 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8106 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8109 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8111 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8112
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8115 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8116 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8118 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8119 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8120
8121 // Move.
8122 x2 += 20; y2 -= 25;
8123 processPosition(mapper, x2, y2);
8124 processId(mapper, 2);
8125 processMTSync(mapper);
8126 processSync(mapper);
8127
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8130 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8131 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8134 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8135
8136 // New finger down.
8137 int32_t x3 = 700, y3 = 300;
8138 processPosition(mapper, x2, y2);
8139 processId(mapper, 2);
8140 processMTSync(mapper);
8141 processPosition(mapper, x3, y3);
8142 processId(mapper, 3);
8143 processMTSync(mapper);
8144 processSync(mapper);
8145
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008147 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008148 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8149 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8151 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8152 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8154 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8156 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8157
8158 // Second finger up.
8159 x3 += 30; y3 -= 20;
8160 processPosition(mapper, x3, y3);
8161 processId(mapper, 3);
8162 processMTSync(mapper);
8163 processSync(mapper);
8164
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008166 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8168 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8170 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8173 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8174 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8175 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8176
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8179 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8180 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8182 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8183 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8184
8185 // Last finger up.
8186 processMTSync(mapper);
8187 processSync(mapper);
8188
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8191 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8192 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8195 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8196
8197 // Should not have sent any more keys or motions.
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8200}
8201
8202TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 addConfigurationProperty("touch.deviceType", "touchScreen");
8204 prepareDisplay(DISPLAY_ORIENTATION_0);
8205 prepareAxes(POSITION | ID | SLOT);
8206 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008207 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208
arthurhungdcef2dc2020-08-11 14:47:50 +08008209 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210
8211 NotifyMotionArgs motionArgs;
8212
8213 // Two fingers down at once.
8214 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8215 processPosition(mapper, x1, y1);
8216 processId(mapper, 1);
8217 processSlot(mapper, 1);
8218 processPosition(mapper, x2, y2);
8219 processId(mapper, 2);
8220 processSync(mapper);
8221
8222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8223 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8224 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8225 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8228 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8229
8230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008231 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8235 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8238 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8240 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8241
8242 // Move.
8243 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8244 processSlot(mapper, 0);
8245 processPosition(mapper, x1, y1);
8246 processSlot(mapper, 1);
8247 processPosition(mapper, x2, y2);
8248 processSync(mapper);
8249
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8252 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8253 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8254 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8255 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8258 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8260 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8261
8262 // First finger up.
8263 x2 += 15; y2 -= 20;
8264 processSlot(mapper, 0);
8265 processId(mapper, -1);
8266 processSlot(mapper, 1);
8267 processPosition(mapper, x2, y2);
8268 processSync(mapper);
8269
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008271 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8273 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8275 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8276 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8278 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8280 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8281
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8284 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8285 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8288 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8289
8290 // Move.
8291 x2 += 20; y2 -= 25;
8292 processPosition(mapper, x2, y2);
8293 processSync(mapper);
8294
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8297 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8298 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8299 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8300 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8301 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8302
8303 // New finger down.
8304 int32_t x3 = 700, y3 = 300;
8305 processPosition(mapper, x2, y2);
8306 processSlot(mapper, 0);
8307 processId(mapper, 3);
8308 processPosition(mapper, x3, y3);
8309 processSync(mapper);
8310
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008312 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8314 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8315 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8316 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8319 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8321 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8322
8323 // Second finger up.
8324 x3 += 30; y3 -= 20;
8325 processSlot(mapper, 1);
8326 processId(mapper, -1);
8327 processSlot(mapper, 0);
8328 processPosition(mapper, x3, y3);
8329 processSync(mapper);
8330
8331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008332 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008333 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8334 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8335 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8336 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8339 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8341 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8342
8343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8344 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8345 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8346 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8347 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8349 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8350
8351 // Last finger up.
8352 processId(mapper, -1);
8353 processSync(mapper);
8354
8355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8356 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8357 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8358 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8361 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8362
8363 // Should not have sent any more keys or motions.
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8366}
8367
8368TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008369 addConfigurationProperty("touch.deviceType", "touchScreen");
8370 prepareDisplay(DISPLAY_ORIENTATION_0);
8371 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008372 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373
8374 // These calculations are based on the input device calibration documentation.
8375 int32_t rawX = 100;
8376 int32_t rawY = 200;
8377 int32_t rawTouchMajor = 7;
8378 int32_t rawTouchMinor = 6;
8379 int32_t rawToolMajor = 9;
8380 int32_t rawToolMinor = 8;
8381 int32_t rawPressure = 11;
8382 int32_t rawDistance = 0;
8383 int32_t rawOrientation = 3;
8384 int32_t id = 5;
8385
8386 float x = toDisplayX(rawX);
8387 float y = toDisplayY(rawY);
8388 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8389 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8390 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8391 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8392 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8393 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8394 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8395 float distance = float(rawDistance);
8396
8397 processPosition(mapper, rawX, rawY);
8398 processTouchMajor(mapper, rawTouchMajor);
8399 processTouchMinor(mapper, rawTouchMinor);
8400 processToolMajor(mapper, rawToolMajor);
8401 processToolMinor(mapper, rawToolMinor);
8402 processPressure(mapper, rawPressure);
8403 processOrientation(mapper, rawOrientation);
8404 processDistance(mapper, rawDistance);
8405 processId(mapper, id);
8406 processMTSync(mapper);
8407 processSync(mapper);
8408
8409 NotifyMotionArgs args;
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8411 ASSERT_EQ(0, args.pointerProperties[0].id);
8412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8413 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8414 orientation, distance));
8415}
8416
8417TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008418 addConfigurationProperty("touch.deviceType", "touchScreen");
8419 prepareDisplay(DISPLAY_ORIENTATION_0);
8420 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8421 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008422 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008423
8424 // These calculations are based on the input device calibration documentation.
8425 int32_t rawX = 100;
8426 int32_t rawY = 200;
8427 int32_t rawTouchMajor = 140;
8428 int32_t rawTouchMinor = 120;
8429 int32_t rawToolMajor = 180;
8430 int32_t rawToolMinor = 160;
8431
8432 float x = toDisplayX(rawX);
8433 float y = toDisplayY(rawY);
8434 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8435 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8436 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8437 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8438 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8439
8440 processPosition(mapper, rawX, rawY);
8441 processTouchMajor(mapper, rawTouchMajor);
8442 processTouchMinor(mapper, rawTouchMinor);
8443 processToolMajor(mapper, rawToolMajor);
8444 processToolMinor(mapper, rawToolMinor);
8445 processMTSync(mapper);
8446 processSync(mapper);
8447
8448 NotifyMotionArgs args;
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8451 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8452}
8453
8454TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008455 addConfigurationProperty("touch.deviceType", "touchScreen");
8456 prepareDisplay(DISPLAY_ORIENTATION_0);
8457 prepareAxes(POSITION | TOUCH | TOOL);
8458 addConfigurationProperty("touch.size.calibration", "diameter");
8459 addConfigurationProperty("touch.size.scale", "10");
8460 addConfigurationProperty("touch.size.bias", "160");
8461 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008462 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008463
8464 // These calculations are based on the input device calibration documentation.
8465 // Note: We only provide a single common touch/tool value because the device is assumed
8466 // not to emit separate values for each pointer (isSummed = 1).
8467 int32_t rawX = 100;
8468 int32_t rawY = 200;
8469 int32_t rawX2 = 150;
8470 int32_t rawY2 = 250;
8471 int32_t rawTouchMajor = 5;
8472 int32_t rawToolMajor = 8;
8473
8474 float x = toDisplayX(rawX);
8475 float y = toDisplayY(rawY);
8476 float x2 = toDisplayX(rawX2);
8477 float y2 = toDisplayY(rawY2);
8478 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8479 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8480 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8481
8482 processPosition(mapper, rawX, rawY);
8483 processTouchMajor(mapper, rawTouchMajor);
8484 processToolMajor(mapper, rawToolMajor);
8485 processMTSync(mapper);
8486 processPosition(mapper, rawX2, rawY2);
8487 processTouchMajor(mapper, rawTouchMajor);
8488 processToolMajor(mapper, rawToolMajor);
8489 processMTSync(mapper);
8490 processSync(mapper);
8491
8492 NotifyMotionArgs args;
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8495
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008497 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008498 ASSERT_EQ(size_t(2), args.pointerCount);
8499 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8500 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8502 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8503}
8504
8505TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008506 addConfigurationProperty("touch.deviceType", "touchScreen");
8507 prepareDisplay(DISPLAY_ORIENTATION_0);
8508 prepareAxes(POSITION | TOUCH | TOOL);
8509 addConfigurationProperty("touch.size.calibration", "area");
8510 addConfigurationProperty("touch.size.scale", "43");
8511 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008512 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008513
8514 // These calculations are based on the input device calibration documentation.
8515 int32_t rawX = 100;
8516 int32_t rawY = 200;
8517 int32_t rawTouchMajor = 5;
8518 int32_t rawToolMajor = 8;
8519
8520 float x = toDisplayX(rawX);
8521 float y = toDisplayY(rawY);
8522 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8523 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8524 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8525
8526 processPosition(mapper, rawX, rawY);
8527 processTouchMajor(mapper, rawTouchMajor);
8528 processToolMajor(mapper, rawToolMajor);
8529 processMTSync(mapper);
8530 processSync(mapper);
8531
8532 NotifyMotionArgs args;
8533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8535 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8536}
8537
8538TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539 addConfigurationProperty("touch.deviceType", "touchScreen");
8540 prepareDisplay(DISPLAY_ORIENTATION_0);
8541 prepareAxes(POSITION | PRESSURE);
8542 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8543 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008544 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545
Michael Wrightaa449c92017-12-13 21:21:43 +00008546 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008547 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008548 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8549 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8550 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8551
Michael Wrightd02c5b62014-02-10 15:10:22 -08008552 // These calculations are based on the input device calibration documentation.
8553 int32_t rawX = 100;
8554 int32_t rawY = 200;
8555 int32_t rawPressure = 60;
8556
8557 float x = toDisplayX(rawX);
8558 float y = toDisplayY(rawY);
8559 float pressure = float(rawPressure) * 0.01f;
8560
8561 processPosition(mapper, rawX, rawY);
8562 processPressure(mapper, rawPressure);
8563 processMTSync(mapper);
8564 processSync(mapper);
8565
8566 NotifyMotionArgs args;
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8569 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8570}
8571
8572TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008573 addConfigurationProperty("touch.deviceType", "touchScreen");
8574 prepareDisplay(DISPLAY_ORIENTATION_0);
8575 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008576 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008577
8578 NotifyMotionArgs motionArgs;
8579 NotifyKeyArgs keyArgs;
8580
8581 processId(mapper, 1);
8582 processPosition(mapper, 100, 200);
8583 processSync(mapper);
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8585 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8586 ASSERT_EQ(0, motionArgs.buttonState);
8587
8588 // press BTN_LEFT, release BTN_LEFT
8589 processKey(mapper, BTN_LEFT, 1);
8590 processSync(mapper);
8591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8593 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8594
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8597 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8598
Michael Wrightd02c5b62014-02-10 15:10:22 -08008599 processKey(mapper, BTN_LEFT, 0);
8600 processSync(mapper);
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008602 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008603 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008604
8605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008607 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008608
8609 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8610 processKey(mapper, BTN_RIGHT, 1);
8611 processKey(mapper, BTN_MIDDLE, 1);
8612 processSync(mapper);
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8615 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8616 motionArgs.buttonState);
8617
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8620 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8621
8622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8624 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8625 motionArgs.buttonState);
8626
Michael Wrightd02c5b62014-02-10 15:10:22 -08008627 processKey(mapper, BTN_RIGHT, 0);
8628 processSync(mapper);
8629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008631 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008632
8633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008635 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008636
8637 processKey(mapper, BTN_MIDDLE, 0);
8638 processSync(mapper);
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008641 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008642
8643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008645 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008646
8647 // press BTN_BACK, release BTN_BACK
8648 processKey(mapper, BTN_BACK, 1);
8649 processSync(mapper);
8650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8651 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8652 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008653
Michael Wrightd02c5b62014-02-10 15:10:22 -08008654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008656 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8657
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8659 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8660 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008661
8662 processKey(mapper, BTN_BACK, 0);
8663 processSync(mapper);
8664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008666 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008667
8668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008670 ASSERT_EQ(0, motionArgs.buttonState);
8671
Michael Wrightd02c5b62014-02-10 15:10:22 -08008672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8673 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8674 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8675
8676 // press BTN_SIDE, release BTN_SIDE
8677 processKey(mapper, BTN_SIDE, 1);
8678 processSync(mapper);
8679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8680 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8681 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008682
Michael Wrightd02c5b62014-02-10 15:10:22 -08008683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008685 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8686
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8688 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8689 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008690
8691 processKey(mapper, BTN_SIDE, 0);
8692 processSync(mapper);
8693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008694 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008695 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008696
8697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008699 ASSERT_EQ(0, motionArgs.buttonState);
8700
Michael Wrightd02c5b62014-02-10 15:10:22 -08008701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8702 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8703 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8704
8705 // press BTN_FORWARD, release BTN_FORWARD
8706 processKey(mapper, BTN_FORWARD, 1);
8707 processSync(mapper);
8708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8709 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8710 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008711
Michael Wrightd02c5b62014-02-10 15:10:22 -08008712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008714 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8715
8716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8718 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008719
8720 processKey(mapper, BTN_FORWARD, 0);
8721 processSync(mapper);
8722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008724 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008725
8726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008728 ASSERT_EQ(0, motionArgs.buttonState);
8729
Michael Wrightd02c5b62014-02-10 15:10:22 -08008730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8731 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8732 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8733
8734 // press BTN_EXTRA, release BTN_EXTRA
8735 processKey(mapper, BTN_EXTRA, 1);
8736 processSync(mapper);
8737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8738 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8739 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008740
Michael Wrightd02c5b62014-02-10 15:10:22 -08008741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008743 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8744
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8746 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8747 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008748
8749 processKey(mapper, BTN_EXTRA, 0);
8750 processSync(mapper);
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008752 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008753 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008754
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008757 ASSERT_EQ(0, motionArgs.buttonState);
8758
Michael Wrightd02c5b62014-02-10 15:10:22 -08008759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8760 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8761 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8762
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8764
Michael Wrightd02c5b62014-02-10 15:10:22 -08008765 // press BTN_STYLUS, release BTN_STYLUS
8766 processKey(mapper, BTN_STYLUS, 1);
8767 processSync(mapper);
8768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008770 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8771
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8773 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8774 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008775
8776 processKey(mapper, BTN_STYLUS, 0);
8777 processSync(mapper);
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008779 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008780 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008781
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008784 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008785
8786 // press BTN_STYLUS2, release BTN_STYLUS2
8787 processKey(mapper, BTN_STYLUS2, 1);
8788 processSync(mapper);
8789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008791 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8792
8793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8794 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8795 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008796
8797 processKey(mapper, BTN_STYLUS2, 0);
8798 processSync(mapper);
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008800 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008801 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008802
8803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008805 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008806
8807 // release touch
8808 processId(mapper, -1);
8809 processSync(mapper);
8810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8811 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8812 ASSERT_EQ(0, motionArgs.buttonState);
8813}
8814
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008815TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8816 addConfigurationProperty("touch.deviceType", "touchScreen");
8817 prepareDisplay(DISPLAY_ORIENTATION_0);
8818 prepareAxes(POSITION | ID | SLOT);
8819 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8820
8821 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8822 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8823
8824 // Touch down.
8825 processId(mapper, 1);
8826 processPosition(mapper, 100, 200);
8827 processSync(mapper);
8828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8829 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8830
8831 // Press and release button mapped to the primary stylus button.
8832 processKey(mapper, BTN_A, 1);
8833 processSync(mapper);
8834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8835 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8836 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8838 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8839 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8840
8841 processKey(mapper, BTN_A, 0);
8842 processSync(mapper);
8843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8844 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8846 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8847
8848 // Press and release the HID usage mapped to the secondary stylus button.
8849 processHidUsage(mapper, 0xabcd, 1);
8850 processSync(mapper);
8851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8852 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8853 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8855 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8856 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8857
8858 processHidUsage(mapper, 0xabcd, 0);
8859 processSync(mapper);
8860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8861 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8863 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8864
8865 // Release touch.
8866 processId(mapper, -1);
8867 processSync(mapper);
8868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8869 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8870}
8871
Michael Wrightd02c5b62014-02-10 15:10:22 -08008872TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008873 addConfigurationProperty("touch.deviceType", "touchScreen");
8874 prepareDisplay(DISPLAY_ORIENTATION_0);
8875 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008876 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008877
8878 NotifyMotionArgs motionArgs;
8879
8880 // default tool type is finger
8881 processId(mapper, 1);
8882 processPosition(mapper, 100, 200);
8883 processSync(mapper);
8884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8885 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8887
8888 // eraser
8889 processKey(mapper, BTN_TOOL_RUBBER, 1);
8890 processSync(mapper);
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8894
8895 // stylus
8896 processKey(mapper, BTN_TOOL_RUBBER, 0);
8897 processKey(mapper, BTN_TOOL_PEN, 1);
8898 processSync(mapper);
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8901 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8902
8903 // brush
8904 processKey(mapper, BTN_TOOL_PEN, 0);
8905 processKey(mapper, BTN_TOOL_BRUSH, 1);
8906 processSync(mapper);
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8910
8911 // pencil
8912 processKey(mapper, BTN_TOOL_BRUSH, 0);
8913 processKey(mapper, BTN_TOOL_PENCIL, 1);
8914 processSync(mapper);
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8917 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8918
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008919 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008920 processKey(mapper, BTN_TOOL_PENCIL, 0);
8921 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8922 processSync(mapper);
8923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8925 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8926
8927 // mouse
8928 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8929 processKey(mapper, BTN_TOOL_MOUSE, 1);
8930 processSync(mapper);
8931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8934
8935 // lens
8936 processKey(mapper, BTN_TOOL_MOUSE, 0);
8937 processKey(mapper, BTN_TOOL_LENS, 1);
8938 processSync(mapper);
8939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8942
8943 // double-tap
8944 processKey(mapper, BTN_TOOL_LENS, 0);
8945 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8946 processSync(mapper);
8947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8949 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8950
8951 // triple-tap
8952 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8953 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8957 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8958
8959 // quad-tap
8960 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8961 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8962 processSync(mapper);
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8966
8967 // finger
8968 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8969 processKey(mapper, BTN_TOOL_FINGER, 1);
8970 processSync(mapper);
8971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8973 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8974
8975 // stylus trumps finger
8976 processKey(mapper, BTN_TOOL_PEN, 1);
8977 processSync(mapper);
8978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8979 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8980 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8981
8982 // eraser trumps stylus
8983 processKey(mapper, BTN_TOOL_RUBBER, 1);
8984 processSync(mapper);
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8986 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8987 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8988
8989 // mouse trumps eraser
8990 processKey(mapper, BTN_TOOL_MOUSE, 1);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8994 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8995
8996 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8997 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8998 processSync(mapper);
8999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9000 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9002
9003 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9004 processToolType(mapper, MT_TOOL_PEN);
9005 processSync(mapper);
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9008 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9009
9010 // back to default tool type
9011 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9012 processKey(mapper, BTN_TOOL_MOUSE, 0);
9013 processKey(mapper, BTN_TOOL_RUBBER, 0);
9014 processKey(mapper, BTN_TOOL_PEN, 0);
9015 processKey(mapper, BTN_TOOL_FINGER, 0);
9016 processSync(mapper);
9017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9018 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9020}
9021
9022TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009023 addConfigurationProperty("touch.deviceType", "touchScreen");
9024 prepareDisplay(DISPLAY_ORIENTATION_0);
9025 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009026 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009027 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009028
9029 NotifyMotionArgs motionArgs;
9030
9031 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9032 processId(mapper, 1);
9033 processPosition(mapper, 100, 200);
9034 processSync(mapper);
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9036 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9038 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9039
9040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9041 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9043 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9044
9045 // move a little
9046 processPosition(mapper, 150, 250);
9047 processSync(mapper);
9048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9049 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9051 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9052
9053 // down when BTN_TOUCH is pressed, pressure defaults to 1
9054 processKey(mapper, BTN_TOUCH, 1);
9055 processSync(mapper);
9056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9057 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9058 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9059 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9060
9061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9062 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9064 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9065
9066 // up when BTN_TOUCH is released, hover restored
9067 processKey(mapper, BTN_TOUCH, 0);
9068 processSync(mapper);
9069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9070 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9071 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9072 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9073
9074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9075 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9076 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9077 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9078
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9082 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9083
9084 // exit hover when pointer goes away
9085 processId(mapper, -1);
9086 processSync(mapper);
9087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9088 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9090 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9091}
9092
9093TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009094 addConfigurationProperty("touch.deviceType", "touchScreen");
9095 prepareDisplay(DISPLAY_ORIENTATION_0);
9096 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009097 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009098
9099 NotifyMotionArgs motionArgs;
9100
9101 // initially hovering because pressure is 0
9102 processId(mapper, 1);
9103 processPosition(mapper, 100, 200);
9104 processPressure(mapper, 0);
9105 processSync(mapper);
9106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9107 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9109 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9110
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9112 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9113 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9114 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9115
9116 // move a little
9117 processPosition(mapper, 150, 250);
9118 processSync(mapper);
9119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9120 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9121 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9122 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9123
9124 // down when pressure becomes non-zero
9125 processPressure(mapper, RAW_PRESSURE_MAX);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9130 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9131
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9135 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9136
9137 // up when pressure becomes 0, hover restored
9138 processPressure(mapper, 0);
9139 processSync(mapper);
9140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9141 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9143 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9144
9145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9146 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9148 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9149
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9151 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9153 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9154
9155 // exit hover when pointer goes away
9156 processId(mapper, -1);
9157 processSync(mapper);
9158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9159 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9161 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9162}
9163
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009164/**
9165 * Set the input device port <--> display port associations, and check that the
9166 * events are routed to the display that matches the display port.
9167 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9168 */
9169TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009170 const std::string usb2 = "USB2";
9171 const uint8_t hdmi1 = 0;
9172 const uint8_t hdmi2 = 1;
9173 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009174 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009175
9176 addConfigurationProperty("touch.deviceType", "touchScreen");
9177 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009178 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009179
9180 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9181 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9182
9183 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9184 // for this input device is specified, and the matching viewport is not present,
9185 // the input device should be disabled (at the mapper level).
9186
9187 // Add viewport for display 2 on hdmi2
9188 prepareSecondaryDisplay(type, hdmi2);
9189 // Send a touch event
9190 processPosition(mapper, 100, 100);
9191 processSync(mapper);
9192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9193
9194 // Add viewport for display 1 on hdmi1
9195 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
9196 // Send a touch event again
9197 processPosition(mapper, 100, 100);
9198 processSync(mapper);
9199
9200 NotifyMotionArgs args;
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9202 ASSERT_EQ(DISPLAY_ID, args.displayId);
9203}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009204
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009205TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9206 addConfigurationProperty("touch.deviceType", "touchScreen");
9207 prepareAxes(POSITION);
9208 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9209
9210 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9211
9212 prepareDisplay(DISPLAY_ORIENTATION_0);
9213 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9214
9215 // Send a touch event
9216 processPosition(mapper, 100, 100);
9217 processSync(mapper);
9218
9219 NotifyMotionArgs args;
9220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9221 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9222}
9223
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009224TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009225 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009226 std::shared_ptr<FakePointerController> fakePointerController =
9227 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009228 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009229 fakePointerController->setPosition(100, 200);
9230 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009231 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009232
Garfield Tan888a6a42020-01-09 11:39:16 -08009233 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009234 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009235
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009236 prepareDisplay(DISPLAY_ORIENTATION_0);
9237 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009238 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009239
Harry Cutts16a24cc2022-10-26 15:22:19 +00009240 // Check source is a touchpad that would obtain the PointerController.
9241 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009242
9243 NotifyMotionArgs motionArgs;
9244 processPosition(mapper, 100, 100);
9245 processSync(mapper);
9246
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9249 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9250}
9251
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009252/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009253 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9254 */
9255TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9256 addConfigurationProperty("touch.deviceType", "touchScreen");
9257 prepareAxes(POSITION);
9258 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9259
9260 prepareDisplay(DISPLAY_ORIENTATION_0);
9261 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9262 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9263 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9264 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9265
9266 NotifyMotionArgs args;
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9268 ASSERT_EQ(26, args.readTime);
9269
9270 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9271 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9272 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9273
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9275 ASSERT_EQ(33, args.readTime);
9276}
9277
9278/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009279 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9280 * events should not be delivered to the listener.
9281 */
9282TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9283 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009284 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009285 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9286 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9287 ViewportType::INTERNAL);
9288 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9289 prepareAxes(POSITION);
9290 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9291
9292 NotifyMotionArgs motionArgs;
9293 processPosition(mapper, 100, 100);
9294 processSync(mapper);
9295
9296 mFakeListener->assertNotifyMotionWasNotCalled();
9297}
9298
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009299/**
9300 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9301 * the touch mapper can process the events and the events can be delivered to the listener.
9302 */
9303TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9304 addConfigurationProperty("touch.deviceType", "touchScreen");
9305 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9306 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9307 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9308 ViewportType::INTERNAL);
9309 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9310 prepareAxes(POSITION);
9311 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9312
9313 NotifyMotionArgs motionArgs;
9314 processPosition(mapper, 100, 100);
9315 processSync(mapper);
9316
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9318 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9319}
9320
Garfield Tanc734e4f2021-01-15 20:01:39 -08009321TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9322 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009323 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Garfield Tanc734e4f2021-01-15 20:01:39 -08009324 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9325 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
9326 ViewportType::INTERNAL);
9327 std::optional<DisplayViewport> optionalDisplayViewport =
9328 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9329 ASSERT_TRUE(optionalDisplayViewport.has_value());
9330 DisplayViewport displayViewport = *optionalDisplayViewport;
9331
9332 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9333 prepareAxes(POSITION);
9334 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9335
9336 // Finger down
9337 int32_t x = 100, y = 100;
9338 processPosition(mapper, x, y);
9339 processSync(mapper);
9340
9341 NotifyMotionArgs motionArgs;
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9343 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9344
9345 // Deactivate display viewport
9346 displayViewport.isActive = false;
9347 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9348 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9349
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009350 // The ongoing touch should be canceled immediately
9351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9352 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9353
9354 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009355 x += 10, y += 10;
9356 processPosition(mapper, x, y);
9357 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009359
9360 // Reactivate display viewport
9361 displayViewport.isActive = true;
9362 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9363 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9364
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009365 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009366 x += 10, y += 10;
9367 processPosition(mapper, x, y);
9368 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9370 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009371}
9372
Arthur Hung7c645402019-01-25 17:45:42 +08009373TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9374 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009375 prepareAxes(POSITION | ID | SLOT);
9376 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009377 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009378
9379 // Create the second touch screen device, and enable multi fingers.
9380 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009381 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009382 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009383 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009384 std::shared_ptr<InputDevice> device2 =
9385 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009386 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009387
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009388 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9389 0 /*flat*/, 0 /*fuzz*/);
9390 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9391 0 /*flat*/, 0 /*fuzz*/);
9392 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9393 0 /*flat*/, 0 /*fuzz*/);
9394 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9395 0 /*flat*/, 0 /*fuzz*/);
9396 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9397 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9398 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009399
9400 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009401 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009402 std::list<NotifyArgs> unused =
9403 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9404 0 /*changes*/);
9405 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009406
9407 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009408 std::shared_ptr<FakePointerController> fakePointerController =
9409 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009410 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009411
9412 // Setup policy for associated displays and show touches.
9413 const uint8_t hdmi1 = 0;
9414 const uint8_t hdmi2 = 1;
9415 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9416 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9417 mFakePolicy->setShowTouches(true);
9418
9419 // Create displays.
9420 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009421 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009422
9423 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009424 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9425 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9426 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009427
9428 // Two fingers down at default display.
9429 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9430 processPosition(mapper, x1, y1);
9431 processId(mapper, 1);
9432 processSlot(mapper, 1);
9433 processPosition(mapper, x2, y2);
9434 processId(mapper, 2);
9435 processSync(mapper);
9436
9437 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9438 fakePointerController->getSpots().find(DISPLAY_ID);
9439 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9440 ASSERT_EQ(size_t(2), iter->second.size());
9441
9442 // Two fingers down at second display.
9443 processPosition(mapper2, x1, y1);
9444 processId(mapper2, 1);
9445 processSlot(mapper2, 1);
9446 processPosition(mapper2, x2, y2);
9447 processId(mapper2, 2);
9448 processSync(mapper2);
9449
9450 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9451 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9452 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009453
9454 // Disable the show touches configuration and ensure the spots are cleared.
9455 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009456 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9457 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009458
9459 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009460}
9461
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009462TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009463 prepareAxes(POSITION);
9464 addConfigurationProperty("touch.deviceType", "touchScreen");
9465 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009466 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009467
9468 NotifyMotionArgs motionArgs;
9469 // Unrotated video frame
9470 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9471 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009472 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009473 processPosition(mapper, 100, 200);
9474 processSync(mapper);
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9476 ASSERT_EQ(frames, motionArgs.videoFrames);
9477
9478 // Subsequent touch events should not have any videoframes
9479 // This is implemented separately in FakeEventHub,
9480 // but that should match the behaviour of TouchVideoDevice.
9481 processPosition(mapper, 200, 200);
9482 processSync(mapper);
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9484 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9485}
9486
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009487TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009488 prepareAxes(POSITION);
9489 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009490 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009491 // Unrotated video frame
9492 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9493 NotifyMotionArgs motionArgs;
9494
9495 // Test all 4 orientations
9496 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009497 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9498 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9499 clearViewports();
9500 prepareDisplay(orientation);
9501 std::vector<TouchVideoFrame> frames{frame};
9502 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9503 processPosition(mapper, 100, 200);
9504 processSync(mapper);
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9506 ASSERT_EQ(frames, motionArgs.videoFrames);
9507 }
9508}
9509
9510TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9511 prepareAxes(POSITION);
9512 addConfigurationProperty("touch.deviceType", "touchScreen");
9513 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9514 // orientation-aware are affected by display rotation.
9515 addConfigurationProperty("touch.orientationAware", "0");
9516 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9517 // Unrotated video frame
9518 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9519 NotifyMotionArgs motionArgs;
9520
9521 // Test all 4 orientations
9522 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009523 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9524 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9525 clearViewports();
9526 prepareDisplay(orientation);
9527 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009528 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009529 processPosition(mapper, 100, 200);
9530 processSync(mapper);
9531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009532 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9533 // compared to the display. This is so that when the window transform (which contains the
9534 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9535 // window's coordinate space.
9536 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009537 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009538
9539 // Release finger.
9540 processSync(mapper);
9541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009542 }
9543}
9544
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009545TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009546 prepareAxes(POSITION);
9547 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009548 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009549 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9550 // so mix these.
9551 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9552 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9553 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9554 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9555 NotifyMotionArgs motionArgs;
9556
9557 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009558 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009559 processPosition(mapper, 100, 200);
9560 processSync(mapper);
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009562 ASSERT_EQ(frames, motionArgs.videoFrames);
9563}
9564
9565TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9566 prepareAxes(POSITION);
9567 addConfigurationProperty("touch.deviceType", "touchScreen");
9568 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9569 // orientation-aware are affected by display rotation.
9570 addConfigurationProperty("touch.orientationAware", "0");
9571 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9572 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9573 // so mix these.
9574 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9575 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9576 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9577 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9578 NotifyMotionArgs motionArgs;
9579
9580 prepareDisplay(DISPLAY_ORIENTATION_90);
9581 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9582 processPosition(mapper, 100, 200);
9583 processSync(mapper);
9584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9585 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9586 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9587 // compared to the display. This is so that when the window transform (which contains the
9588 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9589 // window's coordinate space.
9590 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9591 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009592 ASSERT_EQ(frames, motionArgs.videoFrames);
9593}
9594
Arthur Hung9da14732019-09-02 16:16:58 +08009595/**
9596 * If we had defined port associations, but the viewport is not ready, the touch device would be
9597 * expected to be disabled, and it should be enabled after the viewport has found.
9598 */
9599TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009600 constexpr uint8_t hdmi2 = 1;
9601 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009602 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009603
9604 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9605
9606 addConfigurationProperty("touch.deviceType", "touchScreen");
9607 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009609
9610 ASSERT_EQ(mDevice->isEnabled(), false);
9611
9612 // Add display on hdmi2, the device should be enabled and can receive touch event.
9613 prepareSecondaryDisplay(type, hdmi2);
9614 ASSERT_EQ(mDevice->isEnabled(), true);
9615
9616 // Send a touch event.
9617 processPosition(mapper, 100, 100);
9618 processSync(mapper);
9619
9620 NotifyMotionArgs args;
9621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9622 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9623}
9624
Arthur Hung421eb1c2020-01-16 00:09:42 +08009625TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009626 addConfigurationProperty("touch.deviceType", "touchScreen");
9627 prepareDisplay(DISPLAY_ORIENTATION_0);
9628 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009629 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009630
9631 NotifyMotionArgs motionArgs;
9632
9633 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9634 // finger down
9635 processId(mapper, 1);
9636 processPosition(mapper, x1, y1);
9637 processSync(mapper);
9638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9641
9642 // finger move
9643 processId(mapper, 1);
9644 processPosition(mapper, x2, y2);
9645 processSync(mapper);
9646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9649
9650 // finger up.
9651 processId(mapper, -1);
9652 processSync(mapper);
9653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9654 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9656
9657 // new finger down
9658 processId(mapper, 1);
9659 processPosition(mapper, x3, y3);
9660 processSync(mapper);
9661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9663 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9664}
9665
9666/**
arthurhungcc7f9802020-04-30 17:55:40 +08009667 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9668 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009669 */
arthurhungcc7f9802020-04-30 17:55:40 +08009670TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009671 addConfigurationProperty("touch.deviceType", "touchScreen");
9672 prepareDisplay(DISPLAY_ORIENTATION_0);
9673 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009674 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009675
9676 NotifyMotionArgs motionArgs;
9677
9678 // default tool type is finger
9679 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009680 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009681 processPosition(mapper, x1, y1);
9682 processSync(mapper);
9683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9684 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9686
9687 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9688 processToolType(mapper, MT_TOOL_PALM);
9689 processSync(mapper);
9690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9691 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9692
9693 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009694 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009695 processPosition(mapper, x2, y2);
9696 processSync(mapper);
9697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9698
9699 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009700 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009701 processSync(mapper);
9702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9703
9704 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009705 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009706 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009707 processPosition(mapper, x3, y3);
9708 processSync(mapper);
9709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9710 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9712}
9713
arthurhungbf89a482020-04-17 17:37:55 +08009714/**
arthurhungcc7f9802020-04-30 17:55:40 +08009715 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9716 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009717 */
arthurhungcc7f9802020-04-30 17:55:40 +08009718TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009719 addConfigurationProperty("touch.deviceType", "touchScreen");
9720 prepareDisplay(DISPLAY_ORIENTATION_0);
9721 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9722 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9723
9724 NotifyMotionArgs motionArgs;
9725
9726 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009727 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9728 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009729 processPosition(mapper, x1, y1);
9730 processSync(mapper);
9731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9732 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9734
9735 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009736 processSlot(mapper, SECOND_SLOT);
9737 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009738 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009739 processSync(mapper);
9740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009741 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9743
9744 // If the tool type of the first finger changes to MT_TOOL_PALM,
9745 // we expect to receive ACTION_POINTER_UP with cancel flag.
9746 processSlot(mapper, FIRST_SLOT);
9747 processId(mapper, FIRST_TRACKING_ID);
9748 processToolType(mapper, MT_TOOL_PALM);
9749 processSync(mapper);
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009751 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009752 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9753
9754 // The following MOVE events of second finger should be processed.
9755 processSlot(mapper, SECOND_SLOT);
9756 processId(mapper, SECOND_TRACKING_ID);
9757 processPosition(mapper, x2 + 1, y2 + 1);
9758 processSync(mapper);
9759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9761 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9762
9763 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9764 // it. Second finger receive move.
9765 processSlot(mapper, FIRST_SLOT);
9766 processId(mapper, INVALID_TRACKING_ID);
9767 processSync(mapper);
9768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9770 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9771
9772 // Second finger keeps moving.
9773 processSlot(mapper, SECOND_SLOT);
9774 processId(mapper, SECOND_TRACKING_ID);
9775 processPosition(mapper, x2 + 2, y2 + 2);
9776 processSync(mapper);
9777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9779 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9780
9781 // Second finger up.
9782 processId(mapper, INVALID_TRACKING_ID);
9783 processSync(mapper);
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9786 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9787}
9788
9789/**
9790 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9791 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9792 */
9793TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9794 addConfigurationProperty("touch.deviceType", "touchScreen");
9795 prepareDisplay(DISPLAY_ORIENTATION_0);
9796 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9797 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9798
9799 NotifyMotionArgs motionArgs;
9800
9801 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9802 // First finger down.
9803 processId(mapper, FIRST_TRACKING_ID);
9804 processPosition(mapper, x1, y1);
9805 processSync(mapper);
9806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9807 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9809
9810 // Second finger down.
9811 processSlot(mapper, SECOND_SLOT);
9812 processId(mapper, SECOND_TRACKING_ID);
9813 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009814 processSync(mapper);
9815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009816 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009817 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9818
arthurhungcc7f9802020-04-30 17:55:40 +08009819 // If the tool type of the first finger changes to MT_TOOL_PALM,
9820 // we expect to receive ACTION_POINTER_UP with cancel flag.
9821 processSlot(mapper, FIRST_SLOT);
9822 processId(mapper, FIRST_TRACKING_ID);
9823 processToolType(mapper, MT_TOOL_PALM);
9824 processSync(mapper);
9825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009826 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009827 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9828
9829 // Second finger keeps moving.
9830 processSlot(mapper, SECOND_SLOT);
9831 processId(mapper, SECOND_TRACKING_ID);
9832 processPosition(mapper, x2 + 1, y2 + 1);
9833 processSync(mapper);
9834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9836
9837 // second finger becomes palm, receive cancel due to only 1 finger is active.
9838 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009839 processToolType(mapper, MT_TOOL_PALM);
9840 processSync(mapper);
9841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9842 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9843
arthurhungcc7f9802020-04-30 17:55:40 +08009844 // third finger down.
9845 processSlot(mapper, THIRD_SLOT);
9846 processId(mapper, THIRD_TRACKING_ID);
9847 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009848 processPosition(mapper, x3, y3);
9849 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9851 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009853 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9854
9855 // third finger move
9856 processId(mapper, THIRD_TRACKING_ID);
9857 processPosition(mapper, x3 + 1, y3 + 1);
9858 processSync(mapper);
9859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9861
9862 // first finger up, third finger receive move.
9863 processSlot(mapper, FIRST_SLOT);
9864 processId(mapper, INVALID_TRACKING_ID);
9865 processSync(mapper);
9866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9868 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9869
9870 // second finger up, third finger receive move.
9871 processSlot(mapper, SECOND_SLOT);
9872 processId(mapper, INVALID_TRACKING_ID);
9873 processSync(mapper);
9874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9876 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9877
9878 // third finger up.
9879 processSlot(mapper, THIRD_SLOT);
9880 processId(mapper, INVALID_TRACKING_ID);
9881 processSync(mapper);
9882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9883 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9884 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9885}
9886
9887/**
9888 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9889 * and the active finger could still be allowed to receive the events
9890 */
9891TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9892 addConfigurationProperty("touch.deviceType", "touchScreen");
9893 prepareDisplay(DISPLAY_ORIENTATION_0);
9894 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9895 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9896
9897 NotifyMotionArgs motionArgs;
9898
9899 // default tool type is finger
9900 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9901 processId(mapper, FIRST_TRACKING_ID);
9902 processPosition(mapper, x1, y1);
9903 processSync(mapper);
9904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9905 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9906 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9907
9908 // Second finger down.
9909 processSlot(mapper, SECOND_SLOT);
9910 processId(mapper, SECOND_TRACKING_ID);
9911 processPosition(mapper, x2, y2);
9912 processSync(mapper);
9913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009914 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009915 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9916
9917 // If the tool type of the second finger changes to MT_TOOL_PALM,
9918 // we expect to receive ACTION_POINTER_UP with cancel flag.
9919 processId(mapper, SECOND_TRACKING_ID);
9920 processToolType(mapper, MT_TOOL_PALM);
9921 processSync(mapper);
9922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009923 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009924 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9925
9926 // The following MOVE event should be processed.
9927 processSlot(mapper, FIRST_SLOT);
9928 processId(mapper, FIRST_TRACKING_ID);
9929 processPosition(mapper, x1 + 1, y1 + 1);
9930 processSync(mapper);
9931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9933 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9934
9935 // second finger up.
9936 processSlot(mapper, SECOND_SLOT);
9937 processId(mapper, INVALID_TRACKING_ID);
9938 processSync(mapper);
9939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9941
9942 // first finger keep moving
9943 processSlot(mapper, FIRST_SLOT);
9944 processId(mapper, FIRST_TRACKING_ID);
9945 processPosition(mapper, x1 + 2, y1 + 2);
9946 processSync(mapper);
9947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9949
9950 // first finger up.
9951 processId(mapper, INVALID_TRACKING_ID);
9952 processSync(mapper);
9953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9954 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9955 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009956}
9957
Arthur Hung9ad18942021-06-19 02:04:46 +00009958/**
9959 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9960 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9961 * cause slot be valid again.
9962 */
9963TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9964 addConfigurationProperty("touch.deviceType", "touchScreen");
9965 prepareDisplay(DISPLAY_ORIENTATION_0);
9966 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9967 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9968
9969 NotifyMotionArgs motionArgs;
9970
9971 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9972 // First finger down.
9973 processId(mapper, FIRST_TRACKING_ID);
9974 processPosition(mapper, x1, y1);
9975 processPressure(mapper, RAW_PRESSURE_MAX);
9976 processSync(mapper);
9977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9978 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9979 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9980
9981 // First finger move.
9982 processId(mapper, FIRST_TRACKING_ID);
9983 processPosition(mapper, x1 + 1, y1 + 1);
9984 processPressure(mapper, RAW_PRESSURE_MAX);
9985 processSync(mapper);
9986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9987 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9988 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9989
9990 // Second finger down.
9991 processSlot(mapper, SECOND_SLOT);
9992 processId(mapper, SECOND_TRACKING_ID);
9993 processPosition(mapper, x2, y2);
9994 processPressure(mapper, RAW_PRESSURE_MAX);
9995 processSync(mapper);
9996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009997 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009998 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9999
10000 // second finger up with some unexpected data.
10001 processSlot(mapper, SECOND_SLOT);
10002 processId(mapper, INVALID_TRACKING_ID);
10003 processPosition(mapper, x2, y2);
10004 processSync(mapper);
10005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010006 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010007 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10008
10009 // first finger up with some unexpected data.
10010 processSlot(mapper, FIRST_SLOT);
10011 processId(mapper, INVALID_TRACKING_ID);
10012 processPosition(mapper, x2, y2);
10013 processPressure(mapper, RAW_PRESSURE_MAX);
10014 processSync(mapper);
10015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10016 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10017 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10018}
10019
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010020TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10021 addConfigurationProperty("touch.deviceType", "touchScreen");
10022 prepareDisplay(DISPLAY_ORIENTATION_0);
10023 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10025
10026 // First finger down.
10027 processId(mapper, FIRST_TRACKING_ID);
10028 processPosition(mapper, 100, 200);
10029 processPressure(mapper, RAW_PRESSURE_MAX);
10030 processSync(mapper);
10031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10032 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10033
10034 // Second finger down.
10035 processSlot(mapper, SECOND_SLOT);
10036 processId(mapper, SECOND_TRACKING_ID);
10037 processPosition(mapper, 300, 400);
10038 processPressure(mapper, RAW_PRESSURE_MAX);
10039 processSync(mapper);
10040 ASSERT_NO_FATAL_FAILURE(
10041 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10042
10043 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010044 // preserved. Resetting should cancel the ongoing gesture.
10045 resetMapper(mapper, ARBITRARY_TIME);
10046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10047 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010048
10049 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10050 // the existing touch state to generate a down event.
10051 processPosition(mapper, 301, 302);
10052 processSync(mapper);
10053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10054 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10056 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10057
10058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10059}
10060
10061TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10062 addConfigurationProperty("touch.deviceType", "touchScreen");
10063 prepareDisplay(DISPLAY_ORIENTATION_0);
10064 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10065 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10066
10067 // First finger touches down and releases.
10068 processId(mapper, FIRST_TRACKING_ID);
10069 processPosition(mapper, 100, 200);
10070 processPressure(mapper, RAW_PRESSURE_MAX);
10071 processSync(mapper);
10072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10073 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10074 processId(mapper, INVALID_TRACKING_ID);
10075 processSync(mapper);
10076 ASSERT_NO_FATAL_FAILURE(
10077 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10078
10079 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10080 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010081 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10083
10084 // Send an empty sync frame. Since there are no pointers, no events are generated.
10085 processSync(mapper);
10086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10087}
10088
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010089// --- MultiTouchInputMapperTest_ExternalDevice ---
10090
10091class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10092protected:
Chris Yea52ade12020-08-27 16:49:20 -070010093 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010094};
10095
10096/**
10097 * Expect fallback to internal viewport if device is external and external viewport is not present.
10098 */
10099TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10100 prepareAxes(POSITION);
10101 addConfigurationProperty("touch.deviceType", "touchScreen");
10102 prepareDisplay(DISPLAY_ORIENTATION_0);
10103 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10104
10105 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10106
10107 NotifyMotionArgs motionArgs;
10108
10109 // Expect the event to be sent to the internal viewport,
10110 // because an external viewport is not present.
10111 processPosition(mapper, 100, 100);
10112 processSync(mapper);
10113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10114 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10115
10116 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010117 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010118 processPosition(mapper, 100, 100);
10119 processSync(mapper);
10120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10121 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10122}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010123
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010124TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10125 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10126 std::shared_ptr<FakePointerController> fakePointerController =
10127 std::make_shared<FakePointerController>();
10128 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10129 fakePointerController->setPosition(0, 0);
10130 fakePointerController->setButtonState(0);
10131
10132 // prepare device and capture
10133 prepareDisplay(DISPLAY_ORIENTATION_0);
10134 prepareAxes(POSITION | ID | SLOT);
10135 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10136 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10137 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010138 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010139 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10140
10141 // captured touchpad should be a touchpad source
10142 NotifyDeviceResetArgs resetArgs;
10143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10144 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10145
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010146 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010147
10148 const InputDeviceInfo::MotionRange* relRangeX =
10149 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10150 ASSERT_NE(relRangeX, nullptr);
10151 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10152 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10153 const InputDeviceInfo::MotionRange* relRangeY =
10154 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10155 ASSERT_NE(relRangeY, nullptr);
10156 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10157 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10158
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010159 // run captured pointer tests - note that this is unscaled, so input listener events should be
10160 // identical to what the hardware sends (accounting for any
10161 // calibration).
10162 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010163 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010164 processId(mapper, 1);
10165 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10166 processKey(mapper, BTN_TOUCH, 1);
10167 processSync(mapper);
10168
10169 // expect coord[0] to contain initial location of touch 0
10170 NotifyMotionArgs args;
10171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10172 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10173 ASSERT_EQ(1U, args.pointerCount);
10174 ASSERT_EQ(0, args.pointerProperties[0].id);
10175 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10176 ASSERT_NO_FATAL_FAILURE(
10177 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10178
10179 // FINGER 1 DOWN
10180 processSlot(mapper, 1);
10181 processId(mapper, 2);
10182 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10183 processSync(mapper);
10184
10185 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010187 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010188 ASSERT_EQ(2U, args.pointerCount);
10189 ASSERT_EQ(0, args.pointerProperties[0].id);
10190 ASSERT_EQ(1, args.pointerProperties[1].id);
10191 ASSERT_NO_FATAL_FAILURE(
10192 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10193 ASSERT_NO_FATAL_FAILURE(
10194 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10195
10196 // FINGER 1 MOVE
10197 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10198 processSync(mapper);
10199
10200 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10201 // from move
10202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10204 ASSERT_NO_FATAL_FAILURE(
10205 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10206 ASSERT_NO_FATAL_FAILURE(
10207 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10208
10209 // FINGER 0 MOVE
10210 processSlot(mapper, 0);
10211 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10212 processSync(mapper);
10213
10214 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10217 ASSERT_NO_FATAL_FAILURE(
10218 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10219 ASSERT_NO_FATAL_FAILURE(
10220 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10221
10222 // BUTTON DOWN
10223 processKey(mapper, BTN_LEFT, 1);
10224 processSync(mapper);
10225
10226 // touchinputmapper design sends a move before button press
10227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10231
10232 // BUTTON UP
10233 processKey(mapper, BTN_LEFT, 0);
10234 processSync(mapper);
10235
10236 // touchinputmapper design sends a move after button release
10237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10241
10242 // FINGER 0 UP
10243 processId(mapper, -1);
10244 processSync(mapper);
10245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10246 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10247
10248 // FINGER 1 MOVE
10249 processSlot(mapper, 1);
10250 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10251 processSync(mapper);
10252
10253 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10255 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10256 ASSERT_EQ(1U, args.pointerCount);
10257 ASSERT_EQ(1, args.pointerProperties[0].id);
10258 ASSERT_NO_FATAL_FAILURE(
10259 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10260
10261 // FINGER 1 UP
10262 processId(mapper, -1);
10263 processKey(mapper, BTN_TOUCH, 0);
10264 processSync(mapper);
10265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10266 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10267
Harry Cutts16a24cc2022-10-26 15:22:19 +000010268 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010269 mFakePolicy->setPointerCapture(false);
10270 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +000010272 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010273}
10274
10275TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10276 std::shared_ptr<FakePointerController> fakePointerController =
10277 std::make_shared<FakePointerController>();
10278 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10279 fakePointerController->setPosition(0, 0);
10280 fakePointerController->setButtonState(0);
10281
10282 // prepare device and capture
10283 prepareDisplay(DISPLAY_ORIENTATION_0);
10284 prepareAxes(POSITION | ID | SLOT);
10285 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10286 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010287 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010288 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10289 // run uncaptured pointer tests - pushes out generic events
10290 // FINGER 0 DOWN
10291 processId(mapper, 3);
10292 processPosition(mapper, 100, 100);
10293 processKey(mapper, BTN_TOUCH, 1);
10294 processSync(mapper);
10295
10296 // start at (100,100), cursor should be at (0,0) * scale
10297 NotifyMotionArgs args;
10298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10299 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10300 ASSERT_NO_FATAL_FAILURE(
10301 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10302
10303 // FINGER 0 MOVE
10304 processPosition(mapper, 200, 200);
10305 processSync(mapper);
10306
10307 // compute scaling to help with touch position checking
10308 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10309 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10310 float scale =
10311 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10312
10313 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10315 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10317 0, 0, 0, 0, 0, 0, 0));
10318}
10319
10320TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10321 std::shared_ptr<FakePointerController> fakePointerController =
10322 std::make_shared<FakePointerController>();
10323
10324 prepareDisplay(DISPLAY_ORIENTATION_0);
10325 prepareAxes(POSITION | ID | SLOT);
10326 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010327 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010328 mFakePolicy->setPointerCapture(false);
10329 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10330
Harry Cutts16a24cc2022-10-26 15:22:19 +000010331 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
10332 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010333
Harry Cutts16a24cc2022-10-26 15:22:19 +000010334 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010335 mFakePolicy->setPointerCapture(true);
10336 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10337 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10338}
10339
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010340// --- BluetoothMultiTouchInputMapperTest ---
10341
10342class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10343protected:
10344 void SetUp() override {
10345 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10346 }
10347};
10348
10349TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10350 addConfigurationProperty("touch.deviceType", "touchScreen");
10351 prepareDisplay(DISPLAY_ORIENTATION_0);
10352 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10353 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10354
10355 nsecs_t kernelEventTime = ARBITRARY_TIME;
10356 nsecs_t expectedEventTime = ARBITRARY_TIME;
10357 // Touch down.
10358 processId(mapper, FIRST_TRACKING_ID);
10359 processPosition(mapper, 100, 200);
10360 processPressure(mapper, RAW_PRESSURE_MAX);
10361 processSync(mapper, ARBITRARY_TIME);
10362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10363 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10364
10365 // Process several events that come in quick succession, according to their timestamps.
10366 for (int i = 0; i < 3; i++) {
10367 constexpr static nsecs_t delta = ms2ns(1);
10368 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10369 kernelEventTime += delta;
10370 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10371
10372 processPosition(mapper, 101 + i, 201 + i);
10373 processSync(mapper, kernelEventTime);
10374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10375 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10376 WithEventTime(expectedEventTime))));
10377 }
10378
10379 // Release the touch.
10380 processId(mapper, INVALID_TRACKING_ID);
10381 processPressure(mapper, RAW_PRESSURE_MIN);
10382 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10384 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10385 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10386}
10387
10388// --- MultiTouchPointerModeTest ---
10389
HQ Liue6983c72022-04-19 22:14:56 +000010390class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10391protected:
10392 float mPointerMovementScale;
10393 float mPointerXZoomScale;
10394 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10395 addConfigurationProperty("touch.deviceType", "pointer");
10396 std::shared_ptr<FakePointerController> fakePointerController =
10397 std::make_shared<FakePointerController>();
10398 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10399 fakePointerController->setPosition(0, 0);
10400 fakePointerController->setButtonState(0);
10401 prepareDisplay(DISPLAY_ORIENTATION_0);
10402
10403 prepareAxes(POSITION);
10404 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10405 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10406 // needs to be disabled, and the pointer gesture needs to be enabled.
10407 mFakePolicy->setPointerCapture(false);
10408 mFakePolicy->setPointerGestureEnabled(true);
10409 mFakePolicy->setPointerController(fakePointerController);
10410
10411 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10412 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10413 mPointerMovementScale =
10414 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10415 mPointerXZoomScale =
10416 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10417 }
10418
10419 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10420 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10421 /*flat*/ 0,
10422 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10423 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10424 /*flat*/ 0,
10425 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10426 }
10427};
10428
10429/**
10430 * Two fingers down on a pointer mode touch pad. The width
10431 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10432 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10433 * be greater than the both value to be freeform gesture, so that after two
10434 * fingers start to move downwards, the gesture should be swipe.
10435 */
10436TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10437 // The min freeform gesture width is 25units/mm x 30mm = 750
10438 // which is greater than fraction of the diagnal length of the touchpad (349).
10439 // Thus, MaxSwipWidth is 750.
10440 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10441 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10442 NotifyMotionArgs motionArgs;
10443
10444 // Two fingers down at once.
10445 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10446 // Pointer's initial position is used the [0,0] coordinate.
10447 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10448
10449 processId(mapper, FIRST_TRACKING_ID);
10450 processPosition(mapper, x1, y1);
10451 processMTSync(mapper);
10452 processId(mapper, SECOND_TRACKING_ID);
10453 processPosition(mapper, x2, y2);
10454 processMTSync(mapper);
10455 processSync(mapper);
10456
10457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10458 ASSERT_EQ(1U, motionArgs.pointerCount);
10459 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010461 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010462 ASSERT_NO_FATAL_FAILURE(
10463 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10464
10465 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10466 // that there should be 1 pointer.
10467 int32_t movingDistance = 200;
10468 y1 += movingDistance;
10469 y2 += movingDistance;
10470
10471 processId(mapper, FIRST_TRACKING_ID);
10472 processPosition(mapper, x1, y1);
10473 processMTSync(mapper);
10474 processId(mapper, SECOND_TRACKING_ID);
10475 processPosition(mapper, x2, y2);
10476 processMTSync(mapper);
10477 processSync(mapper);
10478
10479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10480 ASSERT_EQ(1U, motionArgs.pointerCount);
10481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010483 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10485 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10486 0, 0, 0, 0));
10487}
10488
10489/**
10490 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10491 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10492 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10493 * value to be freeform gesture, so that after two fingers start to move downwards,
10494 * the gesture should be swipe.
10495 */
10496TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10497 // The min freeform gesture width is 5units/mm x 30mm = 150
10498 // which is greater than fraction of the diagnal length of the touchpad (349).
10499 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10500 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10501 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10502 NotifyMotionArgs motionArgs;
10503
10504 // Two fingers down at once.
10505 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10506 // Pointer's initial position is used the [0,0] coordinate.
10507 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10508
10509 processId(mapper, FIRST_TRACKING_ID);
10510 processPosition(mapper, x1, y1);
10511 processMTSync(mapper);
10512 processId(mapper, SECOND_TRACKING_ID);
10513 processPosition(mapper, x2, y2);
10514 processMTSync(mapper);
10515 processSync(mapper);
10516
10517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10518 ASSERT_EQ(1U, motionArgs.pointerCount);
10519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010521 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010522 ASSERT_NO_FATAL_FAILURE(
10523 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10524
10525 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10526 // and there should be 1 pointer.
10527 int32_t movingDistance = 200;
10528 y1 += movingDistance;
10529 y2 += movingDistance;
10530
10531 processId(mapper, FIRST_TRACKING_ID);
10532 processPosition(mapper, x1, y1);
10533 processMTSync(mapper);
10534 processId(mapper, SECOND_TRACKING_ID);
10535 processPosition(mapper, x2, y2);
10536 processMTSync(mapper);
10537 processSync(mapper);
10538
10539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10540 ASSERT_EQ(1U, motionArgs.pointerCount);
10541 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010543 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010544 // New coordinate is the scaled relative coordinate from the initial coordinate.
10545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10546 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10547 0, 0, 0, 0));
10548}
10549
10550/**
10551 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10552 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10553 * freeform gestures after two fingers start to move downwards.
10554 */
10555TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10556 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10557 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10558
10559 NotifyMotionArgs motionArgs;
10560
10561 // Two fingers down at once. Wider than the max swipe width.
10562 // The gesture is expected to be PRESS, then transformed to FREEFORM
10563 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10564
10565 processId(mapper, FIRST_TRACKING_ID);
10566 processPosition(mapper, x1, y1);
10567 processMTSync(mapper);
10568 processId(mapper, SECOND_TRACKING_ID);
10569 processPosition(mapper, x2, y2);
10570 processMTSync(mapper);
10571 processSync(mapper);
10572
10573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10574 ASSERT_EQ(1U, motionArgs.pointerCount);
10575 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010577 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010578 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10579 ASSERT_NO_FATAL_FAILURE(
10580 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10581
10582 int32_t movingDistance = 200;
10583
10584 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10585 // then two down events for two pointers.
10586 y1 += movingDistance;
10587 y2 += movingDistance;
10588
10589 processId(mapper, FIRST_TRACKING_ID);
10590 processPosition(mapper, x1, y1);
10591 processMTSync(mapper);
10592 processId(mapper, SECOND_TRACKING_ID);
10593 processPosition(mapper, x2, y2);
10594 processMTSync(mapper);
10595 processSync(mapper);
10596
10597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10598 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10599 ASSERT_EQ(1U, motionArgs.pointerCount);
10600 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10603 ASSERT_EQ(1U, motionArgs.pointerCount);
10604 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010607 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010608 ASSERT_EQ(2U, motionArgs.pointerCount);
10609 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10610 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010611 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010612 // Two pointers' scaled relative coordinates from their initial centroid.
10613 // Initial y coordinates are 0 as y1 and y2 have the same value.
10614 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10615 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10616 // When pointers move, the new coordinates equal to the initial coordinates plus
10617 // scaled moving distance.
10618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10619 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10620 0, 0, 0, 0));
10621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10622 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10623 0, 0, 0, 0));
10624
10625 // Move two fingers down again, expect one MOVE motion event.
10626 y1 += movingDistance;
10627 y2 += movingDistance;
10628
10629 processId(mapper, FIRST_TRACKING_ID);
10630 processPosition(mapper, x1, y1);
10631 processMTSync(mapper);
10632 processId(mapper, SECOND_TRACKING_ID);
10633 processPosition(mapper, x2, y2);
10634 processMTSync(mapper);
10635 processSync(mapper);
10636
10637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10638 ASSERT_EQ(2U, motionArgs.pointerCount);
10639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10640 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010641 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10643 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10644 0, 0, 0, 0, 0));
10645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10646 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10647 0, 0, 0, 0, 0));
10648}
10649
Harry Cutts39b7ca22022-10-05 15:55:48 +000010650TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10651 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10652 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10653 NotifyMotionArgs motionArgs;
10654
10655 // Place two fingers down.
10656 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10657
10658 processId(mapper, FIRST_TRACKING_ID);
10659 processPosition(mapper, x1, y1);
10660 processMTSync(mapper);
10661 processId(mapper, SECOND_TRACKING_ID);
10662 processPosition(mapper, x2, y2);
10663 processMTSync(mapper);
10664 processSync(mapper);
10665
10666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10667 ASSERT_EQ(1U, motionArgs.pointerCount);
10668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10669 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10670 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10671 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10672
10673 // Move the two fingers down and to the left.
10674 int32_t movingDistance = 200;
10675 x1 -= movingDistance;
10676 y1 += movingDistance;
10677 x2 -= movingDistance;
10678 y2 += movingDistance;
10679
10680 processId(mapper, FIRST_TRACKING_ID);
10681 processPosition(mapper, x1, y1);
10682 processMTSync(mapper);
10683 processId(mapper, SECOND_TRACKING_ID);
10684 processPosition(mapper, x2, y2);
10685 processMTSync(mapper);
10686 processSync(mapper);
10687
10688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10689 ASSERT_EQ(1U, motionArgs.pointerCount);
10690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10691 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10692 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10693 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10694}
10695
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010696TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
10697 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10698 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10699 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10701
10702 // Start a stylus gesture.
10703 processKey(mapper, BTN_TOOL_PEN, 1);
10704 processId(mapper, FIRST_TRACKING_ID);
10705 processPosition(mapper, 100, 200);
10706 processSync(mapper);
10707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10708 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10709 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10710 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10711 // TODO(b/257078296): Pointer mode generates extra event.
10712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10713 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10714 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10715 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10717
10718 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10719 // gesture should be disabled.
10720 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10721 viewport->isActive = false;
10722 mFakePolicy->updateViewport(*viewport);
10723 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10725 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10726 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10727 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10728 // TODO(b/257078296): Pointer mode generates extra event.
10729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10730 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10731 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10732 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10734}
10735
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010736// --- JoystickInputMapperTest ---
10737
10738class JoystickInputMapperTest : public InputMapperTest {
10739protected:
10740 static const int32_t RAW_X_MIN;
10741 static const int32_t RAW_X_MAX;
10742 static const int32_t RAW_Y_MIN;
10743 static const int32_t RAW_Y_MAX;
10744
10745 void SetUp() override {
10746 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10747 }
10748 void prepareAxes() {
10749 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10750 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10751 }
10752
10753 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10755 }
10756
10757 void processSync(JoystickInputMapper& mapper) {
10758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10759 }
10760
10761 void prepareVirtualDisplay(int32_t orientation) {
10762 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10763 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10764 NO_PORT, ViewportType::VIRTUAL);
10765 }
10766};
10767
10768const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10769const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10770const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10771const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10772
10773TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10774 prepareAxes();
10775 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10776
10777 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10778
10779 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10780
10781 // Send an axis event
10782 processAxis(mapper, ABS_X, 100);
10783 processSync(mapper);
10784
10785 NotifyMotionArgs args;
10786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10787 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10788
10789 // Send another axis event
10790 processAxis(mapper, ABS_Y, 100);
10791 processSync(mapper);
10792
10793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10794 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10795}
10796
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010797// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010798
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010799class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010800protected:
10801 static const char* DEVICE_NAME;
10802 static const char* DEVICE_LOCATION;
10803 static const int32_t DEVICE_ID;
10804 static const int32_t DEVICE_GENERATION;
10805 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010806 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010807 static const int32_t EVENTHUB_ID;
10808
10809 std::shared_ptr<FakeEventHub> mFakeEventHub;
10810 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010811 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010812 std::unique_ptr<InstrumentedInputReader> mReader;
10813 std::shared_ptr<InputDevice> mDevice;
10814
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010815 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010816 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010817 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010818 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010819 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010820 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010821 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10822 }
10823
10824 void SetUp() override { SetUp(DEVICE_CLASSES); }
10825
10826 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010827 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010828 mFakePolicy.clear();
10829 }
10830
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010831 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010832 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10833 mReader->requestRefreshConfiguration(changes);
10834 mReader->loopOnce();
10835 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010836 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010837 }
10838
10839 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10840 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010841 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010842 InputDeviceIdentifier identifier;
10843 identifier.name = name;
10844 identifier.location = location;
10845 std::shared_ptr<InputDevice> device =
10846 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10847 identifier);
10848 mReader->pushNextDevice(device);
10849 mFakeEventHub->addDevice(eventHubId, name, classes);
10850 mReader->loopOnce();
10851 return device;
10852 }
10853
10854 template <class T, typename... Args>
10855 T& addControllerAndConfigure(Args... args) {
10856 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10857
10858 return controller;
10859 }
10860};
10861
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010862const char* PeripheralControllerTest::DEVICE_NAME = "device";
10863const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10864const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10865const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10866const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010867const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10868 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010869const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010870
10871// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010872class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010873protected:
10874 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010875 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010876 }
10877};
10878
10879TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010880 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010881
10882 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10883 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10884}
10885
10886TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010887 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010888
10889 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10890 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10891}
10892
10893// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010894class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010895protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010896 void SetUp() override {
10897 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10898 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010899};
10900
Chris Ye85758332021-05-16 23:05:17 -070010901TEST_F(LightControllerTest, MonoLight) {
10902 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010903 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010904 .maxBrightness = 255,
10905 .flags = InputLightClass::BRIGHTNESS,
10906 .path = ""};
10907 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010908
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010909 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010910 InputDeviceInfo info;
10911 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010912 std::vector<InputDeviceLightInfo> lights = info.getLights();
10913 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010914 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10915 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10916
10917 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10918 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10919}
10920
10921TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10922 RawLightInfo infoMono = {.id = 1,
10923 .name = "mono_keyboard_backlight",
10924 .maxBrightness = 255,
10925 .flags = InputLightClass::BRIGHTNESS |
10926 InputLightClass::KEYBOARD_BACKLIGHT,
10927 .path = ""};
10928 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10929
10930 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10931 InputDeviceInfo info;
10932 controller.populateDeviceInfo(&info);
10933 std::vector<InputDeviceLightInfo> lights = info.getLights();
10934 ASSERT_EQ(1U, lights.size());
10935 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10936 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010937
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010938 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10939 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010940}
10941
10942TEST_F(LightControllerTest, RGBLight) {
10943 RawLightInfo infoRed = {.id = 1,
10944 .name = "red",
10945 .maxBrightness = 255,
10946 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10947 .path = ""};
10948 RawLightInfo infoGreen = {.id = 2,
10949 .name = "green",
10950 .maxBrightness = 255,
10951 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10952 .path = ""};
10953 RawLightInfo infoBlue = {.id = 3,
10954 .name = "blue",
10955 .maxBrightness = 255,
10956 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10957 .path = ""};
10958 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10959 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10960 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10961
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010962 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010963 InputDeviceInfo info;
10964 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010965 std::vector<InputDeviceLightInfo> lights = info.getLights();
10966 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010967 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10968 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10969 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10970
10971 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10972 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10973}
10974
10975TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10976 RawLightInfo infoRed = {.id = 1,
10977 .name = "red_keyboard_backlight",
10978 .maxBrightness = 255,
10979 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10980 InputLightClass::KEYBOARD_BACKLIGHT,
10981 .path = ""};
10982 RawLightInfo infoGreen = {.id = 2,
10983 .name = "green_keyboard_backlight",
10984 .maxBrightness = 255,
10985 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10986 InputLightClass::KEYBOARD_BACKLIGHT,
10987 .path = ""};
10988 RawLightInfo infoBlue = {.id = 3,
10989 .name = "blue_keyboard_backlight",
10990 .maxBrightness = 255,
10991 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10992 InputLightClass::KEYBOARD_BACKLIGHT,
10993 .path = ""};
10994 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10995 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10996 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10997
10998 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10999 InputDeviceInfo info;
11000 controller.populateDeviceInfo(&info);
11001 std::vector<InputDeviceLightInfo> lights = info.getLights();
11002 ASSERT_EQ(1U, lights.size());
11003 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11004 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11005 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11006
11007 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11008 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11009}
11010
11011TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11012 RawLightInfo infoRed = {.id = 1,
11013 .name = "red",
11014 .maxBrightness = 255,
11015 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11016 .path = ""};
11017 RawLightInfo infoGreen = {.id = 2,
11018 .name = "green",
11019 .maxBrightness = 255,
11020 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11021 .path = ""};
11022 RawLightInfo infoBlue = {.id = 3,
11023 .name = "blue",
11024 .maxBrightness = 255,
11025 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11026 .path = ""};
11027 RawLightInfo infoGlobal = {.id = 3,
11028 .name = "global_keyboard_backlight",
11029 .maxBrightness = 255,
11030 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11031 InputLightClass::KEYBOARD_BACKLIGHT,
11032 .path = ""};
11033 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11034 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11035 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11036 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11037
11038 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11039 InputDeviceInfo info;
11040 controller.populateDeviceInfo(&info);
11041 std::vector<InputDeviceLightInfo> lights = info.getLights();
11042 ASSERT_EQ(1U, lights.size());
11043 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11044 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11045 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011046
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011047 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11048 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011049}
11050
11051TEST_F(LightControllerTest, MultiColorRGBLight) {
11052 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011053 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011054 .maxBrightness = 255,
11055 .flags = InputLightClass::BRIGHTNESS |
11056 InputLightClass::MULTI_INTENSITY |
11057 InputLightClass::MULTI_INDEX,
11058 .path = ""};
11059
11060 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11061
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011062 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011063 InputDeviceInfo info;
11064 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011065 std::vector<InputDeviceLightInfo> lights = info.getLights();
11066 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011067 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11068 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11069 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11070
11071 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11072 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11073}
11074
11075TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11076 RawLightInfo infoColor = {.id = 1,
11077 .name = "multi_color_keyboard_backlight",
11078 .maxBrightness = 255,
11079 .flags = InputLightClass::BRIGHTNESS |
11080 InputLightClass::MULTI_INTENSITY |
11081 InputLightClass::MULTI_INDEX |
11082 InputLightClass::KEYBOARD_BACKLIGHT,
11083 .path = ""};
11084
11085 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11086
11087 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11088 InputDeviceInfo info;
11089 controller.populateDeviceInfo(&info);
11090 std::vector<InputDeviceLightInfo> lights = info.getLights();
11091 ASSERT_EQ(1U, lights.size());
11092 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11093 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11094 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011095
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011096 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11097 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011098}
11099
11100TEST_F(LightControllerTest, PlayerIdLight) {
11101 RawLightInfo info1 = {.id = 1,
11102 .name = "player1",
11103 .maxBrightness = 255,
11104 .flags = InputLightClass::BRIGHTNESS,
11105 .path = ""};
11106 RawLightInfo info2 = {.id = 2,
11107 .name = "player2",
11108 .maxBrightness = 255,
11109 .flags = InputLightClass::BRIGHTNESS,
11110 .path = ""};
11111 RawLightInfo info3 = {.id = 3,
11112 .name = "player3",
11113 .maxBrightness = 255,
11114 .flags = InputLightClass::BRIGHTNESS,
11115 .path = ""};
11116 RawLightInfo info4 = {.id = 4,
11117 .name = "player4",
11118 .maxBrightness = 255,
11119 .flags = InputLightClass::BRIGHTNESS,
11120 .path = ""};
11121 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11122 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11123 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11124 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11125
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011126 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011127 InputDeviceInfo info;
11128 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011129 std::vector<InputDeviceLightInfo> lights = info.getLights();
11130 ASSERT_EQ(1U, lights.size());
11131 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011132 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11133 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011134
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011135 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11136 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11137 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011138}
11139
Michael Wrightd02c5b62014-02-10 15:10:22 -080011140} // namespace android