blob: 6a174d076ed1baae9c950f5377aa6f4f32566206 [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:
2382 InvalidUinputDevice() : UinputDevice("Invalid Device") {}
2383
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
Michael Wrightd02c5b62014-02-10 15:10:22 -08002834// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002835class InputDeviceTest : public testing::Test {
2836protected:
2837 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002838 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002839 static const int32_t DEVICE_ID;
2840 static const int32_t DEVICE_GENERATION;
2841 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002842 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002843 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002844 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002846 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002847 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002848 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002849 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002850 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851
Chris Yea52ade12020-08-27 16:49:20 -07002852 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002853 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002854 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002855 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002856 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002857 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002858 InputDeviceIdentifier identifier;
2859 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002860 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002861 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08002862 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002863 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002864 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002865 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002866 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002867 }
2868
Chris Yea52ade12020-08-27 16:49:20 -07002869 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002870 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002871 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872 }
2873};
2874
2875const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002876const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002877const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2879const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002880const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002881 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002882const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002883const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002884
2885TEST_F(InputDeviceTest, ImmutableProperties) {
2886 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002887 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002888 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889}
2890
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002891TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2892 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2893
2894 // Configuration
2895 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2896 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002897 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002898
2899 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2900}
2901
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002902TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2903 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002904}
2905
Michael Wrightd02c5b62014-02-10 15:10:22 -08002906TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2907 // Configuration.
2908 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002909 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910
2911 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002912 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002913
2914 NotifyDeviceResetArgs resetArgs;
2915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2916 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2917 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2918
2919 // Metadata.
2920 ASSERT_TRUE(mDevice->isIgnored());
2921 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2922
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002923 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002924 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002925 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2927 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2928
2929 // State queries.
2930 ASSERT_EQ(0, mDevice->getMetaState());
2931
2932 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2933 << "Ignored device should return unknown key code state.";
2934 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2935 << "Ignored device should return unknown scan code state.";
2936 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2937 << "Ignored device should return unknown switch state.";
2938
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002939 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002940 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002941 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002942 << "Ignored device should never mark any key codes.";
2943 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2944 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2945}
2946
2947TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2948 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002949 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002950
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002951 FakeInputMapper& mapper1 =
2952 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002953 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2954 mapper1.setMetaState(AMETA_ALT_ON);
2955 mapper1.addSupportedKeyCode(AKEYCODE_A);
2956 mapper1.addSupportedKeyCode(AKEYCODE_B);
2957 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2958 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2959 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2960 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2961 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002963 FakeInputMapper& mapper2 =
2964 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002965 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002966
2967 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002968 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002969
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002970 std::string propertyValue;
2971 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002973 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002975 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2976 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002977
2978 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002979 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002980 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2981 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982
2983 NotifyDeviceResetArgs resetArgs;
2984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2985 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2986 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2987
2988 // Metadata.
2989 ASSERT_FALSE(mDevice->isIgnored());
2990 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2991
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002992 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002993 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002994 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002995 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2996 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2997
2998 // State queries.
2999 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
3000 << "Should query mappers and combine meta states.";
3001
3002 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3003 << "Should return unknown key code state when source not supported.";
3004 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3005 << "Should return unknown scan code state when source not supported.";
3006 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3007 << "Should return unknown switch state when source not supported.";
3008
3009 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
3010 << "Should query mapper when source is supported.";
3011 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
3012 << "Should query mapper when source is supported.";
3013 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
3014 << "Should query mapper when source is supported.";
3015
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003016 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08003017 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003018 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 << "Should do nothing when source is unsupported.";
3020 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
3021 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3022 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
3023 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3024
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003025 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 << "Should query mapper when source is supported.";
3027 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3028 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
3029 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3030 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3031
3032 // Event handling.
3033 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003034 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003035 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003036
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003037 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3038 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003039}
3040
Arthur Hung2c9a3342019-07-23 14:18:59 +08003041// A single input device is associated with a specific display. Check that:
3042// 1. Device is disabled if the viewport corresponding to the associated display is not found
3043// 2. Device is disabled when setEnabled API is called
3044TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003045 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003046
3047 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003048 std::list<NotifyArgs> unused =
3049 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003050
3051 // Device should be enabled by default.
3052 ASSERT_TRUE(mDevice->isEnabled());
3053
3054 // Prepare associated info.
3055 constexpr uint8_t hdmi = 1;
3056 const std::string UNIQUE_ID = "local:1";
3057
3058 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003059 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3060 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003061 // Device should be disabled because it is associated with a specific display via
3062 // input port <-> display port association, but the corresponding display is not found
3063 ASSERT_FALSE(mDevice->isEnabled());
3064
3065 // Prepare displays.
3066 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003067 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3068 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003069 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3070 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003071 ASSERT_TRUE(mDevice->isEnabled());
3072
3073 // Device should be disabled after set disable.
3074 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003075 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3076 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003077 ASSERT_FALSE(mDevice->isEnabled());
3078
3079 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003080 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3081 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003082 ASSERT_FALSE(mDevice->isEnabled());
3083}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003084
Christine Franks1ba71cc2021-04-07 14:37:42 -07003085TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
3086 // Device should be enabled by default.
3087 mFakePolicy->clearViewports();
3088 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003089 std::list<NotifyArgs> unused =
3090 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003091 ASSERT_TRUE(mDevice->isEnabled());
3092
3093 // Device should be disabled because it is associated with a specific display, but the
3094 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08003095 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003096 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3097 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003098 ASSERT_FALSE(mDevice->isEnabled());
3099
3100 // Device should be enabled when a display is found.
3101 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3102 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3103 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003104 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3105 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003106 ASSERT_TRUE(mDevice->isEnabled());
3107
3108 // Device should be disabled after set disable.
3109 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003110 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3111 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003112 ASSERT_FALSE(mDevice->isEnabled());
3113
3114 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003115 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3116 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07003117 ASSERT_FALSE(mDevice->isEnabled());
3118}
3119
Christine Franks2a2293c2022-01-18 11:51:16 -08003120TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3121 mFakePolicy->clearViewports();
3122 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003123 std::list<NotifyArgs> unused =
3124 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08003125
Christine Franks2a2293c2022-01-18 11:51:16 -08003126 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
3127 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3128 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
3129 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003130 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3131 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08003132 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3133}
3134
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003135/**
3136 * This test reproduces a crash caused by a dangling reference that remains after device is added
3137 * and removed. The reference is accessed in InputDevice::dump(..);
3138 */
3139TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3140 constexpr int32_t TEST_EVENTHUB_ID = 10;
3141 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
3142
3143 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
3144 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
3145 device.removeEventHubDevice(TEST_EVENTHUB_ID);
3146 std::string dumpStr, eventHubDevStr;
3147 device.dump(dumpStr, eventHubDevStr);
3148}
3149
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003150TEST_F(InputDeviceTest, GetBluetoothAddress) {
3151 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3152 ASSERT_TRUE(address);
3153 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3154}
3155
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156// --- InputMapperTest ---
3157
3158class InputMapperTest : public testing::Test {
3159protected:
3160 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003161 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 static const int32_t DEVICE_ID;
3163 static const int32_t DEVICE_GENERATION;
3164 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003165 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003166 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003168 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003170 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08003171 std::unique_ptr<InstrumentedInputReader> mReader;
3172 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003174 virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003175 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003176 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003177 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08003178 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003179 *mFakeListener);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003180 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes, bus);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003181 // Consume the device reset notification generated when adding a new device.
3182 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003183 }
3184
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003185 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003186 SetUp(DEVICE_CLASSES);
3187 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003188
Chris Yea52ade12020-08-27 16:49:20 -07003189 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003190 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003191 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192 }
3193
3194 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003195 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196 }
3197
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003198 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003199 if (!changes ||
3200 (changes &
3201 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3202 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003203 mReader->requestRefreshConfiguration(changes);
3204 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003205 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003206 std::list<NotifyArgs> out =
3207 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003208 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003209 for (const NotifyArgs& args : out) {
3210 mFakeListener->notify(args);
3211 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003212 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003213 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003214 }
3215
arthurhungdcef2dc2020-08-11 14:47:50 +08003216 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
3217 const std::string& location, int32_t eventHubId,
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003218 ftl::Flags<InputDeviceClass> classes, int bus = 0) {
arthurhungdcef2dc2020-08-11 14:47:50 +08003219 InputDeviceIdentifier identifier;
3220 identifier.name = name;
3221 identifier.location = location;
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003222 identifier.bus = bus;
arthurhungdcef2dc2020-08-11 14:47:50 +08003223 std::shared_ptr<InputDevice> device =
3224 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3225 identifier);
3226 mReader->pushNextDevice(device);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003227 mFakeEventHub->addDevice(eventHubId, name, classes, bus);
arthurhungdcef2dc2020-08-11 14:47:50 +08003228 mReader->loopOnce();
3229 return device;
3230 }
3231
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003232 template <class T, typename... Args>
3233 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003234 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003235 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003236 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
3237 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003238 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003239 for (const NotifyArgs& loopArgs : resetArgList) {
3240 mFakeListener->notify(loopArgs);
3241 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003242 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003243 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244 }
3245
3246 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003247 int32_t orientation, const std::string& uniqueId,
3248 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003249 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3250 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07003251 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3252 }
3253
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003254 void clearViewports() {
3255 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003256 }
3257
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003258 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3259 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 RawEvent event;
3261 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003262 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003263 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264 event.type = type;
3265 event.code = code;
3266 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003267 std::list<NotifyArgs> processArgList = mapper.process(&event);
3268 for (const NotifyArgs& args : processArgList) {
3269 mFakeListener->notify(args);
3270 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003271 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003272 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003273 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274 }
3275
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003276 void resetMapper(InputMapper& mapper, nsecs_t when) {
3277 const auto resetArgs = mapper.reset(when);
3278 for (const auto args : resetArgs) {
3279 mFakeListener->notify(args);
3280 }
3281 // Loop the reader to flush the input listener queue.
3282 mReader->loopOnce();
3283 }
3284
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 static void assertMotionRange(const InputDeviceInfo& info,
3286 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3287 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003288 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3290 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3291 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3292 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3293 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3294 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3295 }
3296
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003297 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3298 float size, float touchMajor, float touchMinor, float toolMajor,
3299 float toolMinor, float orientation, float distance,
3300 float scaledAxisEpsilon = 1.f) {
3301 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3302 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3304 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003305 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3306 scaledAxisEpsilon);
3307 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3308 scaledAxisEpsilon);
3309 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3310 scaledAxisEpsilon);
3311 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3312 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3314 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3315 }
3316
Michael Wright17db18e2020-06-26 20:51:44 +01003317 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003319 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320 ASSERT_NEAR(x, actualX, 1);
3321 ASSERT_NEAR(y, actualY, 1);
3322 }
3323};
3324
3325const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003326const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003327const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3329const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003330const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3331 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333
3334// --- SwitchInputMapperTest ---
3335
3336class SwitchInputMapperTest : public InputMapperTest {
3337protected:
3338};
3339
3340TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003341 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003343 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344}
3345
3346TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003347 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003349 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003350 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003351
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003352 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003353 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003354}
3355
3356TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003357 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003358 std::list<NotifyArgs> out;
3359 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3360 ASSERT_TRUE(out.empty());
3361 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3362 ASSERT_TRUE(out.empty());
3363 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3364 ASSERT_TRUE(out.empty());
3365 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003366
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003367 ASSERT_EQ(1u, out.size());
3368 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003369 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003370 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3371 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372 args.switchMask);
3373 ASSERT_EQ(uint32_t(0), args.policyFlags);
3374}
3375
Chris Ye87143712020-11-10 05:05:58 +00003376// --- VibratorInputMapperTest ---
3377class VibratorInputMapperTest : public InputMapperTest {
3378protected:
3379 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3380};
3381
3382TEST_F(VibratorInputMapperTest, GetSources) {
3383 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3384
3385 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3386}
3387
3388TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3389 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3390
3391 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3392}
3393
3394TEST_F(VibratorInputMapperTest, Vibrate) {
3395 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003396 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003397 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3398
3399 VibrationElement pattern(2);
3400 VibrationSequence sequence(2);
3401 pattern.duration = std::chrono::milliseconds(200);
3402 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3403 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3404 sequence.addElement(pattern);
3405 pattern.duration = std::chrono::milliseconds(500);
3406 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3407 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3408 sequence.addElement(pattern);
3409
3410 std::vector<int64_t> timings = {0, 1};
3411 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3412
3413 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003414 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003415 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003416 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003417 // Verify vibrator state listener was notified.
3418 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003419 ASSERT_EQ(1u, out.size());
3420 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3421 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3422 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003423 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003424 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003425 ASSERT_FALSE(mapper.isVibrating());
3426 // Verify vibrator state listener was notified.
3427 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003428 ASSERT_EQ(1u, out.size());
3429 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3430 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3431 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003432}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003433
Chris Yef59a2f42020-10-16 12:55:26 -07003434// --- SensorInputMapperTest ---
3435
3436class SensorInputMapperTest : public InputMapperTest {
3437protected:
3438 static const int32_t ACCEL_RAW_MIN;
3439 static const int32_t ACCEL_RAW_MAX;
3440 static const int32_t ACCEL_RAW_FUZZ;
3441 static const int32_t ACCEL_RAW_FLAT;
3442 static const int32_t ACCEL_RAW_RESOLUTION;
3443
3444 static const int32_t GYRO_RAW_MIN;
3445 static const int32_t GYRO_RAW_MAX;
3446 static const int32_t GYRO_RAW_FUZZ;
3447 static const int32_t GYRO_RAW_FLAT;
3448 static const int32_t GYRO_RAW_RESOLUTION;
3449
3450 static const float GRAVITY_MS2_UNIT;
3451 static const float DEGREE_RADIAN_UNIT;
3452
3453 void prepareAccelAxes();
3454 void prepareGyroAxes();
3455 void setAccelProperties();
3456 void setGyroProperties();
3457 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3458};
3459
3460const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3461const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3462const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3463const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3464const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3465
3466const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3467const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3468const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3469const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3470const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3471
3472const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3473const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3474
3475void SensorInputMapperTest::prepareAccelAxes() {
3476 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3477 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3478 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3479 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3480 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3481 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3482}
3483
3484void SensorInputMapperTest::prepareGyroAxes() {
3485 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3486 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3487 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3488 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3489 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3490 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3491}
3492
3493void SensorInputMapperTest::setAccelProperties() {
3494 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3495 /* sensorDataIndex */ 0);
3496 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3497 /* sensorDataIndex */ 1);
3498 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3499 /* sensorDataIndex */ 2);
3500 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3501 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3502 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3503 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3504 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3505}
3506
3507void SensorInputMapperTest::setGyroProperties() {
3508 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3509 /* sensorDataIndex */ 0);
3510 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3511 /* sensorDataIndex */ 1);
3512 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3513 /* sensorDataIndex */ 2);
3514 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3515 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3516 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3517 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3518 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3519}
3520
3521TEST_F(SensorInputMapperTest, GetSources) {
3522 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3523
3524 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3525}
3526
3527TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3528 setAccelProperties();
3529 prepareAccelAxes();
3530 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3531
3532 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3533 std::chrono::microseconds(10000),
3534 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003535 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3538 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003541
3542 NotifySensorArgs args;
3543 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3544 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3545 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3546
3547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3548 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3549 ASSERT_EQ(args.deviceId, DEVICE_ID);
3550 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3551 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3552 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3553 ASSERT_EQ(args.values, values);
3554 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3555}
3556
3557TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3558 setGyroProperties();
3559 prepareGyroAxes();
3560 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3561
3562 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3563 std::chrono::microseconds(10000),
3564 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003565 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003571
3572 NotifySensorArgs args;
3573 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3574 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3575 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3576
3577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3578 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3579 ASSERT_EQ(args.deviceId, DEVICE_ID);
3580 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3581 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3582 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3583 ASSERT_EQ(args.values, values);
3584 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3585}
3586
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587// --- KeyboardInputMapperTest ---
3588
3589class KeyboardInputMapperTest : public InputMapperTest {
3590protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003591 const std::string UNIQUE_ID = "local:0";
3592
3593 void prepareDisplay(int32_t orientation);
3594
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003595 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003596 int32_t originalKeyCode, int32_t rotatedKeyCode,
3597 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003598};
3599
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003600/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3601 * orientation.
3602 */
3603void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003604 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3605 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003606}
3607
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003608void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003609 int32_t originalScanCode, int32_t originalKeyCode,
3610 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003611 NotifyKeyArgs args;
3612
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3615 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3616 ASSERT_EQ(originalScanCode, args.scanCode);
3617 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003618 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003619
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3622 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3623 ASSERT_EQ(originalScanCode, args.scanCode);
3624 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003625 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003626}
3627
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003629 KeyboardInputMapper& mapper =
3630 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3631 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003633 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634}
3635
3636TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3637 const int32_t USAGE_A = 0x070004;
3638 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003639 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3640 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003641 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3642 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3643 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003645 KeyboardInputMapper& mapper =
3646 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3647 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003648 // Initial metastate is AMETA_NONE.
3649 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650
3651 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003653 NotifyKeyArgs args;
3654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3655 ASSERT_EQ(DEVICE_ID, args.deviceId);
3656 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3657 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3658 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3659 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3660 ASSERT_EQ(KEY_HOME, args.scanCode);
3661 ASSERT_EQ(AMETA_NONE, args.metaState);
3662 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3663 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3664 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3665
3666 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003667 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3669 ASSERT_EQ(DEVICE_ID, args.deviceId);
3670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3671 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3672 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3673 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3674 ASSERT_EQ(KEY_HOME, args.scanCode);
3675 ASSERT_EQ(AMETA_NONE, args.metaState);
3676 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3677 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3678 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3679
3680 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003681 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3684 ASSERT_EQ(DEVICE_ID, args.deviceId);
3685 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3686 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3687 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3688 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3689 ASSERT_EQ(0, args.scanCode);
3690 ASSERT_EQ(AMETA_NONE, args.metaState);
3691 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3692 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3693 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3694
3695 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3697 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3699 ASSERT_EQ(DEVICE_ID, args.deviceId);
3700 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3701 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3702 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3703 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3704 ASSERT_EQ(0, args.scanCode);
3705 ASSERT_EQ(AMETA_NONE, args.metaState);
3706 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3707 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3708 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3709
3710 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3714 ASSERT_EQ(DEVICE_ID, args.deviceId);
3715 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3716 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3717 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3718 ASSERT_EQ(0, args.keyCode);
3719 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3720 ASSERT_EQ(AMETA_NONE, args.metaState);
3721 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3722 ASSERT_EQ(0U, args.policyFlags);
3723 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3724
3725 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3727 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3729 ASSERT_EQ(DEVICE_ID, args.deviceId);
3730 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3731 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3732 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3733 ASSERT_EQ(0, args.keyCode);
3734 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3735 ASSERT_EQ(AMETA_NONE, args.metaState);
3736 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3737 ASSERT_EQ(0U, args.policyFlags);
3738 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3739}
3740
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003741/**
3742 * Ensure that the readTime is set to the time when the EV_KEY is received.
3743 */
3744TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3745 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3746
3747 KeyboardInputMapper& mapper =
3748 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3749 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3750 NotifyKeyArgs args;
3751
3752 // Key down
3753 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3755 ASSERT_EQ(12, args.readTime);
3756
3757 // Key up
3758 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3760 ASSERT_EQ(15, args.readTime);
3761}
3762
Michael Wrightd02c5b62014-02-10 15:10:22 -08003763TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003764 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3765 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003766 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3767 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3768 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003770 KeyboardInputMapper& mapper =
3771 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3772 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
Arthur Hung95f68612022-04-07 14:08:22 +08003774 // Initial metastate is AMETA_NONE.
3775 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003776
3777 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003779 NotifyKeyArgs args;
3780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3781 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003782 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003783 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003784
3785 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003786 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3788 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790
3791 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003792 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3794 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796
3797 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003798 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3800 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003801 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003802 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803}
3804
3805TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003806 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3807 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3808 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3809 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003811 KeyboardInputMapper& mapper =
3812 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3813 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003815 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003816 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3817 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3818 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3819 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3820 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3821 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3822 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3823 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3824}
3825
3826TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003827 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3828 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3829 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3830 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003831
Michael Wrightd02c5b62014-02-10 15:10:22 -08003832 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003833 KeyboardInputMapper& mapper =
3834 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3835 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003837 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003838 ASSERT_NO_FATAL_FAILURE(
3839 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3840 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3841 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3842 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3843 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3844 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3845 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003846
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003847 clearViewports();
3848 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003849 ASSERT_NO_FATAL_FAILURE(
3850 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3851 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3852 AKEYCODE_DPAD_UP, DISPLAY_ID));
3853 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3854 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3855 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3856 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003857
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003858 clearViewports();
3859 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003860 ASSERT_NO_FATAL_FAILURE(
3861 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3862 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3863 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3864 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3865 AKEYCODE_DPAD_UP, DISPLAY_ID));
3866 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3867 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003869 clearViewports();
3870 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003871 ASSERT_NO_FATAL_FAILURE(
3872 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3873 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3874 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3875 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3876 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3877 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3878 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879
3880 // Special case: if orientation changes while key is down, we still emit the same keycode
3881 // in the key up as we did in the key down.
3882 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003883 clearViewports();
3884 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3887 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3888 ASSERT_EQ(KEY_UP, args.scanCode);
3889 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3890
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003891 clearViewports();
3892 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3895 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3896 ASSERT_EQ(KEY_UP, args.scanCode);
3897 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3898}
3899
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003900TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3901 // If the keyboard is not orientation aware,
3902 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003903 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003905 KeyboardInputMapper& mapper =
3906 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3907 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003908 NotifyKeyArgs args;
3909
3910 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003913 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3915 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3916
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003917 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3922 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3923}
3924
3925TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3926 // If the keyboard is orientation aware,
3927 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003928 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003929
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003930 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003931 KeyboardInputMapper& mapper =
3932 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3933 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003934 NotifyKeyArgs args;
3935
3936 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3937 // ^--- already checked by the previous test
3938
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003939 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003940 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3945 ASSERT_EQ(DISPLAY_ID, args.displayId);
3946
3947 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003948 clearViewports();
3949 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003950 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3955 ASSERT_EQ(newDisplayId, args.displayId);
3956}
3957
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003959 KeyboardInputMapper& mapper =
3960 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3961 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003963 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003964 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003965
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003966 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003967 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968}
3969
Philip Junker4af3b3d2021-12-14 10:36:55 +01003970TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3971 KeyboardInputMapper& mapper =
3972 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3973 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3974
3975 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3976 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3977 << "If a mapping is available, the result is equal to the mapping";
3978
3979 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3980 << "If no mapping is available, the result is the key location";
3981}
3982
Michael Wrightd02c5b62014-02-10 15:10:22 -08003983TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003984 KeyboardInputMapper& mapper =
3985 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3986 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003987
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003988 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003989 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003991 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003992 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003993}
3994
3995TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003996 KeyboardInputMapper& mapper =
3997 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3998 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004000 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004001
Michael Wrightd02c5b62014-02-10 15:10:22 -08004002 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004003 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004004 ASSERT_TRUE(flags[0]);
4005 ASSERT_FALSE(flags[1]);
4006}
4007
4008TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004009 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4010 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4011 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4012 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4013 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4014 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004016 KeyboardInputMapper& mapper =
4017 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4018 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004019 // Initial metastate is AMETA_NONE.
4020 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021
4022 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004023 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4024 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4025 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026
4027 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004030 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4031 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4032 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004033 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004034
4035 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004038 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4039 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4040 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004041 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042
4043 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004044 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004046 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4047 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4048 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004049 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004050
4051 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004054 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4055 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4056 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004057 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058
4059 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4061 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004062 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4063 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4064 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004065 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066
4067 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004070 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4071 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4072 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004073 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074}
4075
Chris Yea52ade12020-08-27 16:49:20 -07004076TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4077 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4078 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4079 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4080 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4081
4082 KeyboardInputMapper& mapper =
4083 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4084 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4085
Chris Yea52ade12020-08-27 16:49:20 -07004086 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004087 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004088 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4089 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4090 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4091 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4092
4093 NotifyKeyArgs args;
4094 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4097 ASSERT_EQ(AMETA_NONE, args.metaState);
4098 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4099 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4100 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4101
4102 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004103 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4105 ASSERT_EQ(AMETA_NONE, args.metaState);
4106 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4107 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4108 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4109}
4110
Arthur Hung2c9a3342019-07-23 14:18:59 +08004111TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4112 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004113 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4114 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4115 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4116 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004117
4118 // keyboard 2.
4119 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004120 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004121 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004122 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004123 std::shared_ptr<InputDevice> device2 =
4124 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004125 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004126
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004127 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4128 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4129 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4130 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004131
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004132 KeyboardInputMapper& mapper =
4133 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4134 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004136 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004137 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004138 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004139 std::list<NotifyArgs> unused =
4140 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4141 0 /*changes*/);
4142 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004143
4144 // Prepared displays and associated info.
4145 constexpr uint8_t hdmi1 = 0;
4146 constexpr uint8_t hdmi2 = 1;
4147 const std::string SECONDARY_UNIQUE_ID = "local:1";
4148
4149 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4150 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4151
4152 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004153 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4154 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004155 ASSERT_FALSE(device2->isEnabled());
4156
4157 // Prepare second display.
4158 constexpr int32_t newDisplayId = 2;
4159 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004160 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004161 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004162 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004163 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004164 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4165 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004166
4167 // Device should be enabled after the associated display is found.
4168 ASSERT_TRUE(mDevice->isEnabled());
4169 ASSERT_TRUE(device2->isEnabled());
4170
4171 // Test pad key events
4172 ASSERT_NO_FATAL_FAILURE(
4173 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4174 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4175 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4176 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4177 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4178 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4179 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4180
4181 ASSERT_NO_FATAL_FAILURE(
4182 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4183 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4184 AKEYCODE_DPAD_RIGHT, newDisplayId));
4185 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4186 AKEYCODE_DPAD_DOWN, newDisplayId));
4187 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4188 AKEYCODE_DPAD_LEFT, newDisplayId));
4189}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190
arthurhungc903df12020-08-11 15:08:42 +08004191TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4192 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4193 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4194 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4195 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4196 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4197 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4198
4199 KeyboardInputMapper& mapper =
4200 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4201 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004202 // Initial metastate is AMETA_NONE.
4203 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004204
4205 // Initialization should have turned all of the lights off.
4206 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4207 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4208 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4209
4210 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004213 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4214 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4215
4216 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004219 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4220 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4221
4222 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004225 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4226 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4227
4228 mFakeEventHub->removeDevice(EVENTHUB_ID);
4229 mReader->loopOnce();
4230
4231 // keyboard 2 should default toggle keys.
4232 const std::string USB2 = "USB2";
4233 const std::string DEVICE_NAME2 = "KEYBOARD2";
4234 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4235 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4236 std::shared_ptr<InputDevice> device2 =
4237 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004238 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004239 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4240 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4241 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4242 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4243 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4244 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4245
arthurhung6fe95782020-10-05 22:41:16 +08004246 KeyboardInputMapper& mapper2 =
4247 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4248 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004249 std::list<NotifyArgs> unused =
4250 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4251 0 /*changes*/);
4252 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004253
4254 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4255 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4256 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004257 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4258 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004259}
4260
Arthur Hungcb40a002021-08-03 14:31:01 +00004261TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4262 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4263 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4264 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4265
4266 // Suppose we have two mappers. (DPAD + KEYBOARD)
4267 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4268 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4269 KeyboardInputMapper& mapper =
4270 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4271 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004272 // Initial metastate is AMETA_NONE.
4273 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004274
4275 mReader->toggleCapsLockState(DEVICE_ID);
4276 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4277}
4278
Arthur Hungfb3cc112022-04-13 07:39:50 +00004279TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4280 // keyboard 1.
4281 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4282 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4283 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4284 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4285 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4287
4288 KeyboardInputMapper& mapper1 =
4289 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4290 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4291
4292 // keyboard 2.
4293 const std::string USB2 = "USB2";
4294 const std::string DEVICE_NAME2 = "KEYBOARD2";
4295 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4296 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4297 std::shared_ptr<InputDevice> device2 =
4298 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4299 ftl::Flags<InputDeviceClass>(0));
4300 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4301 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4302 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4303 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4304 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4305 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4306
4307 KeyboardInputMapper& mapper2 =
4308 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4309 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004310 std::list<NotifyArgs> unused =
4311 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4312 0 /*changes*/);
4313 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004314
Arthur Hung95f68612022-04-07 14:08:22 +08004315 // Initial metastate is AMETA_NONE.
4316 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4317 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4318
4319 // Toggle num lock on and off.
4320 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4321 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004322 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4323 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4324 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4325
4326 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4327 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4328 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4329 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4330 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4331
4332 // Toggle caps lock on and off.
4333 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4334 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4335 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4336 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4337 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4338
4339 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4340 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4341 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4342 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4343 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4344
4345 // Toggle scroll lock on and off.
4346 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4347 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4348 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4349 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4350 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4351
4352 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4353 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4354 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4355 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4356 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4357}
4358
Arthur Hung2141d542022-08-23 07:45:21 +00004359TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4360 const int32_t USAGE_A = 0x070004;
4361 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4362 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4363
4364 KeyboardInputMapper& mapper =
4365 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4366 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4367 // Key down by scan code.
4368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4369 NotifyKeyArgs args;
4370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4371 ASSERT_EQ(DEVICE_ID, args.deviceId);
4372 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4373 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4374 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4375 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4376 ASSERT_EQ(KEY_HOME, args.scanCode);
4377 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4378
4379 // Disable device, it should synthesize cancellation events for down events.
4380 mFakePolicy->addDisabledDevice(DEVICE_ID);
4381 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4382
4383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4384 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4385 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4386 ASSERT_EQ(KEY_HOME, args.scanCode);
4387 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4388}
4389
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004390// --- KeyboardInputMapperTest_ExternalDevice ---
4391
4392class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4393protected:
Chris Yea52ade12020-08-27 16:49:20 -07004394 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004395};
4396
4397TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004398 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4399 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004400
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004401 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4402 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4403 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4404 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004405
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004406 KeyboardInputMapper& mapper =
4407 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4408 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004409
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004411 NotifyKeyArgs args;
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4413 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4414
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004415 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4417 ASSERT_EQ(uint32_t(0), args.policyFlags);
4418
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4421 ASSERT_EQ(uint32_t(0), args.policyFlags);
4422
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004423 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4425 ASSERT_EQ(uint32_t(0), args.policyFlags);
4426
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4429 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4430
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004431 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4433 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4434}
4435
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004436TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004437 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004438
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004439 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4440 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4441 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004442
Powei Fengd041c5d2019-05-03 17:11:33 -07004443 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004444 KeyboardInputMapper& mapper =
4445 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4446 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004447
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004449 NotifyKeyArgs args;
4450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4451 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4452
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004453 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4455 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4456
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4459 ASSERT_EQ(uint32_t(0), args.policyFlags);
4460
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004461 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4463 ASSERT_EQ(uint32_t(0), args.policyFlags);
4464
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4467 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4468
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004469 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4471 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4472}
4473
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474// --- CursorInputMapperTest ---
4475
4476class CursorInputMapperTest : public InputMapperTest {
4477protected:
4478 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4479
Michael Wright17db18e2020-06-26 20:51:44 +01004480 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481
Chris Yea52ade12020-08-27 16:49:20 -07004482 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483 InputMapperTest::SetUp();
4484
Michael Wright17db18e2020-06-26 20:51:44 +01004485 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004486 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487 }
4488
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004489 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4490 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004491
4492 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004493 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4494 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4495 }
4496
4497 void prepareSecondaryDisplay() {
4498 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4499 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4500 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004501 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004502
4503 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4504 float pressure) {
4505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4506 0.0f, 0.0f, 0.0f, EPSILON));
4507 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508};
4509
4510const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4511
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004512void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4513 int32_t originalY, int32_t rotatedX,
4514 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515 NotifyMotionArgs args;
4516
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004522 ASSERT_NO_FATAL_FAILURE(
4523 assertCursorPointerCoords(args.pointerCoords[0],
4524 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4525 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526}
4527
4528TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004530 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004532 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533}
4534
4535TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004537 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004539 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004540}
4541
4542TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004544 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545
4546 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004547 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548
4549 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004550 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4551 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4553 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4554
4555 // When the bounds are set, then there should be a valid motion range.
4556 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4557
4558 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004559 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560
4561 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4562 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4563 1, 800 - 1, 0.0f, 0.0f));
4564 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4565 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4566 2, 480 - 1, 0.0f, 0.0f));
4567 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4568 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4569 0.0f, 1.0f, 0.0f, 0.0f));
4570}
4571
4572TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004574 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575
4576 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004577 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578
4579 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4580 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4581 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4582 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4583 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4584 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4585 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4586 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4587 0.0f, 1.0f, 0.0f, 0.0f));
4588}
4589
4590TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004592 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593
arthurhungdcef2dc2020-08-11 14:47:50 +08004594 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595
4596 NotifyMotionArgs args;
4597
4598 // Button press.
4599 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4601 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4603 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4604 ASSERT_EQ(DEVICE_ID, args.deviceId);
4605 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4606 ASSERT_EQ(uint32_t(0), args.policyFlags);
4607 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4608 ASSERT_EQ(0, args.flags);
4609 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4610 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4611 ASSERT_EQ(0, args.edgeFlags);
4612 ASSERT_EQ(uint32_t(1), args.pointerCount);
4613 ASSERT_EQ(0, args.pointerProperties[0].id);
4614 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004615 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4617 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4618 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4619
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4621 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4622 ASSERT_EQ(DEVICE_ID, args.deviceId);
4623 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4624 ASSERT_EQ(uint32_t(0), args.policyFlags);
4625 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4626 ASSERT_EQ(0, args.flags);
4627 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4628 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4629 ASSERT_EQ(0, args.edgeFlags);
4630 ASSERT_EQ(uint32_t(1), args.pointerCount);
4631 ASSERT_EQ(0, args.pointerProperties[0].id);
4632 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004633 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004634 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4635 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4636 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4637
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004639 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4640 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4642 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4643 ASSERT_EQ(DEVICE_ID, args.deviceId);
4644 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4645 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4647 ASSERT_EQ(0, args.flags);
4648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4649 ASSERT_EQ(0, args.buttonState);
4650 ASSERT_EQ(0, args.edgeFlags);
4651 ASSERT_EQ(uint32_t(1), args.pointerCount);
4652 ASSERT_EQ(0, args.pointerProperties[0].id);
4653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004654 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4656 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4657 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4658
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4660 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4661 ASSERT_EQ(DEVICE_ID, args.deviceId);
4662 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4663 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4665 ASSERT_EQ(0, args.flags);
4666 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4667 ASSERT_EQ(0, args.buttonState);
4668 ASSERT_EQ(0, args.edgeFlags);
4669 ASSERT_EQ(uint32_t(1), args.pointerCount);
4670 ASSERT_EQ(0, args.pointerProperties[0].id);
4671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004672 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4674 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4675 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4676}
4677
4678TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004680 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681
4682 NotifyMotionArgs args;
4683
4684 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004689 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4690 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4691 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692
4693 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004698 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4699 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004700}
4701
4702TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004703 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004704 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004705
4706 NotifyMotionArgs args;
4707
4708 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4712 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004713 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004714
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4716 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004717 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004718
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4721 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004724 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004725
4726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004728 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729}
4730
4731TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004733 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004734
4735 NotifyMotionArgs args;
4736
4737 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4743 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004744 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4745 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4746 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4749 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004750 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4751 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4752 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004753
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004755 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4757 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004760 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4761 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4762 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763
4764 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004768 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004769 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004770
4771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004773 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774}
4775
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004776TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004777 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004779 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4780 // need to be rotated.
4781 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004782 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004784 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4786 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4787 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4788 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4789 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4790 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4791 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4792 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4793}
4794
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004795TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004796 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004798 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4799 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004800 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004802 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004803 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4805 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4806 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4807 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4808 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4809 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4810 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4811 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4812
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004813 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004814 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004815 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4816 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4817 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4818 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4819 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4820 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4821 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4822 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004824 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004825 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4827 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4828 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4829 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4830 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4831 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4832 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4833 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4834
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004835 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004836 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004837 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4838 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4839 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4840 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4841 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4842 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4843 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4844 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004845}
4846
4847TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004849 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850
4851 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4852 mFakePointerController->setPosition(100, 200);
4853 mFakePointerController->setButtonState(0);
4854
4855 NotifyMotionArgs motionArgs;
4856 NotifyKeyArgs keyArgs;
4857
4858 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4862 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4863 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4864 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004865 ASSERT_NO_FATAL_FAILURE(
4866 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4869 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4870 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4871 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004872 ASSERT_NO_FATAL_FAILURE(
4873 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004874
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004875 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4876 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879 ASSERT_EQ(0, motionArgs.buttonState);
4880 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004881 ASSERT_NO_FATAL_FAILURE(
4882 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883
4884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004885 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004886 ASSERT_EQ(0, motionArgs.buttonState);
4887 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004888 ASSERT_NO_FATAL_FAILURE(
4889 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004890
4891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004893 ASSERT_EQ(0, motionArgs.buttonState);
4894 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004895 ASSERT_NO_FATAL_FAILURE(
4896 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897
4898 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4903 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4904 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4905 motionArgs.buttonState);
4906 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4907 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004908 ASSERT_NO_FATAL_FAILURE(
4909 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004910
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4913 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4914 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4915 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004916 ASSERT_NO_FATAL_FAILURE(
4917 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004918
4919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4920 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4921 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4922 motionArgs.buttonState);
4923 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4924 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004925 ASSERT_NO_FATAL_FAILURE(
4926 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004927
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004928 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004931 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4933 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004934 ASSERT_NO_FATAL_FAILURE(
4935 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004936
4937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004939 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4940 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004941 ASSERT_NO_FATAL_FAILURE(
4942 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4945 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004947 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4948 ASSERT_EQ(0, motionArgs.buttonState);
4949 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004950 ASSERT_NO_FATAL_FAILURE(
4951 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004954
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004956 ASSERT_EQ(0, motionArgs.buttonState);
4957 ASSERT_EQ(0, mFakePointerController->getButtonState());
4958 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004959 ASSERT_NO_FATAL_FAILURE(
4960 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004961
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4963 ASSERT_EQ(0, motionArgs.buttonState);
4964 ASSERT_EQ(0, mFakePointerController->getButtonState());
4965 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004966 ASSERT_NO_FATAL_FAILURE(
4967 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968
4969 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4973 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4974 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004975
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004977 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4979 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004980 ASSERT_NO_FATAL_FAILURE(
4981 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004982
4983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4985 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4986 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004987 ASSERT_NO_FATAL_FAILURE(
4988 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004990 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004993 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 ASSERT_EQ(0, motionArgs.buttonState);
4995 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004996 ASSERT_NO_FATAL_FAILURE(
4997 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004998
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005001 ASSERT_EQ(0, motionArgs.buttonState);
5002 ASSERT_EQ(0, mFakePointerController->getButtonState());
5003
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005004 ASSERT_NO_FATAL_FAILURE(
5005 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5007 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5008 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5009
5010 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005011 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5014 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5015 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005016
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005018 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5020 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005021 ASSERT_NO_FATAL_FAILURE(
5022 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005023
5024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5025 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5026 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5027 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005028 ASSERT_NO_FATAL_FAILURE(
5029 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 ASSERT_EQ(0, motionArgs.buttonState);
5036 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005037 ASSERT_NO_FATAL_FAILURE(
5038 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005039
5040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5041 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5042 ASSERT_EQ(0, motionArgs.buttonState);
5043 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005044 ASSERT_NO_FATAL_FAILURE(
5045 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005046
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5048 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5049 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5050
5051 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5055 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5056 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005057
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005059 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5061 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005062 ASSERT_NO_FATAL_FAILURE(
5063 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005064
5065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5067 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5068 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005069 ASSERT_NO_FATAL_FAILURE(
5070 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076 ASSERT_EQ(0, motionArgs.buttonState);
5077 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005078 ASSERT_NO_FATAL_FAILURE(
5079 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005080
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5082 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5083 ASSERT_EQ(0, motionArgs.buttonState);
5084 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005085 ASSERT_NO_FATAL_FAILURE(
5086 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005087
Michael Wrightd02c5b62014-02-10 15:10:22 -08005088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5089 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5090 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5091
5092 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5096 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5097 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005098
Michael Wrightd02c5b62014-02-10 15:10:22 -08005099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005100 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5102 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005103 ASSERT_NO_FATAL_FAILURE(
5104 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005105
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5108 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5109 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005110 ASSERT_NO_FATAL_FAILURE(
5111 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117 ASSERT_EQ(0, motionArgs.buttonState);
5118 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005119 ASSERT_NO_FATAL_FAILURE(
5120 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005121
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5123 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5124 ASSERT_EQ(0, motionArgs.buttonState);
5125 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005126 ASSERT_NO_FATAL_FAILURE(
5127 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005128
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5130 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5131 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5132}
5133
5134TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005136 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005137
5138 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5139 mFakePointerController->setPosition(100, 200);
5140 mFakePointerController->setButtonState(0);
5141
5142 NotifyMotionArgs args;
5143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005148 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5149 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5151 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 +01005152 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005153}
5154
5155TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005156 addConfigurationProperty("cursor.mode", "pointer");
5157 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005158 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005159
5160 NotifyDeviceResetArgs resetArgs;
5161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5162 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5163 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5164
5165 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5166 mFakePointerController->setPosition(100, 200);
5167 mFakePointerController->setButtonState(0);
5168
5169 NotifyMotionArgs args;
5170
5171 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5176 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5179 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 +01005180 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005181
5182 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5186 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5187 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5188 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5189 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5191 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5192 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5194 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5195
5196 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005197 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5198 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5200 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5203 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5205 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5208 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5209
5210 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5215 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5217 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5218 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 +01005219 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005220
5221 // Disable pointer capture and check that the device generation got bumped
5222 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005223 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005224 mFakePolicy->setPointerCapture(false);
5225 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005226 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005227
5228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005229 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5230
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5235 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5238 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 +01005239 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005240}
5241
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005242/**
5243 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5244 * pointer acceleration or speed processing should not be applied.
5245 */
5246TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5247 addConfigurationProperty("cursor.mode", "pointer");
5248 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5249 100.f /*high threshold*/, 10.f /*acceleration*/);
5250 mFakePolicy->setVelocityControlParams(testParams);
5251 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5252
5253 NotifyDeviceResetArgs resetArgs;
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5255 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5256 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5257
5258 NotifyMotionArgs args;
5259
5260 // Move and verify scale is applied.
5261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5265 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5267 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5268 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5269 ASSERT_GT(relX, 10);
5270 ASSERT_GT(relY, 20);
5271
5272 // Enable Pointer Capture
5273 mFakePolicy->setPointerCapture(true);
5274 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5275 NotifyPointerCaptureChangedArgs captureArgs;
5276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5277 ASSERT_TRUE(captureArgs.request.enable);
5278
5279 // Move and verify scale is not applied.
5280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5284 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5286 ASSERT_EQ(10, args.pointerCoords[0].getX());
5287 ASSERT_EQ(20, args.pointerCoords[0].getY());
5288}
5289
Prabir Pradhan208360b2022-06-24 18:37:04 +00005290TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5291 addConfigurationProperty("cursor.mode", "pointer");
5292 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5293
5294 NotifyDeviceResetArgs resetArgs;
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5296 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5297 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5298
5299 // Ensure the display is rotated.
5300 prepareDisplay(DISPLAY_ORIENTATION_90);
5301
5302 NotifyMotionArgs args;
5303
5304 // Verify that the coordinates are rotated.
5305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5309 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5310 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5311 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5312 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5313
5314 // Enable Pointer Capture.
5315 mFakePolicy->setPointerCapture(true);
5316 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5317 NotifyPointerCaptureChangedArgs captureArgs;
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5319 ASSERT_TRUE(captureArgs.request.enable);
5320
5321 // Move and verify rotation is not applied.
5322 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5326 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5328 ASSERT_EQ(10, args.pointerCoords[0].getX());
5329 ASSERT_EQ(20, args.pointerCoords[0].getY());
5330}
5331
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005332TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005333 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005334
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005335 // Set up the default display.
5336 prepareDisplay(DISPLAY_ORIENTATION_90);
5337
5338 // Set up the secondary display as the display on which the pointer should be shown.
5339 // The InputDevice is not associated with any display.
5340 prepareSecondaryDisplay();
5341 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005342 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5343
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005344 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005345 mFakePointerController->setPosition(100, 200);
5346 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005347
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005348 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005353 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5354 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5355 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005356 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005357}
5358
5359TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5360 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5361
5362 // Set up the default display.
5363 prepareDisplay(DISPLAY_ORIENTATION_90);
5364
5365 // Set up the secondary display as the display on which the pointer should be shown,
5366 // and associate the InputDevice with the secondary display.
5367 prepareSecondaryDisplay();
5368 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5369 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5370 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5371
5372 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5373 mFakePointerController->setPosition(100, 200);
5374 mFakePointerController->setButtonState(0);
5375
5376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005380 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5381 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5382 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005383 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5384}
5385
5386TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5387 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5388
5389 // Set up the default display as the display on which the pointer should be shown.
5390 prepareDisplay(DISPLAY_ORIENTATION_90);
5391 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5392
5393 // Associate the InputDevice with the secondary display.
5394 prepareSecondaryDisplay();
5395 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5396 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5397
5398 // The mapper should not generate any events because it is associated with a display that is
5399 // different from the pointer display.
5400 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005404}
5405
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005406// --- BluetoothCursorInputMapperTest ---
5407
5408class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
5409protected:
5410 void SetUp() override {
5411 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
5412
5413 mFakePointerController = std::make_shared<FakePointerController>();
5414 mFakePolicy->setPointerController(mFakePointerController);
5415 }
5416};
5417
5418TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
5419 addConfigurationProperty("cursor.mode", "pointer");
5420 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5421
5422 nsecs_t kernelEventTime = ARBITRARY_TIME;
5423 nsecs_t expectedEventTime = ARBITRARY_TIME;
5424 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5425 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5427 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5428 WithEventTime(expectedEventTime))));
5429
5430 // Process several events that come in quick succession, according to their timestamps.
5431 for (int i = 0; i < 3; i++) {
5432 constexpr static nsecs_t delta = ms2ns(1);
5433 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5434 kernelEventTime += delta;
5435 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5436
5437 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5438 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5440 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5441 WithEventTime(expectedEventTime))));
5442 }
5443}
5444
5445TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
5446 addConfigurationProperty("cursor.mode", "pointer");
5447 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5448
5449 nsecs_t expectedEventTime = ARBITRARY_TIME;
5450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5451 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5453 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5454 WithEventTime(expectedEventTime))));
5455
5456 // Process several events with the same timestamp from the kernel.
5457 // Ensure that we do not generate events too far into the future.
5458 constexpr static int32_t numEvents =
5459 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5460 for (int i = 0; i < numEvents; i++) {
5461 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5462
5463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5464 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5466 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5467 WithEventTime(expectedEventTime))));
5468 }
5469
5470 // By processing more events with the same timestamp, we should not generate events with a
5471 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5472 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5473 for (int i = 0; i < 3; i++) {
5474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5477 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5478 WithEventTime(cappedEventTime))));
5479 }
5480}
5481
5482TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
5483 addConfigurationProperty("cursor.mode", "pointer");
5484 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5485
5486 nsecs_t kernelEventTime = ARBITRARY_TIME;
5487 nsecs_t expectedEventTime = ARBITRARY_TIME;
5488 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5489 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5491 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5492 WithEventTime(expectedEventTime))));
5493
5494 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5495 // smoothening is not needed, its timestamp is not affected.
5496 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5497 expectedEventTime = kernelEventTime;
5498
5499 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5500 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5502 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5503 WithEventTime(expectedEventTime))));
5504}
5505
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506// --- TouchInputMapperTest ---
5507
5508class TouchInputMapperTest : public InputMapperTest {
5509protected:
5510 static const int32_t RAW_X_MIN;
5511 static const int32_t RAW_X_MAX;
5512 static const int32_t RAW_Y_MIN;
5513 static const int32_t RAW_Y_MAX;
5514 static const int32_t RAW_TOUCH_MIN;
5515 static const int32_t RAW_TOUCH_MAX;
5516 static const int32_t RAW_TOOL_MIN;
5517 static const int32_t RAW_TOOL_MAX;
5518 static const int32_t RAW_PRESSURE_MIN;
5519 static const int32_t RAW_PRESSURE_MAX;
5520 static const int32_t RAW_ORIENTATION_MIN;
5521 static const int32_t RAW_ORIENTATION_MAX;
5522 static const int32_t RAW_DISTANCE_MIN;
5523 static const int32_t RAW_DISTANCE_MAX;
5524 static const int32_t RAW_TILT_MIN;
5525 static const int32_t RAW_TILT_MAX;
5526 static const int32_t RAW_ID_MIN;
5527 static const int32_t RAW_ID_MAX;
5528 static const int32_t RAW_SLOT_MIN;
5529 static const int32_t RAW_SLOT_MAX;
5530 static const float X_PRECISION;
5531 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005532 static const float X_PRECISION_VIRTUAL;
5533 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534
5535 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005536 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537
5538 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5539
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005540 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005541 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005542
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543 enum Axes {
5544 POSITION = 1 << 0,
5545 TOUCH = 1 << 1,
5546 TOOL = 1 << 2,
5547 PRESSURE = 1 << 3,
5548 ORIENTATION = 1 << 4,
5549 MINOR = 1 << 5,
5550 ID = 1 << 6,
5551 DISTANCE = 1 << 7,
5552 TILT = 1 << 8,
5553 SLOT = 1 << 9,
5554 TOOL_TYPE = 1 << 10,
5555 };
5556
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005557 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5558 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005559 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005560 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005561 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562 int32_t toRawX(float displayX);
5563 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005564 int32_t toRotatedRawX(float displayX);
5565 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005566 float toCookedX(float rawX, float rawY);
5567 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005569 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005570 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005571 float toDisplayY(int32_t rawY, int32_t displayHeight);
5572
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573};
5574
5575const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5576const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5577const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5578const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5579const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5580const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5581const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5582const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005583const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5584const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5586const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5587const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5588const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5589const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5590const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5591const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5592const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5593const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5594const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5595const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5596const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005597const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5598 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5599const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5600 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005601const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5602 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005603
5604const float TouchInputMapperTest::GEOMETRIC_SCALE =
5605 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5606 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5607
5608const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5609 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5610 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5611};
5612
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005613void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005614 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5615 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005616}
5617
5618void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5619 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5620 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621}
5622
Santos Cordonfa5cf462017-04-05 10:37:00 -07005623void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005624 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5625 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5626 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005627}
5628
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005630 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5631 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5632 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5633 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634}
5635
Jason Gerecke489fda82012-09-07 17:19:40 -07005636void TouchInputMapperTest::prepareLocationCalibration() {
5637 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5638}
5639
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640int32_t TouchInputMapperTest::toRawX(float displayX) {
5641 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5642}
5643
5644int32_t TouchInputMapperTest::toRawY(float displayY) {
5645 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5646}
5647
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005648int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5649 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5650}
5651
5652int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5653 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5654}
5655
Jason Gerecke489fda82012-09-07 17:19:40 -07005656float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5657 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5658 return rawX;
5659}
5660
5661float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5662 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5663 return rawY;
5664}
5665
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005667 return toDisplayX(rawX, DISPLAY_WIDTH);
5668}
5669
5670float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5671 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005672}
5673
5674float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005675 return toDisplayY(rawY, DISPLAY_HEIGHT);
5676}
5677
5678float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5679 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680}
5681
5682
5683// --- SingleTouchInputMapperTest ---
5684
5685class SingleTouchInputMapperTest : public TouchInputMapperTest {
5686protected:
5687 void prepareButtons();
5688 void prepareAxes(int axes);
5689
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005690 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5691 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5692 void processUp(SingleTouchInputMapper& mappery);
5693 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5694 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5695 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5696 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5697 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5698 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699};
5700
5701void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005702 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703}
5704
5705void SingleTouchInputMapperTest::prepareAxes(int axes) {
5706 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005707 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5708 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709 }
5710 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005711 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5712 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005713 }
5714 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005715 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5716 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717 }
5718 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005719 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5720 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721 }
5722 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005723 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5724 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725 }
5726}
5727
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005728void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005729 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005732}
5733
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005734void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005735 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005737}
5738
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005739void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741}
5742
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005743void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745}
5746
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005747void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5748 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750}
5751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005752void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005754}
5755
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005756void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5757 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005758 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760}
5761
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005762void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5763 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765}
5766
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005767void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005768 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005769}
5770
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772 prepareButtons();
5773 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005774 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005776 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777}
5778
Michael Wrightd02c5b62014-02-10 15:10:22 -08005779TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005780 prepareButtons();
5781 prepareAxes(POSITION);
5782 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005783 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005785 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786}
5787
5788TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789 addConfigurationProperty("touch.deviceType", "touchScreen");
5790 prepareDisplay(DISPLAY_ORIENTATION_0);
5791 prepareButtons();
5792 prepareAxes(POSITION);
5793 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005794 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795
5796 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005797 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005798
5799 // Virtual key is down.
5800 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5801 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5802 processDown(mapper, x, y);
5803 processSync(mapper);
5804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5805
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005806 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807
5808 // Virtual key is up.
5809 processUp(mapper);
5810 processSync(mapper);
5811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5812
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005813 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005814}
5815
5816TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817 addConfigurationProperty("touch.deviceType", "touchScreen");
5818 prepareDisplay(DISPLAY_ORIENTATION_0);
5819 prepareButtons();
5820 prepareAxes(POSITION);
5821 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005822 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005823
5824 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005825 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826
5827 // Virtual key is down.
5828 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5829 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5830 processDown(mapper, x, y);
5831 processSync(mapper);
5832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5833
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005834 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835
5836 // Virtual key is up.
5837 processUp(mapper);
5838 processSync(mapper);
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5840
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005841 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842}
5843
5844TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845 addConfigurationProperty("touch.deviceType", "touchScreen");
5846 prepareDisplay(DISPLAY_ORIENTATION_0);
5847 prepareButtons();
5848 prepareAxes(POSITION);
5849 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005850 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005853 ASSERT_TRUE(
5854 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005855 ASSERT_TRUE(flags[0]);
5856 ASSERT_FALSE(flags[1]);
5857}
5858
5859TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860 addConfigurationProperty("touch.deviceType", "touchScreen");
5861 prepareDisplay(DISPLAY_ORIENTATION_0);
5862 prepareButtons();
5863 prepareAxes(POSITION);
5864 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005865 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866
arthurhungdcef2dc2020-08-11 14:47:50 +08005867 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868
5869 NotifyKeyArgs args;
5870
5871 // Press virtual key.
5872 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5873 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5874 processDown(mapper, x, y);
5875 processSync(mapper);
5876
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5878 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5879 ASSERT_EQ(DEVICE_ID, args.deviceId);
5880 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5881 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5882 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5883 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5884 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5885 ASSERT_EQ(KEY_HOME, args.scanCode);
5886 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5887 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5888
5889 // Release virtual key.
5890 processUp(mapper);
5891 processSync(mapper);
5892
5893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5894 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5895 ASSERT_EQ(DEVICE_ID, args.deviceId);
5896 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5897 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5898 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5899 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5900 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5901 ASSERT_EQ(KEY_HOME, args.scanCode);
5902 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5903 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5904
5905 // Should not have sent any motions.
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5907}
5908
5909TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 addConfigurationProperty("touch.deviceType", "touchScreen");
5911 prepareDisplay(DISPLAY_ORIENTATION_0);
5912 prepareButtons();
5913 prepareAxes(POSITION);
5914 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005915 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916
arthurhungdcef2dc2020-08-11 14:47:50 +08005917 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918
5919 NotifyKeyArgs keyArgs;
5920
5921 // Press virtual key.
5922 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5923 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5924 processDown(mapper, x, y);
5925 processSync(mapper);
5926
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5928 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5929 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5930 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5931 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5932 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5933 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5934 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5935 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5936 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5937 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5938
5939 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5940 // into the display area.
5941 y -= 100;
5942 processMove(mapper, x, y);
5943 processSync(mapper);
5944
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5946 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5947 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5948 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5949 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5950 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5951 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5952 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5953 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5954 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5955 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5956 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5957
5958 NotifyMotionArgs motionArgs;
5959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5960 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5961 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5962 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5963 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5964 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5965 ASSERT_EQ(0, motionArgs.flags);
5966 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5967 ASSERT_EQ(0, motionArgs.buttonState);
5968 ASSERT_EQ(0, motionArgs.edgeFlags);
5969 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5970 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5971 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5973 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5974 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5975 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5976 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5977
5978 // Keep moving out of bounds. Should generate a pointer move.
5979 y -= 50;
5980 processMove(mapper, x, y);
5981 processSync(mapper);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5985 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5986 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5987 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5988 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5989 ASSERT_EQ(0, motionArgs.flags);
5990 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5991 ASSERT_EQ(0, motionArgs.buttonState);
5992 ASSERT_EQ(0, motionArgs.edgeFlags);
5993 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5994 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5997 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5998 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5999 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6000 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6001
6002 // Release out of bounds. Should generate a pointer up.
6003 processUp(mapper);
6004 processSync(mapper);
6005
6006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6007 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6008 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6009 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6010 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6011 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6012 ASSERT_EQ(0, motionArgs.flags);
6013 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6014 ASSERT_EQ(0, motionArgs.buttonState);
6015 ASSERT_EQ(0, motionArgs.edgeFlags);
6016 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6017 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6018 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6020 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6021 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6022 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6023 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6024
6025 // Should not have sent any more keys or motions.
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6028}
6029
6030TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 addConfigurationProperty("touch.deviceType", "touchScreen");
6032 prepareDisplay(DISPLAY_ORIENTATION_0);
6033 prepareButtons();
6034 prepareAxes(POSITION);
6035 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006036 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037
arthurhungdcef2dc2020-08-11 14:47:50 +08006038 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006039
6040 NotifyMotionArgs motionArgs;
6041
6042 // Initially go down out of bounds.
6043 int32_t x = -10;
6044 int32_t y = -10;
6045 processDown(mapper, x, y);
6046 processSync(mapper);
6047
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6049
6050 // Move into the display area. Should generate a pointer down.
6051 x = 50;
6052 y = 75;
6053 processMove(mapper, x, y);
6054 processSync(mapper);
6055
6056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6057 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6058 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6059 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6060 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6061 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6062 ASSERT_EQ(0, motionArgs.flags);
6063 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6064 ASSERT_EQ(0, motionArgs.buttonState);
6065 ASSERT_EQ(0, motionArgs.edgeFlags);
6066 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6067 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6070 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6071 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6072 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6073 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6074
6075 // Release. Should generate a pointer up.
6076 processUp(mapper);
6077 processSync(mapper);
6078
6079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6080 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6081 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6082 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6083 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6085 ASSERT_EQ(0, motionArgs.flags);
6086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6087 ASSERT_EQ(0, motionArgs.buttonState);
6088 ASSERT_EQ(0, motionArgs.edgeFlags);
6089 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6090 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6093 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6094 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6095 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6096 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6097
6098 // Should not have sent any more keys or motions.
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6101}
6102
Santos Cordonfa5cf462017-04-05 10:37:00 -07006103TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006104 addConfigurationProperty("touch.deviceType", "touchScreen");
6105 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6106
6107 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
6108 prepareButtons();
6109 prepareAxes(POSITION);
6110 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006111 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006112
arthurhungdcef2dc2020-08-11 14:47:50 +08006113 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006114
6115 NotifyMotionArgs motionArgs;
6116
6117 // Down.
6118 int32_t x = 100;
6119 int32_t y = 125;
6120 processDown(mapper, x, y);
6121 processSync(mapper);
6122
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6124 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6125 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6126 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6127 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6128 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6129 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6130 ASSERT_EQ(0, motionArgs.flags);
6131 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6132 ASSERT_EQ(0, motionArgs.buttonState);
6133 ASSERT_EQ(0, motionArgs.edgeFlags);
6134 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6135 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6138 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6139 1, 0, 0, 0, 0, 0, 0, 0));
6140 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6141 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6142 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6143
6144 // Move.
6145 x += 50;
6146 y += 75;
6147 processMove(mapper, x, y);
6148 processSync(mapper);
6149
6150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6151 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6152 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6153 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6154 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6155 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6157 ASSERT_EQ(0, motionArgs.flags);
6158 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6159 ASSERT_EQ(0, motionArgs.buttonState);
6160 ASSERT_EQ(0, motionArgs.edgeFlags);
6161 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6162 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6165 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6166 1, 0, 0, 0, 0, 0, 0, 0));
6167 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6168 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6169 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6170
6171 // Up.
6172 processUp(mapper);
6173 processSync(mapper);
6174
6175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6176 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6177 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6178 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6179 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6180 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6181 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6182 ASSERT_EQ(0, motionArgs.flags);
6183 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6184 ASSERT_EQ(0, motionArgs.buttonState);
6185 ASSERT_EQ(0, motionArgs.edgeFlags);
6186 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6187 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6190 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6191 1, 0, 0, 0, 0, 0, 0, 0));
6192 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6193 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6194 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6195
6196 // Should not have sent any more keys or motions.
6197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6199}
6200
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006202 addConfigurationProperty("touch.deviceType", "touchScreen");
6203 prepareDisplay(DISPLAY_ORIENTATION_0);
6204 prepareButtons();
6205 prepareAxes(POSITION);
6206 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006207 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208
arthurhungdcef2dc2020-08-11 14:47:50 +08006209 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210
6211 NotifyMotionArgs motionArgs;
6212
6213 // Down.
6214 int32_t x = 100;
6215 int32_t y = 125;
6216 processDown(mapper, x, y);
6217 processSync(mapper);
6218
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6221 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6222 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6223 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6224 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6225 ASSERT_EQ(0, motionArgs.flags);
6226 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6227 ASSERT_EQ(0, motionArgs.buttonState);
6228 ASSERT_EQ(0, motionArgs.edgeFlags);
6229 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6230 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6233 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6234 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6235 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6237
6238 // Move.
6239 x += 50;
6240 y += 75;
6241 processMove(mapper, x, y);
6242 processSync(mapper);
6243
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6246 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6247 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6248 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6250 ASSERT_EQ(0, motionArgs.flags);
6251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6252 ASSERT_EQ(0, motionArgs.buttonState);
6253 ASSERT_EQ(0, motionArgs.edgeFlags);
6254 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6255 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6256 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6258 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6259 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6260 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6261 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6262
6263 // Up.
6264 processUp(mapper);
6265 processSync(mapper);
6266
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6269 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6270 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6271 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6272 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6273 ASSERT_EQ(0, motionArgs.flags);
6274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6275 ASSERT_EQ(0, motionArgs.buttonState);
6276 ASSERT_EQ(0, motionArgs.edgeFlags);
6277 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6278 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6279 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6281 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6282 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6283 ASSERT_NEAR(Y_PRECISION, 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
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006291TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006292 addConfigurationProperty("touch.deviceType", "touchScreen");
6293 prepareButtons();
6294 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006295 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6296 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006297 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298
6299 NotifyMotionArgs args;
6300
6301 // Rotation 90.
6302 prepareDisplay(DISPLAY_ORIENTATION_90);
6303 processDown(mapper, toRawX(50), toRawY(75));
6304 processSync(mapper);
6305
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6307 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6308 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6309
6310 processUp(mapper);
6311 processSync(mapper);
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6313}
6314
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006315TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 addConfigurationProperty("touch.deviceType", "touchScreen");
6317 prepareButtons();
6318 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006319 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6320 // orientation-aware are affected by display rotation.
6321 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006322 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006323
6324 NotifyMotionArgs args;
6325
6326 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006327 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006328 prepareDisplay(DISPLAY_ORIENTATION_0);
6329 processDown(mapper, toRawX(50), toRawY(75));
6330 processSync(mapper);
6331
6332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6333 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6334 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6335
6336 processUp(mapper);
6337 processSync(mapper);
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6339
6340 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006341 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006342 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006343 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344 processSync(mapper);
6345
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6347 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6348 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6349
6350 processUp(mapper);
6351 processSync(mapper);
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6353
6354 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006355 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356 prepareDisplay(DISPLAY_ORIENTATION_180);
6357 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6358 processSync(mapper);
6359
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6361 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6362 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6363
6364 processUp(mapper);
6365 processSync(mapper);
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6367
6368 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006369 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006371 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372 processSync(mapper);
6373
6374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6375 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6376 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6377
6378 processUp(mapper);
6379 processSync(mapper);
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6381}
6382
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006383TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6384 addConfigurationProperty("touch.deviceType", "touchScreen");
6385 prepareButtons();
6386 prepareAxes(POSITION);
6387 addConfigurationProperty("touch.orientationAware", "1");
6388 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6389 clearViewports();
6390 prepareDisplay(DISPLAY_ORIENTATION_0);
6391 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6392 NotifyMotionArgs args;
6393
6394 // Orientation 0.
6395 processDown(mapper, toRawX(50), toRawY(75));
6396 processSync(mapper);
6397
6398 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6399 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6400 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6401
6402 processUp(mapper);
6403 processSync(mapper);
6404 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6405}
6406
6407TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6408 addConfigurationProperty("touch.deviceType", "touchScreen");
6409 prepareButtons();
6410 prepareAxes(POSITION);
6411 addConfigurationProperty("touch.orientationAware", "1");
6412 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6413 clearViewports();
6414 prepareDisplay(DISPLAY_ORIENTATION_0);
6415 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6416 NotifyMotionArgs args;
6417
6418 // Orientation 90.
6419 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6420 processSync(mapper);
6421
6422 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6423 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6424 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6425
6426 processUp(mapper);
6427 processSync(mapper);
6428 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6429}
6430
6431TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6432 addConfigurationProperty("touch.deviceType", "touchScreen");
6433 prepareButtons();
6434 prepareAxes(POSITION);
6435 addConfigurationProperty("touch.orientationAware", "1");
6436 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6437 clearViewports();
6438 prepareDisplay(DISPLAY_ORIENTATION_0);
6439 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6440 NotifyMotionArgs args;
6441
6442 // Orientation 180.
6443 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6444 processSync(mapper);
6445
6446 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6447 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6448 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6449
6450 processUp(mapper);
6451 processSync(mapper);
6452 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6453}
6454
6455TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6456 addConfigurationProperty("touch.deviceType", "touchScreen");
6457 prepareButtons();
6458 prepareAxes(POSITION);
6459 addConfigurationProperty("touch.orientationAware", "1");
6460 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6461 clearViewports();
6462 prepareDisplay(DISPLAY_ORIENTATION_0);
6463 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6464 NotifyMotionArgs args;
6465
6466 // Orientation 270.
6467 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6468 processSync(mapper);
6469
6470 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6471 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6472 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6473
6474 processUp(mapper);
6475 processSync(mapper);
6476 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6477}
6478
6479TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6480 addConfigurationProperty("touch.deviceType", "touchScreen");
6481 prepareButtons();
6482 prepareAxes(POSITION);
6483 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6484 // orientation-aware are affected by display rotation.
6485 addConfigurationProperty("touch.orientationAware", "0");
6486 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6487 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6488
6489 NotifyMotionArgs args;
6490
6491 // Orientation 90, Rotation 0.
6492 clearViewports();
6493 prepareDisplay(DISPLAY_ORIENTATION_0);
6494 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6495 processSync(mapper);
6496
6497 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6498 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6499 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6500
6501 processUp(mapper);
6502 processSync(mapper);
6503 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6504
6505 // Orientation 90, Rotation 90.
6506 clearViewports();
6507 prepareDisplay(DISPLAY_ORIENTATION_90);
6508 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6509 processSync(mapper);
6510
6511 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6512 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6513 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6514
6515 processUp(mapper);
6516 processSync(mapper);
6517 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6518
6519 // Orientation 90, Rotation 180.
6520 clearViewports();
6521 prepareDisplay(DISPLAY_ORIENTATION_180);
6522 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6523 processSync(mapper);
6524
6525 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6526 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6527 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6528
6529 processUp(mapper);
6530 processSync(mapper);
6531 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6532
6533 // Orientation 90, Rotation 270.
6534 clearViewports();
6535 prepareDisplay(DISPLAY_ORIENTATION_270);
6536 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6537 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6538 processSync(mapper);
6539
6540 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6541 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6542 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6543
6544 processUp(mapper);
6545 processSync(mapper);
6546 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6547}
6548
Michael Wrightd02c5b62014-02-10 15:10:22 -08006549TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006550 addConfigurationProperty("touch.deviceType", "touchScreen");
6551 prepareDisplay(DISPLAY_ORIENTATION_0);
6552 prepareButtons();
6553 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006554 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006555
6556 // These calculations are based on the input device calibration documentation.
6557 int32_t rawX = 100;
6558 int32_t rawY = 200;
6559 int32_t rawPressure = 10;
6560 int32_t rawToolMajor = 12;
6561 int32_t rawDistance = 2;
6562 int32_t rawTiltX = 30;
6563 int32_t rawTiltY = 110;
6564
6565 float x = toDisplayX(rawX);
6566 float y = toDisplayY(rawY);
6567 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6568 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6569 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6570 float distance = float(rawDistance);
6571
6572 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6573 float tiltScale = M_PI / 180;
6574 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6575 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6576 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6577 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6578
6579 processDown(mapper, rawX, rawY);
6580 processPressure(mapper, rawPressure);
6581 processToolMajor(mapper, rawToolMajor);
6582 processDistance(mapper, rawDistance);
6583 processTilt(mapper, rawTiltX, rawTiltY);
6584 processSync(mapper);
6585
6586 NotifyMotionArgs args;
6587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6589 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6590 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6591}
6592
Jason Gerecke489fda82012-09-07 17:19:40 -07006593TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006594 addConfigurationProperty("touch.deviceType", "touchScreen");
6595 prepareDisplay(DISPLAY_ORIENTATION_0);
6596 prepareLocationCalibration();
6597 prepareButtons();
6598 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006599 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006600
6601 int32_t rawX = 100;
6602 int32_t rawY = 200;
6603
6604 float x = toDisplayX(toCookedX(rawX, rawY));
6605 float y = toDisplayY(toCookedY(rawX, rawY));
6606
6607 processDown(mapper, rawX, rawY);
6608 processSync(mapper);
6609
6610 NotifyMotionArgs args;
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6613 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6614}
6615
Michael Wrightd02c5b62014-02-10 15:10:22 -08006616TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006617 addConfigurationProperty("touch.deviceType", "touchScreen");
6618 prepareDisplay(DISPLAY_ORIENTATION_0);
6619 prepareButtons();
6620 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006621 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006622
6623 NotifyMotionArgs motionArgs;
6624 NotifyKeyArgs keyArgs;
6625
6626 processDown(mapper, 100, 200);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6629 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6630 ASSERT_EQ(0, motionArgs.buttonState);
6631
6632 // press BTN_LEFT, release BTN_LEFT
6633 processKey(mapper, BTN_LEFT, 1);
6634 processSync(mapper);
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6637 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6638
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6641 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6642
Michael Wrightd02c5b62014-02-10 15:10:22 -08006643 processKey(mapper, BTN_LEFT, 0);
6644 processSync(mapper);
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006647 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006648
6649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006651 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006652
6653 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6654 processKey(mapper, BTN_RIGHT, 1);
6655 processKey(mapper, BTN_MIDDLE, 1);
6656 processSync(mapper);
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6659 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6660 motionArgs.buttonState);
6661
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6664 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6665
6666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6668 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6669 motionArgs.buttonState);
6670
Michael Wrightd02c5b62014-02-10 15:10:22 -08006671 processKey(mapper, BTN_RIGHT, 0);
6672 processSync(mapper);
6673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006675 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006676
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006679 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006680
6681 processKey(mapper, BTN_MIDDLE, 0);
6682 processSync(mapper);
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006684 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006685 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006686
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006689 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006690
6691 // press BTN_BACK, release BTN_BACK
6692 processKey(mapper, BTN_BACK, 1);
6693 processSync(mapper);
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6695 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6696 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006697
Michael Wrightd02c5b62014-02-10 15:10:22 -08006698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006700 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6701
6702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6703 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6704 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006705
6706 processKey(mapper, BTN_BACK, 0);
6707 processSync(mapper);
6708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006710 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006711
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006714 ASSERT_EQ(0, motionArgs.buttonState);
6715
Michael Wrightd02c5b62014-02-10 15:10:22 -08006716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6717 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6718 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6719
6720 // press BTN_SIDE, release BTN_SIDE
6721 processKey(mapper, BTN_SIDE, 1);
6722 processSync(mapper);
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6724 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6725 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006726
Michael Wrightd02c5b62014-02-10 15:10:22 -08006727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006729 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6730
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6732 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6733 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006734
6735 processKey(mapper, BTN_SIDE, 0);
6736 processSync(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006739 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006740
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006743 ASSERT_EQ(0, motionArgs.buttonState);
6744
Michael Wrightd02c5b62014-02-10 15:10:22 -08006745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6746 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6747 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6748
6749 // press BTN_FORWARD, release BTN_FORWARD
6750 processKey(mapper, BTN_FORWARD, 1);
6751 processSync(mapper);
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6753 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6754 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006755
Michael Wrightd02c5b62014-02-10 15:10:22 -08006756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006758 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6759
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6761 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6762 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006763
6764 processKey(mapper, BTN_FORWARD, 0);
6765 processSync(mapper);
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006767 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006768 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006769
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006772 ASSERT_EQ(0, motionArgs.buttonState);
6773
Michael Wrightd02c5b62014-02-10 15:10:22 -08006774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6775 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6776 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6777
6778 // press BTN_EXTRA, release BTN_EXTRA
6779 processKey(mapper, BTN_EXTRA, 1);
6780 processSync(mapper);
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6782 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6783 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006784
Michael Wrightd02c5b62014-02-10 15:10:22 -08006785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006787 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6788
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6790 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6791 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006792
6793 processKey(mapper, BTN_EXTRA, 0);
6794 processSync(mapper);
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006796 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006797 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006798
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006801 ASSERT_EQ(0, motionArgs.buttonState);
6802
Michael Wrightd02c5b62014-02-10 15:10:22 -08006803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6804 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6805 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6806
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6808
Michael Wrightd02c5b62014-02-10 15:10:22 -08006809 // press BTN_STYLUS, release BTN_STYLUS
6810 processKey(mapper, BTN_STYLUS, 1);
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006814 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6815
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6817 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6818 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006819
6820 processKey(mapper, BTN_STYLUS, 0);
6821 processSync(mapper);
6822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006823 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006824 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006825
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006828 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006829
6830 // press BTN_STYLUS2, release BTN_STYLUS2
6831 processKey(mapper, BTN_STYLUS2, 1);
6832 processSync(mapper);
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006835 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6836
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6838 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6839 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006840
6841 processKey(mapper, BTN_STYLUS2, 0);
6842 processSync(mapper);
6843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006844 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006845 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006846
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006849 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006850
6851 // release touch
6852 processUp(mapper);
6853 processSync(mapper);
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6855 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6856 ASSERT_EQ(0, motionArgs.buttonState);
6857}
6858
6859TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006860 addConfigurationProperty("touch.deviceType", "touchScreen");
6861 prepareDisplay(DISPLAY_ORIENTATION_0);
6862 prepareButtons();
6863 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006864 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006865
6866 NotifyMotionArgs motionArgs;
6867
6868 // default tool type is finger
6869 processDown(mapper, 100, 200);
6870 processSync(mapper);
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6872 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6874
6875 // eraser
6876 processKey(mapper, BTN_TOOL_RUBBER, 1);
6877 processSync(mapper);
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6881
6882 // stylus
6883 processKey(mapper, BTN_TOOL_RUBBER, 0);
6884 processKey(mapper, BTN_TOOL_PEN, 1);
6885 processSync(mapper);
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6889
6890 // brush
6891 processKey(mapper, BTN_TOOL_PEN, 0);
6892 processKey(mapper, BTN_TOOL_BRUSH, 1);
6893 processSync(mapper);
6894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6897
6898 // pencil
6899 processKey(mapper, BTN_TOOL_BRUSH, 0);
6900 processKey(mapper, BTN_TOOL_PENCIL, 1);
6901 processSync(mapper);
6902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6905
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006906 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006907 processKey(mapper, BTN_TOOL_PENCIL, 0);
6908 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6909 processSync(mapper);
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6913
6914 // mouse
6915 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6916 processKey(mapper, BTN_TOOL_MOUSE, 1);
6917 processSync(mapper);
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6919 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6920 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6921
6922 // lens
6923 processKey(mapper, BTN_TOOL_MOUSE, 0);
6924 processKey(mapper, BTN_TOOL_LENS, 1);
6925 processSync(mapper);
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6929
6930 // double-tap
6931 processKey(mapper, BTN_TOOL_LENS, 0);
6932 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6933 processSync(mapper);
6934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6935 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6937
6938 // triple-tap
6939 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6940 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6941 processSync(mapper);
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6944 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6945
6946 // quad-tap
6947 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6948 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6949 processSync(mapper);
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6952 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6953
6954 // finger
6955 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6956 processKey(mapper, BTN_TOOL_FINGER, 1);
6957 processSync(mapper);
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6961
6962 // stylus trumps finger
6963 processKey(mapper, BTN_TOOL_PEN, 1);
6964 processSync(mapper);
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6968
6969 // eraser trumps stylus
6970 processKey(mapper, BTN_TOOL_RUBBER, 1);
6971 processSync(mapper);
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6975
6976 // mouse trumps eraser
6977 processKey(mapper, BTN_TOOL_MOUSE, 1);
6978 processSync(mapper);
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6980 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6981 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6982
6983 // back to default tool type
6984 processKey(mapper, BTN_TOOL_MOUSE, 0);
6985 processKey(mapper, BTN_TOOL_RUBBER, 0);
6986 processKey(mapper, BTN_TOOL_PEN, 0);
6987 processKey(mapper, BTN_TOOL_FINGER, 0);
6988 processSync(mapper);
6989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6992}
6993
6994TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006995 addConfigurationProperty("touch.deviceType", "touchScreen");
6996 prepareDisplay(DISPLAY_ORIENTATION_0);
6997 prepareButtons();
6998 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006999 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007000 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007001
7002 NotifyMotionArgs motionArgs;
7003
7004 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7005 processKey(mapper, BTN_TOOL_FINGER, 1);
7006 processMove(mapper, 100, 200);
7007 processSync(mapper);
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7009 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7010 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7011 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7012
7013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7014 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7016 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7017
7018 // move a little
7019 processMove(mapper, 150, 250);
7020 processSync(mapper);
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7022 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7024 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7025
7026 // down when BTN_TOUCH is pressed, pressure defaults to 1
7027 processKey(mapper, BTN_TOUCH, 1);
7028 processSync(mapper);
7029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7030 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7032 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7033
7034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7035 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7037 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7038
7039 // up when BTN_TOUCH is released, hover restored
7040 processKey(mapper, BTN_TOUCH, 0);
7041 processSync(mapper);
7042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7043 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7045 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7046
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7048 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7050 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7051
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7053 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7055 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7056
7057 // exit hover when pointer goes away
7058 processKey(mapper, BTN_TOOL_FINGER, 0);
7059 processSync(mapper);
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7061 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7063 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7064}
7065
7066TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 addConfigurationProperty("touch.deviceType", "touchScreen");
7068 prepareDisplay(DISPLAY_ORIENTATION_0);
7069 prepareButtons();
7070 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007071 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007072
7073 NotifyMotionArgs motionArgs;
7074
7075 // initially hovering because pressure is 0
7076 processDown(mapper, 100, 200);
7077 processPressure(mapper, 0);
7078 processSync(mapper);
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7080 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7082 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7083
7084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7085 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7087 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7088
7089 // move a little
7090 processMove(mapper, 150, 250);
7091 processSync(mapper);
7092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7093 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7095 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7096
7097 // down when pressure is non-zero
7098 processPressure(mapper, RAW_PRESSURE_MAX);
7099 processSync(mapper);
7100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7101 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7103 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7104
7105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7106 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7108 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7109
7110 // up when pressure becomes 0, hover restored
7111 processPressure(mapper, 0);
7112 processSync(mapper);
7113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7114 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7115 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7116 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7117
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7119 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7121 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7122
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7124 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7125 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7126 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7127
7128 // exit hover when pointer goes away
7129 processUp(mapper);
7130 processSync(mapper);
7131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7132 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7134 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7135}
7136
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007137TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7138 addConfigurationProperty("touch.deviceType", "touchScreen");
7139 prepareDisplay(DISPLAY_ORIENTATION_0);
7140 prepareButtons();
7141 prepareAxes(POSITION | PRESSURE);
7142 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7143
7144 // Touch down.
7145 processDown(mapper, 100, 200);
7146 processPressure(mapper, 1);
7147 processSync(mapper);
7148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7149 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7150
7151 // Reset the mapper. This should cancel the ongoing gesture.
7152 resetMapper(mapper, ARBITRARY_TIME);
7153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7154 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7155
7156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7157}
7158
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007159TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7160 addConfigurationProperty("touch.deviceType", "touchScreen");
7161 prepareDisplay(DISPLAY_ORIENTATION_0);
7162 prepareButtons();
7163 prepareAxes(POSITION | PRESSURE);
7164 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7165
7166 // Set the initial state for the touch pointer.
7167 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7168 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7169 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7170 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7171
7172 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007173 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7174 // does not generate any events.
7175 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007176
7177 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7178 // the recreated touch state to generate a down event.
7179 processSync(mapper);
7180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7181 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7182
7183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7184}
7185
lilinnan687e58f2022-07-19 16:00:50 +08007186TEST_F(SingleTouchInputMapperTest,
7187 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7188 addConfigurationProperty("touch.deviceType", "touchScreen");
7189 prepareDisplay(DISPLAY_ORIENTATION_0);
7190 prepareButtons();
7191 prepareAxes(POSITION);
7192 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7193 NotifyMotionArgs motionArgs;
7194
7195 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007196 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007197 processSync(mapper);
7198
7199 // We should receive a down event
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7201 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7202
7203 // Change display id
7204 clearViewports();
7205 prepareSecondaryDisplay(ViewportType::INTERNAL);
7206
7207 // We should receive a cancel event
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7209 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7210 // Then receive reset called
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7212}
7213
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007214TEST_F(SingleTouchInputMapperTest,
7215 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7216 addConfigurationProperty("touch.deviceType", "touchScreen");
7217 prepareDisplay(DISPLAY_ORIENTATION_0);
7218 prepareButtons();
7219 prepareAxes(POSITION);
7220 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7222 NotifyMotionArgs motionArgs;
7223
7224 // Start a new gesture.
7225 processDown(mapper, 100, 200);
7226 processSync(mapper);
7227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7228 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7229
7230 // Make the viewport inactive. This will put the device in disabled mode.
7231 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7232 viewport->isActive = false;
7233 mFakePolicy->updateViewport(*viewport);
7234 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7235
7236 // We should receive a cancel event for the ongoing gesture.
7237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7238 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7239 // Then we should be notified that the device was reset.
7240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7241
7242 // No events are generated while the viewport is inactive.
7243 processMove(mapper, 101, 201);
7244 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007245 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007246 processSync(mapper);
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7248
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007249 // Start a new gesture while the viewport is still inactive.
7250 processDown(mapper, 300, 400);
7251 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7252 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7253 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7254 processSync(mapper);
7255
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007256 // Make the viewport active again. The device should resume processing events.
7257 viewport->isActive = true;
7258 mFakePolicy->updateViewport(*viewport);
7259 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7260
7261 // The device is reset because it changes back to direct mode, without generating any events.
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7264
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007265 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007266 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7268 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007269
7270 // No more events.
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7273}
7274
Prabir Pradhan211ba622022-10-31 21:09:21 +00007275TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7276 addConfigurationProperty("touch.deviceType", "touchScreen");
7277 prepareDisplay(DISPLAY_ORIENTATION_0);
7278 prepareButtons();
7279 prepareAxes(POSITION);
7280 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7282
7283 // Press a stylus button.
7284 processKey(mapper, BTN_STYLUS, 1);
7285 processSync(mapper);
7286
7287 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7288 processDown(mapper, 100, 200);
7289 processSync(mapper);
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7291 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7292 WithCoords(toDisplayX(100), toDisplayY(200)),
7293 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7295 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7296 WithCoords(toDisplayX(100), toDisplayY(200)),
7297 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7298
7299 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7300 // the button has not actually been released, since there will be no pointers through which the
7301 // button state can be reported. The event is generated at the location of the pointer before
7302 // it went up.
7303 processUp(mapper);
7304 processSync(mapper);
7305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7306 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7307 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7309 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7310 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7311}
7312
Prabir Pradhan5632d622021-09-06 07:57:20 -07007313// --- TouchDisplayProjectionTest ---
7314
7315class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7316public:
7317 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7318 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7319 // rotated equivalent of the given un-rotated physical display bounds.
7320 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7321 uint32_t inverseRotationFlags;
7322 auto width = DISPLAY_WIDTH;
7323 auto height = DISPLAY_HEIGHT;
7324 switch (orientation) {
7325 case DISPLAY_ORIENTATION_90:
7326 inverseRotationFlags = ui::Transform::ROT_270;
7327 std::swap(width, height);
7328 break;
7329 case DISPLAY_ORIENTATION_180:
7330 inverseRotationFlags = ui::Transform::ROT_180;
7331 break;
7332 case DISPLAY_ORIENTATION_270:
7333 inverseRotationFlags = ui::Transform::ROT_90;
7334 std::swap(width, height);
7335 break;
7336 case DISPLAY_ORIENTATION_0:
7337 inverseRotationFlags = ui::Transform::ROT_0;
7338 break;
7339 default:
7340 FAIL() << "Invalid orientation: " << orientation;
7341 }
7342
7343 const ui::Transform rotation(inverseRotationFlags, width, height);
7344 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7345
7346 std::optional<DisplayViewport> internalViewport =
7347 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7348 DisplayViewport& v = *internalViewport;
7349 v.displayId = DISPLAY_ID;
7350 v.orientation = orientation;
7351
7352 v.logicalLeft = 0;
7353 v.logicalTop = 0;
7354 v.logicalRight = 100;
7355 v.logicalBottom = 100;
7356
7357 v.physicalLeft = rotatedPhysicalDisplay.left;
7358 v.physicalTop = rotatedPhysicalDisplay.top;
7359 v.physicalRight = rotatedPhysicalDisplay.right;
7360 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7361
7362 v.deviceWidth = width;
7363 v.deviceHeight = height;
7364
7365 v.isActive = true;
7366 v.uniqueId = UNIQUE_ID;
7367 v.type = ViewportType::INTERNAL;
7368 mFakePolicy->updateViewport(v);
7369 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7370 }
7371
7372 void assertReceivedMove(const Point& point) {
7373 NotifyMotionArgs motionArgs;
7374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7376 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7378 1, 0, 0, 0, 0, 0, 0, 0));
7379 }
7380};
7381
7382TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7383 addConfigurationProperty("touch.deviceType", "touchScreen");
7384 prepareDisplay(DISPLAY_ORIENTATION_0);
7385
7386 prepareButtons();
7387 prepareAxes(POSITION);
7388 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7389
7390 NotifyMotionArgs motionArgs;
7391
7392 // Configure the DisplayViewport such that the logical display maps to a subsection of
7393 // the display panel called the physical display. Here, the physical display is bounded by the
7394 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7395 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7396 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7397 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7398
7399 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7400 DISPLAY_ORIENTATION_270}) {
7401 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7402
7403 // Touches outside the physical display should be ignored, and should not generate any
7404 // events. Ensure touches at the following points that lie outside of the physical display
7405 // area do not generate any events.
7406 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7407 processDown(mapper, toRawX(point.x), toRawY(point.y));
7408 processSync(mapper);
7409 processUp(mapper);
7410 processSync(mapper);
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7412 << "Unexpected event generated for touch outside physical display at point: "
7413 << point.x << ", " << point.y;
7414 }
7415 }
7416}
7417
7418TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7419 addConfigurationProperty("touch.deviceType", "touchScreen");
7420 prepareDisplay(DISPLAY_ORIENTATION_0);
7421
7422 prepareButtons();
7423 prepareAxes(POSITION);
7424 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7425
7426 NotifyMotionArgs motionArgs;
7427
7428 // Configure the DisplayViewport such that the logical display maps to a subsection of
7429 // the display panel called the physical display. Here, the physical display is bounded by the
7430 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7431 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7432
7433 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7434 DISPLAY_ORIENTATION_270}) {
7435 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7436
7437 // Touches that start outside the physical display should be ignored until it enters the
7438 // physical display bounds, at which point it should generate a down event. Start a touch at
7439 // the point (5, 100), which is outside the physical display bounds.
7440 static const Point kOutsidePoint{5, 100};
7441 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7442 processSync(mapper);
7443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7444
7445 // Move the touch into the physical display area. This should generate a pointer down.
7446 processMove(mapper, toRawX(11), toRawY(21));
7447 processSync(mapper);
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7449 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7450 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7451 ASSERT_NO_FATAL_FAILURE(
7452 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7453
7454 // Move the touch inside the physical display area. This should generate a pointer move.
7455 processMove(mapper, toRawX(69), toRawY(159));
7456 processSync(mapper);
7457 assertReceivedMove({69, 159});
7458
7459 // Move outside the physical display area. Since the pointer is already down, this should
7460 // now continue generating events.
7461 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7462 processSync(mapper);
7463 assertReceivedMove(kOutsidePoint);
7464
7465 // Release. This should generate a pointer up.
7466 processUp(mapper);
7467 processSync(mapper);
7468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7469 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7471 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7472
7473 // Ensure no more events were generated.
7474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7476 }
7477}
7478
Michael Wrightd02c5b62014-02-10 15:10:22 -08007479// --- MultiTouchInputMapperTest ---
7480
7481class MultiTouchInputMapperTest : public TouchInputMapperTest {
7482protected:
7483 void prepareAxes(int axes);
7484
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007485 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7486 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7487 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7488 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7489 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7490 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7491 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7492 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7493 void processId(MultiTouchInputMapper& mapper, int32_t id);
7494 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7495 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7496 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007497 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007498 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007499 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7500 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007501};
7502
7503void MultiTouchInputMapperTest::prepareAxes(int axes) {
7504 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007505 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007507 }
7508 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7510 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007511 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007512 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7513 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007514 }
7515 }
7516 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007517 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7518 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007519 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007520 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007521 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007522 }
7523 }
7524 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007525 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7526 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007527 }
7528 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007529 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7530 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007531 }
7532 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007533 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7534 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007535 }
7536 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007537 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7538 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007539 }
7540 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007541 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7542 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007543 }
7544 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007545 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546 }
7547}
7548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007549void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7550 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553}
7554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007555void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7556 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007558}
7559
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007560void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7561 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007563}
7564
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007565void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007567}
7568
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007569void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007571}
7572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007573void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7574 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007576}
7577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007578void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007580}
7581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007582void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007584}
7585
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007586void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007588}
7589
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007590void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007592}
7593
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007594void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007596}
7597
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007598void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7599 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007601}
7602
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007603void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7604 int32_t value) {
7605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7607}
7608
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007609void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611}
7612
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007613void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7614 nsecs_t readTime) {
7615 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007616}
7617
Michael Wrightd02c5b62014-02-10 15:10:22 -08007618TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619 addConfigurationProperty("touch.deviceType", "touchScreen");
7620 prepareDisplay(DISPLAY_ORIENTATION_0);
7621 prepareAxes(POSITION);
7622 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007623 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007624
arthurhungdcef2dc2020-08-11 14:47:50 +08007625 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626
7627 NotifyMotionArgs motionArgs;
7628
7629 // Two fingers down at once.
7630 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7631 processPosition(mapper, x1, y1);
7632 processMTSync(mapper);
7633 processPosition(mapper, x2, y2);
7634 processMTSync(mapper);
7635 processSync(mapper);
7636
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7638 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7639 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7640 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7641 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7642 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7643 ASSERT_EQ(0, motionArgs.flags);
7644 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7645 ASSERT_EQ(0, motionArgs.buttonState);
7646 ASSERT_EQ(0, motionArgs.edgeFlags);
7647 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7648 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7651 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7652 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7653 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7655
7656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7657 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7658 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7659 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7660 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007661 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007662 ASSERT_EQ(0, motionArgs.flags);
7663 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7664 ASSERT_EQ(0, motionArgs.buttonState);
7665 ASSERT_EQ(0, motionArgs.edgeFlags);
7666 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7667 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7669 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7672 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7674 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7675 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7676 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7677 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7678
7679 // Move.
7680 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7681 processPosition(mapper, x1, y1);
7682 processMTSync(mapper);
7683 processPosition(mapper, x2, y2);
7684 processMTSync(mapper);
7685 processSync(mapper);
7686
7687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7688 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7689 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7690 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7691 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7693 ASSERT_EQ(0, motionArgs.flags);
7694 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7695 ASSERT_EQ(0, motionArgs.buttonState);
7696 ASSERT_EQ(0, motionArgs.edgeFlags);
7697 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7698 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7699 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7700 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7703 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7705 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7706 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7707 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7709
7710 // First finger up.
7711 x2 += 15; y2 -= 20;
7712 processPosition(mapper, x2, y2);
7713 processMTSync(mapper);
7714 processSync(mapper);
7715
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7717 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7718 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7719 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7720 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007721 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007722 ASSERT_EQ(0, motionArgs.flags);
7723 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7724 ASSERT_EQ(0, motionArgs.buttonState);
7725 ASSERT_EQ(0, motionArgs.edgeFlags);
7726 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7727 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7728 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7729 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7732 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7734 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7735 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7736 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7737 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7738
7739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7740 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7741 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7742 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7743 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7745 ASSERT_EQ(0, motionArgs.flags);
7746 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7747 ASSERT_EQ(0, motionArgs.buttonState);
7748 ASSERT_EQ(0, motionArgs.edgeFlags);
7749 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7750 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7753 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7754 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7755 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7756 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7757
7758 // Move.
7759 x2 += 20; y2 -= 25;
7760 processPosition(mapper, x2, y2);
7761 processMTSync(mapper);
7762 processSync(mapper);
7763
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7765 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7766 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7767 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7768 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7770 ASSERT_EQ(0, motionArgs.flags);
7771 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7772 ASSERT_EQ(0, motionArgs.buttonState);
7773 ASSERT_EQ(0, motionArgs.edgeFlags);
7774 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7775 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7778 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7779 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7780 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7782
7783 // New finger down.
7784 int32_t x3 = 700, y3 = 300;
7785 processPosition(mapper, x2, y2);
7786 processMTSync(mapper);
7787 processPosition(mapper, x3, y3);
7788 processMTSync(mapper);
7789 processSync(mapper);
7790
7791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7792 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7793 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7794 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7795 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007796 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007797 ASSERT_EQ(0, motionArgs.flags);
7798 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7799 ASSERT_EQ(0, motionArgs.buttonState);
7800 ASSERT_EQ(0, motionArgs.edgeFlags);
7801 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7802 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7804 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7805 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7807 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7808 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7809 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7810 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7811 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7812 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7813
7814 // Second finger up.
7815 x3 += 30; y3 -= 20;
7816 processPosition(mapper, x3, y3);
7817 processMTSync(mapper);
7818 processSync(mapper);
7819
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7821 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7822 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7823 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7824 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007825 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007826 ASSERT_EQ(0, motionArgs.flags);
7827 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7828 ASSERT_EQ(0, motionArgs.buttonState);
7829 ASSERT_EQ(0, motionArgs.edgeFlags);
7830 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7831 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7833 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7836 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7838 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7839 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7840 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7841 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7842
7843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7844 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7845 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7846 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7847 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7849 ASSERT_EQ(0, motionArgs.flags);
7850 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7851 ASSERT_EQ(0, motionArgs.buttonState);
7852 ASSERT_EQ(0, motionArgs.edgeFlags);
7853 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7854 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7857 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7858 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7859 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7860 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7861
7862 // Last finger up.
7863 processMTSync(mapper);
7864 processSync(mapper);
7865
7866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7867 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7868 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7869 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7870 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7871 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7872 ASSERT_EQ(0, motionArgs.flags);
7873 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7874 ASSERT_EQ(0, motionArgs.buttonState);
7875 ASSERT_EQ(0, motionArgs.edgeFlags);
7876 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7877 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7878 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7880 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7881 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7882 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7883 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7884
7885 // Should not have sent any more keys or motions.
7886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7888}
7889
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007890TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7891 addConfigurationProperty("touch.deviceType", "touchScreen");
7892 prepareDisplay(DISPLAY_ORIENTATION_0);
7893
7894 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7895 /*fuzz*/ 0, /*resolution*/ 10);
7896 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7897 /*fuzz*/ 0, /*resolution*/ 11);
7898 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7899 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7900 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7901 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7902 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7903 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7904 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7905 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7906
7907 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7908
7909 // X and Y axes
7910 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7911 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7912 // Touch major and minor
7913 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7914 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7915 // Tool major and minor
7916 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7917 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7918}
7919
7920TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7921 addConfigurationProperty("touch.deviceType", "touchScreen");
7922 prepareDisplay(DISPLAY_ORIENTATION_0);
7923
7924 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7925 /*fuzz*/ 0, /*resolution*/ 10);
7926 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7927 /*fuzz*/ 0, /*resolution*/ 11);
7928
7929 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7930
7931 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7932
7933 // Touch major and minor
7934 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7935 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7936 // Tool major and minor
7937 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7938 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7939}
7940
Michael Wrightd02c5b62014-02-10 15:10:22 -08007941TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007942 addConfigurationProperty("touch.deviceType", "touchScreen");
7943 prepareDisplay(DISPLAY_ORIENTATION_0);
7944 prepareAxes(POSITION | ID);
7945 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007946 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007947
arthurhungdcef2dc2020-08-11 14:47:50 +08007948 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007949
7950 NotifyMotionArgs motionArgs;
7951
7952 // Two fingers down at once.
7953 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7954 processPosition(mapper, x1, y1);
7955 processId(mapper, 1);
7956 processMTSync(mapper);
7957 processPosition(mapper, x2, y2);
7958 processId(mapper, 2);
7959 processMTSync(mapper);
7960 processSync(mapper);
7961
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7963 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7964 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7965 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7966 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7968 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7969
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007971 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7973 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7975 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7978 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7980 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7981
7982 // Move.
7983 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7984 processPosition(mapper, x1, y1);
7985 processId(mapper, 1);
7986 processMTSync(mapper);
7987 processPosition(mapper, x2, y2);
7988 processId(mapper, 2);
7989 processMTSync(mapper);
7990 processSync(mapper);
7991
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7993 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7994 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7995 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7996 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7997 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8000 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8002 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8003
8004 // First finger up.
8005 x2 += 15; y2 -= 20;
8006 processPosition(mapper, x2, y2);
8007 processId(mapper, 2);
8008 processMTSync(mapper);
8009 processSync(mapper);
8010
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008012 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8014 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8015 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8016 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8019 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8021 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8022
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8025 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8026 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8027 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8028 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8029 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8030
8031 // Move.
8032 x2 += 20; y2 -= 25;
8033 processPosition(mapper, x2, y2);
8034 processId(mapper, 2);
8035 processMTSync(mapper);
8036 processSync(mapper);
8037
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8040 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8041 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8044 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8045
8046 // New finger down.
8047 int32_t x3 = 700, y3 = 300;
8048 processPosition(mapper, x2, y2);
8049 processId(mapper, 2);
8050 processMTSync(mapper);
8051 processPosition(mapper, x3, y3);
8052 processId(mapper, 3);
8053 processMTSync(mapper);
8054 processSync(mapper);
8055
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008057 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8059 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8061 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8062 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8064 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8066 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8067
8068 // Second finger up.
8069 x3 += 30; y3 -= 20;
8070 processPosition(mapper, x3, y3);
8071 processId(mapper, 3);
8072 processMTSync(mapper);
8073 processSync(mapper);
8074
8075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008076 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8078 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8079 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8080 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8081 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8082 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8083 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8085 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8086
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8089 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8090 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8093 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8094
8095 // Last finger up.
8096 processMTSync(mapper);
8097 processSync(mapper);
8098
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8101 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8102 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8105 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8106
8107 // Should not have sent any more keys or motions.
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8110}
8111
8112TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113 addConfigurationProperty("touch.deviceType", "touchScreen");
8114 prepareDisplay(DISPLAY_ORIENTATION_0);
8115 prepareAxes(POSITION | ID | SLOT);
8116 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008117 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118
arthurhungdcef2dc2020-08-11 14:47:50 +08008119 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120
8121 NotifyMotionArgs motionArgs;
8122
8123 // Two fingers down at once.
8124 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8125 processPosition(mapper, x1, y1);
8126 processId(mapper, 1);
8127 processSlot(mapper, 1);
8128 processPosition(mapper, x2, y2);
8129 processId(mapper, 2);
8130 processSync(mapper);
8131
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8134 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8135 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8136 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8137 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8138 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8139
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008141 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8143 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8145 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8148 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8150 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8151
8152 // Move.
8153 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8154 processSlot(mapper, 0);
8155 processPosition(mapper, x1, y1);
8156 processSlot(mapper, 1);
8157 processPosition(mapper, x2, y2);
8158 processSync(mapper);
8159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8162 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8163 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8165 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8168 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8170 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8171
8172 // First finger up.
8173 x2 += 15; y2 -= 20;
8174 processSlot(mapper, 0);
8175 processId(mapper, -1);
8176 processSlot(mapper, 1);
8177 processPosition(mapper, x2, y2);
8178 processSync(mapper);
8179
8180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008181 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8183 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8185 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8188 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8189 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8190 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8191
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8194 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8195 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8197 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8198 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8199
8200 // Move.
8201 x2 += 20; y2 -= 25;
8202 processPosition(mapper, x2, y2);
8203 processSync(mapper);
8204
8205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8206 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8207 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8208 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8211 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8212
8213 // New finger down.
8214 int32_t x3 = 700, y3 = 300;
8215 processPosition(mapper, x2, y2);
8216 processSlot(mapper, 0);
8217 processId(mapper, 3);
8218 processPosition(mapper, x3, y3);
8219 processSync(mapper);
8220
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008222 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8224 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8226 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8229 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8231 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8232
8233 // Second finger up.
8234 x3 += 30; y3 -= 20;
8235 processSlot(mapper, 1);
8236 processId(mapper, -1);
8237 processSlot(mapper, 0);
8238 processPosition(mapper, x3, y3);
8239 processSync(mapper);
8240
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008242 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8244 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8246 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8248 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8249 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8251 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8252
8253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8255 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8259 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8260
8261 // Last finger up.
8262 processId(mapper, -1);
8263 processSync(mapper);
8264
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8266 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8267 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8268 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8271 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8272
8273 // Should not have sent any more keys or motions.
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8276}
8277
8278TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 addConfigurationProperty("touch.deviceType", "touchScreen");
8280 prepareDisplay(DISPLAY_ORIENTATION_0);
8281 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008282 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283
8284 // These calculations are based on the input device calibration documentation.
8285 int32_t rawX = 100;
8286 int32_t rawY = 200;
8287 int32_t rawTouchMajor = 7;
8288 int32_t rawTouchMinor = 6;
8289 int32_t rawToolMajor = 9;
8290 int32_t rawToolMinor = 8;
8291 int32_t rawPressure = 11;
8292 int32_t rawDistance = 0;
8293 int32_t rawOrientation = 3;
8294 int32_t id = 5;
8295
8296 float x = toDisplayX(rawX);
8297 float y = toDisplayY(rawY);
8298 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8299 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8300 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8301 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8302 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8303 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8304 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8305 float distance = float(rawDistance);
8306
8307 processPosition(mapper, rawX, rawY);
8308 processTouchMajor(mapper, rawTouchMajor);
8309 processTouchMinor(mapper, rawTouchMinor);
8310 processToolMajor(mapper, rawToolMajor);
8311 processToolMinor(mapper, rawToolMinor);
8312 processPressure(mapper, rawPressure);
8313 processOrientation(mapper, rawOrientation);
8314 processDistance(mapper, rawDistance);
8315 processId(mapper, id);
8316 processMTSync(mapper);
8317 processSync(mapper);
8318
8319 NotifyMotionArgs args;
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8321 ASSERT_EQ(0, args.pointerProperties[0].id);
8322 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8323 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8324 orientation, distance));
8325}
8326
8327TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008328 addConfigurationProperty("touch.deviceType", "touchScreen");
8329 prepareDisplay(DISPLAY_ORIENTATION_0);
8330 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8331 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008332 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008333
8334 // These calculations are based on the input device calibration documentation.
8335 int32_t rawX = 100;
8336 int32_t rawY = 200;
8337 int32_t rawTouchMajor = 140;
8338 int32_t rawTouchMinor = 120;
8339 int32_t rawToolMajor = 180;
8340 int32_t rawToolMinor = 160;
8341
8342 float x = toDisplayX(rawX);
8343 float y = toDisplayY(rawY);
8344 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8345 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8346 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8347 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8348 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8349
8350 processPosition(mapper, rawX, rawY);
8351 processTouchMajor(mapper, rawTouchMajor);
8352 processTouchMinor(mapper, rawTouchMinor);
8353 processToolMajor(mapper, rawToolMajor);
8354 processToolMinor(mapper, rawToolMinor);
8355 processMTSync(mapper);
8356 processSync(mapper);
8357
8358 NotifyMotionArgs args;
8359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8361 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8362}
8363
8364TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008365 addConfigurationProperty("touch.deviceType", "touchScreen");
8366 prepareDisplay(DISPLAY_ORIENTATION_0);
8367 prepareAxes(POSITION | TOUCH | TOOL);
8368 addConfigurationProperty("touch.size.calibration", "diameter");
8369 addConfigurationProperty("touch.size.scale", "10");
8370 addConfigurationProperty("touch.size.bias", "160");
8371 addConfigurationProperty("touch.size.isSummed", "1");
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 // Note: We only provide a single common touch/tool value because the device is assumed
8376 // not to emit separate values for each pointer (isSummed = 1).
8377 int32_t rawX = 100;
8378 int32_t rawY = 200;
8379 int32_t rawX2 = 150;
8380 int32_t rawY2 = 250;
8381 int32_t rawTouchMajor = 5;
8382 int32_t rawToolMajor = 8;
8383
8384 float x = toDisplayX(rawX);
8385 float y = toDisplayY(rawY);
8386 float x2 = toDisplayX(rawX2);
8387 float y2 = toDisplayY(rawY2);
8388 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8389 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8390 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8391
8392 processPosition(mapper, rawX, rawY);
8393 processTouchMajor(mapper, rawTouchMajor);
8394 processToolMajor(mapper, rawToolMajor);
8395 processMTSync(mapper);
8396 processPosition(mapper, rawX2, rawY2);
8397 processTouchMajor(mapper, rawTouchMajor);
8398 processToolMajor(mapper, rawToolMajor);
8399 processMTSync(mapper);
8400 processSync(mapper);
8401
8402 NotifyMotionArgs args;
8403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8404 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8405
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008407 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408 ASSERT_EQ(size_t(2), args.pointerCount);
8409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8410 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8412 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8413}
8414
8415TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008416 addConfigurationProperty("touch.deviceType", "touchScreen");
8417 prepareDisplay(DISPLAY_ORIENTATION_0);
8418 prepareAxes(POSITION | TOUCH | TOOL);
8419 addConfigurationProperty("touch.size.calibration", "area");
8420 addConfigurationProperty("touch.size.scale", "43");
8421 addConfigurationProperty("touch.size.bias", "3");
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 = 5;
8428 int32_t rawToolMajor = 8;
8429
8430 float x = toDisplayX(rawX);
8431 float y = toDisplayY(rawY);
8432 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8433 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8434 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8435
8436 processPosition(mapper, rawX, rawY);
8437 processTouchMajor(mapper, rawTouchMajor);
8438 processToolMajor(mapper, rawToolMajor);
8439 processMTSync(mapper);
8440 processSync(mapper);
8441
8442 NotifyMotionArgs args;
8443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8444 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8445 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8446}
8447
8448TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008449 addConfigurationProperty("touch.deviceType", "touchScreen");
8450 prepareDisplay(DISPLAY_ORIENTATION_0);
8451 prepareAxes(POSITION | PRESSURE);
8452 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8453 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008454 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008455
Michael Wrightaa449c92017-12-13 21:21:43 +00008456 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008457 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008458 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8459 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8460 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8461
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462 // These calculations are based on the input device calibration documentation.
8463 int32_t rawX = 100;
8464 int32_t rawY = 200;
8465 int32_t rawPressure = 60;
8466
8467 float x = toDisplayX(rawX);
8468 float y = toDisplayY(rawY);
8469 float pressure = float(rawPressure) * 0.01f;
8470
8471 processPosition(mapper, rawX, rawY);
8472 processPressure(mapper, rawPressure);
8473 processMTSync(mapper);
8474 processSync(mapper);
8475
8476 NotifyMotionArgs args;
8477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8478 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8479 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8480}
8481
8482TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483 addConfigurationProperty("touch.deviceType", "touchScreen");
8484 prepareDisplay(DISPLAY_ORIENTATION_0);
8485 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008486 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008487
8488 NotifyMotionArgs motionArgs;
8489 NotifyKeyArgs keyArgs;
8490
8491 processId(mapper, 1);
8492 processPosition(mapper, 100, 200);
8493 processSync(mapper);
8494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8495 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8496 ASSERT_EQ(0, motionArgs.buttonState);
8497
8498 // press BTN_LEFT, release BTN_LEFT
8499 processKey(mapper, BTN_LEFT, 1);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8503 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8504
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8506 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8507 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8508
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509 processKey(mapper, BTN_LEFT, 0);
8510 processSync(mapper);
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008512 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008513 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008514
8515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008516 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008517 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008518
8519 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8520 processKey(mapper, BTN_RIGHT, 1);
8521 processKey(mapper, BTN_MIDDLE, 1);
8522 processSync(mapper);
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8525 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8526 motionArgs.buttonState);
8527
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8529 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8530 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8531
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8534 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8535 motionArgs.buttonState);
8536
Michael Wrightd02c5b62014-02-10 15:10:22 -08008537 processKey(mapper, BTN_RIGHT, 0);
8538 processSync(mapper);
8539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008540 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008541 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008542
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008545 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008546
8547 processKey(mapper, BTN_MIDDLE, 0);
8548 processSync(mapper);
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008552
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008555 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008556
8557 // press BTN_BACK, release BTN_BACK
8558 processKey(mapper, BTN_BACK, 1);
8559 processSync(mapper);
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8561 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8562 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008563
Michael Wrightd02c5b62014-02-10 15:10:22 -08008564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008566 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8567
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8570 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008571
8572 processKey(mapper, BTN_BACK, 0);
8573 processSync(mapper);
8574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008575 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008576 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008577
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008580 ASSERT_EQ(0, motionArgs.buttonState);
8581
Michael Wrightd02c5b62014-02-10 15:10:22 -08008582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8583 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8584 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8585
8586 // press BTN_SIDE, release BTN_SIDE
8587 processKey(mapper, BTN_SIDE, 1);
8588 processSync(mapper);
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8590 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8591 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008592
Michael Wrightd02c5b62014-02-10 15:10:22 -08008593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008595 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8596
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8598 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8599 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008600
8601 processKey(mapper, BTN_SIDE, 0);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008604 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008605 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008606
8607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008608 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008609 ASSERT_EQ(0, motionArgs.buttonState);
8610
Michael Wrightd02c5b62014-02-10 15:10:22 -08008611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8612 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8613 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8614
8615 // press BTN_FORWARD, release BTN_FORWARD
8616 processKey(mapper, BTN_FORWARD, 1);
8617 processSync(mapper);
8618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8619 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8620 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008621
Michael Wrightd02c5b62014-02-10 15:10:22 -08008622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008624 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8625
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8628 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008629
8630 processKey(mapper, BTN_FORWARD, 0);
8631 processSync(mapper);
8632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008634 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008635
8636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008638 ASSERT_EQ(0, motionArgs.buttonState);
8639
Michael Wrightd02c5b62014-02-10 15:10:22 -08008640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8641 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8642 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8643
8644 // press BTN_EXTRA, release BTN_EXTRA
8645 processKey(mapper, BTN_EXTRA, 1);
8646 processSync(mapper);
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8648 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8649 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008650
Michael Wrightd02c5b62014-02-10 15:10:22 -08008651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008653 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8654
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8656 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8657 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008658
8659 processKey(mapper, BTN_EXTRA, 0);
8660 processSync(mapper);
8661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008662 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008663 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008664
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008667 ASSERT_EQ(0, motionArgs.buttonState);
8668
Michael Wrightd02c5b62014-02-10 15:10:22 -08008669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8670 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8671 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8672
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8674
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675 // press BTN_STYLUS, release BTN_STYLUS
8676 processKey(mapper, BTN_STYLUS, 1);
8677 processSync(mapper);
8678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008680 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8681
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8683 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8684 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008685
8686 processKey(mapper, BTN_STYLUS, 0);
8687 processSync(mapper);
8688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008689 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008690 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008691
8692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008694 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008695
8696 // press BTN_STYLUS2, release BTN_STYLUS2
8697 processKey(mapper, BTN_STYLUS2, 1);
8698 processSync(mapper);
8699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008701 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8702
8703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8704 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8705 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008706
8707 processKey(mapper, BTN_STYLUS2, 0);
8708 processSync(mapper);
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008710 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008711 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008712
8713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008715 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008716
8717 // release touch
8718 processId(mapper, -1);
8719 processSync(mapper);
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8721 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8722 ASSERT_EQ(0, motionArgs.buttonState);
8723}
8724
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008725TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8726 addConfigurationProperty("touch.deviceType", "touchScreen");
8727 prepareDisplay(DISPLAY_ORIENTATION_0);
8728 prepareAxes(POSITION | ID | SLOT);
8729 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8730
8731 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8732 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8733
8734 // Touch down.
8735 processId(mapper, 1);
8736 processPosition(mapper, 100, 200);
8737 processSync(mapper);
8738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8739 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8740
8741 // Press and release button mapped to the primary stylus button.
8742 processKey(mapper, BTN_A, 1);
8743 processSync(mapper);
8744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8745 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8746 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8748 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8749 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8750
8751 processKey(mapper, BTN_A, 0);
8752 processSync(mapper);
8753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8754 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8756 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8757
8758 // Press and release the HID usage mapped to the secondary stylus button.
8759 processHidUsage(mapper, 0xabcd, 1);
8760 processSync(mapper);
8761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8762 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8763 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8765 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8766 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8767
8768 processHidUsage(mapper, 0xabcd, 0);
8769 processSync(mapper);
8770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8771 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8773 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8774
8775 // Release touch.
8776 processId(mapper, -1);
8777 processSync(mapper);
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8779 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8780}
8781
Michael Wrightd02c5b62014-02-10 15:10:22 -08008782TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008783 addConfigurationProperty("touch.deviceType", "touchScreen");
8784 prepareDisplay(DISPLAY_ORIENTATION_0);
8785 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008786 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008787
8788 NotifyMotionArgs motionArgs;
8789
8790 // default tool type is finger
8791 processId(mapper, 1);
8792 processPosition(mapper, 100, 200);
8793 processSync(mapper);
8794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8795 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8796 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8797
8798 // eraser
8799 processKey(mapper, BTN_TOOL_RUBBER, 1);
8800 processSync(mapper);
8801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8804
8805 // stylus
8806 processKey(mapper, BTN_TOOL_RUBBER, 0);
8807 processKey(mapper, BTN_TOOL_PEN, 1);
8808 processSync(mapper);
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8812
8813 // brush
8814 processKey(mapper, BTN_TOOL_PEN, 0);
8815 processKey(mapper, BTN_TOOL_BRUSH, 1);
8816 processSync(mapper);
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8820
8821 // pencil
8822 processKey(mapper, BTN_TOOL_BRUSH, 0);
8823 processKey(mapper, BTN_TOOL_PENCIL, 1);
8824 processSync(mapper);
8825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8828
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008829 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008830 processKey(mapper, BTN_TOOL_PENCIL, 0);
8831 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8832 processSync(mapper);
8833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8836
8837 // mouse
8838 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8839 processKey(mapper, BTN_TOOL_MOUSE, 1);
8840 processSync(mapper);
8841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8844
8845 // lens
8846 processKey(mapper, BTN_TOOL_MOUSE, 0);
8847 processKey(mapper, BTN_TOOL_LENS, 1);
8848 processSync(mapper);
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8851 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8852
8853 // double-tap
8854 processKey(mapper, BTN_TOOL_LENS, 0);
8855 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8856 processSync(mapper);
8857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8859 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8860
8861 // triple-tap
8862 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8863 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8864 processSync(mapper);
8865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8868
8869 // quad-tap
8870 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8871 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8872 processSync(mapper);
8873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8876
8877 // finger
8878 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8879 processKey(mapper, BTN_TOOL_FINGER, 1);
8880 processSync(mapper);
8881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8884
8885 // stylus trumps finger
8886 processKey(mapper, BTN_TOOL_PEN, 1);
8887 processSync(mapper);
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8891
8892 // eraser trumps stylus
8893 processKey(mapper, BTN_TOOL_RUBBER, 1);
8894 processSync(mapper);
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8898
8899 // mouse trumps eraser
8900 processKey(mapper, BTN_TOOL_MOUSE, 1);
8901 processSync(mapper);
8902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8905
8906 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8907 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8908 processSync(mapper);
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8912
8913 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8914 processToolType(mapper, MT_TOOL_PEN);
8915 processSync(mapper);
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8918 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8919
8920 // back to default tool type
8921 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8922 processKey(mapper, BTN_TOOL_MOUSE, 0);
8923 processKey(mapper, BTN_TOOL_RUBBER, 0);
8924 processKey(mapper, BTN_TOOL_PEN, 0);
8925 processKey(mapper, BTN_TOOL_FINGER, 0);
8926 processSync(mapper);
8927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8929 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8930}
8931
8932TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008933 addConfigurationProperty("touch.deviceType", "touchScreen");
8934 prepareDisplay(DISPLAY_ORIENTATION_0);
8935 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008936 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008937 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008938
8939 NotifyMotionArgs motionArgs;
8940
8941 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8942 processId(mapper, 1);
8943 processPosition(mapper, 100, 200);
8944 processSync(mapper);
8945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8946 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8948 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8949
8950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8951 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8953 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8954
8955 // move a little
8956 processPosition(mapper, 150, 250);
8957 processSync(mapper);
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8959 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8961 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8962
8963 // down when BTN_TOUCH is pressed, pressure defaults to 1
8964 processKey(mapper, BTN_TOUCH, 1);
8965 processSync(mapper);
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8967 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8969 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8970
8971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8972 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8974 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8975
8976 // up when BTN_TOUCH is released, hover restored
8977 processKey(mapper, BTN_TOUCH, 0);
8978 processSync(mapper);
8979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8980 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8982 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8983
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8985 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8987 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8988
8989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8990 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8992 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8993
8994 // exit hover when pointer goes away
8995 processId(mapper, -1);
8996 processSync(mapper);
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8998 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9000 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9001}
9002
9003TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009004 addConfigurationProperty("touch.deviceType", "touchScreen");
9005 prepareDisplay(DISPLAY_ORIENTATION_0);
9006 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009007 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009008
9009 NotifyMotionArgs motionArgs;
9010
9011 // initially hovering because pressure is 0
9012 processId(mapper, 1);
9013 processPosition(mapper, 100, 200);
9014 processPressure(mapper, 0);
9015 processSync(mapper);
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9017 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9019 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9020
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9022 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9024 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9025
9026 // move a little
9027 processPosition(mapper, 150, 250);
9028 processSync(mapper);
9029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9030 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9031 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9032 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9033
9034 // down when pressure becomes non-zero
9035 processPressure(mapper, RAW_PRESSURE_MAX);
9036 processSync(mapper);
9037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9038 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9040 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9041
9042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9043 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9044 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9045 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9046
9047 // up when pressure becomes 0, hover restored
9048 processPressure(mapper, 0);
9049 processSync(mapper);
9050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9051 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9053 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9054
9055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9056 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9058 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9059
9060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9061 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9063 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9064
9065 // exit hover when pointer goes away
9066 processId(mapper, -1);
9067 processSync(mapper);
9068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9069 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9071 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9072}
9073
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009074/**
9075 * Set the input device port <--> display port associations, and check that the
9076 * events are routed to the display that matches the display port.
9077 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9078 */
9079TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009080 const std::string usb2 = "USB2";
9081 const uint8_t hdmi1 = 0;
9082 const uint8_t hdmi2 = 1;
9083 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009084 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009085
9086 addConfigurationProperty("touch.deviceType", "touchScreen");
9087 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009088 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009089
9090 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9091 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9092
9093 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9094 // for this input device is specified, and the matching viewport is not present,
9095 // the input device should be disabled (at the mapper level).
9096
9097 // Add viewport for display 2 on hdmi2
9098 prepareSecondaryDisplay(type, hdmi2);
9099 // Send a touch event
9100 processPosition(mapper, 100, 100);
9101 processSync(mapper);
9102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9103
9104 // Add viewport for display 1 on hdmi1
9105 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
9106 // Send a touch event again
9107 processPosition(mapper, 100, 100);
9108 processSync(mapper);
9109
9110 NotifyMotionArgs args;
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9112 ASSERT_EQ(DISPLAY_ID, args.displayId);
9113}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009114
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009115TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9116 addConfigurationProperty("touch.deviceType", "touchScreen");
9117 prepareAxes(POSITION);
9118 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9119
9120 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9121
9122 prepareDisplay(DISPLAY_ORIENTATION_0);
9123 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9124
9125 // Send a touch event
9126 processPosition(mapper, 100, 100);
9127 processSync(mapper);
9128
9129 NotifyMotionArgs args;
9130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9131 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9132}
9133
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009134TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009135 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009136 std::shared_ptr<FakePointerController> fakePointerController =
9137 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009138 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009139 fakePointerController->setPosition(100, 200);
9140 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009141 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009142
Garfield Tan888a6a42020-01-09 11:39:16 -08009143 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009144 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009145
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009146 prepareDisplay(DISPLAY_ORIENTATION_0);
9147 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009148 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009149
9150 // Check source is mouse that would obtain the PointerController.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009151 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009152
9153 NotifyMotionArgs motionArgs;
9154 processPosition(mapper, 100, 100);
9155 processSync(mapper);
9156
9157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9158 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9159 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9160}
9161
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009162/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009163 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9164 */
9165TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9166 addConfigurationProperty("touch.deviceType", "touchScreen");
9167 prepareAxes(POSITION);
9168 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9169
9170 prepareDisplay(DISPLAY_ORIENTATION_0);
9171 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9172 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9173 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9174 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9175
9176 NotifyMotionArgs args;
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9178 ASSERT_EQ(26, args.readTime);
9179
9180 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9181 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9182 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9183
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9185 ASSERT_EQ(33, args.readTime);
9186}
9187
9188/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009189 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9190 * events should not be delivered to the listener.
9191 */
9192TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9193 addConfigurationProperty("touch.deviceType", "touchScreen");
9194 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9195 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9196 ViewportType::INTERNAL);
9197 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9198 prepareAxes(POSITION);
9199 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9200
9201 NotifyMotionArgs motionArgs;
9202 processPosition(mapper, 100, 100);
9203 processSync(mapper);
9204
9205 mFakeListener->assertNotifyMotionWasNotCalled();
9206}
9207
Garfield Tanc734e4f2021-01-15 20:01:39 -08009208TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9209 addConfigurationProperty("touch.deviceType", "touchScreen");
9210 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9211 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
9212 ViewportType::INTERNAL);
9213 std::optional<DisplayViewport> optionalDisplayViewport =
9214 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9215 ASSERT_TRUE(optionalDisplayViewport.has_value());
9216 DisplayViewport displayViewport = *optionalDisplayViewport;
9217
9218 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9219 prepareAxes(POSITION);
9220 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9221
9222 // Finger down
9223 int32_t x = 100, y = 100;
9224 processPosition(mapper, x, y);
9225 processSync(mapper);
9226
9227 NotifyMotionArgs motionArgs;
9228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9229 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9230
9231 // Deactivate display viewport
9232 displayViewport.isActive = false;
9233 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9234 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9235
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009236 // The ongoing touch should be canceled immediately
9237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9238 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9239
9240 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009241 x += 10, y += 10;
9242 processPosition(mapper, x, y);
9243 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009245
9246 // Reactivate display viewport
9247 displayViewport.isActive = true;
9248 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9249 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9250
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009251 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009252 x += 10, y += 10;
9253 processPosition(mapper, x, y);
9254 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9256 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009257}
9258
Arthur Hung7c645402019-01-25 17:45:42 +08009259TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9260 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009261 prepareAxes(POSITION | ID | SLOT);
9262 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009263 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009264
9265 // Create the second touch screen device, and enable multi fingers.
9266 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009267 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009268 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009269 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009270 std::shared_ptr<InputDevice> device2 =
9271 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009272 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009273
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009274 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9275 0 /*flat*/, 0 /*fuzz*/);
9276 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9277 0 /*flat*/, 0 /*fuzz*/);
9278 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9279 0 /*flat*/, 0 /*fuzz*/);
9280 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9281 0 /*flat*/, 0 /*fuzz*/);
9282 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9283 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9284 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009285
9286 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009287 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009288 std::list<NotifyArgs> unused =
9289 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9290 0 /*changes*/);
9291 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009292
9293 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009294 std::shared_ptr<FakePointerController> fakePointerController =
9295 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009296 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009297
9298 // Setup policy for associated displays and show touches.
9299 const uint8_t hdmi1 = 0;
9300 const uint8_t hdmi2 = 1;
9301 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9302 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9303 mFakePolicy->setShowTouches(true);
9304
9305 // Create displays.
9306 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009307 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009308
9309 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009310 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9311 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9312 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009313
9314 // Two fingers down at default display.
9315 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9316 processPosition(mapper, x1, y1);
9317 processId(mapper, 1);
9318 processSlot(mapper, 1);
9319 processPosition(mapper, x2, y2);
9320 processId(mapper, 2);
9321 processSync(mapper);
9322
9323 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9324 fakePointerController->getSpots().find(DISPLAY_ID);
9325 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9326 ASSERT_EQ(size_t(2), iter->second.size());
9327
9328 // Two fingers down at second display.
9329 processPosition(mapper2, x1, y1);
9330 processId(mapper2, 1);
9331 processSlot(mapper2, 1);
9332 processPosition(mapper2, x2, y2);
9333 processId(mapper2, 2);
9334 processSync(mapper2);
9335
9336 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9337 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9338 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009339
9340 // Disable the show touches configuration and ensure the spots are cleared.
9341 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009342 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9343 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009344
9345 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009346}
9347
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009348TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009349 prepareAxes(POSITION);
9350 addConfigurationProperty("touch.deviceType", "touchScreen");
9351 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009352 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009353
9354 NotifyMotionArgs motionArgs;
9355 // Unrotated video frame
9356 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9357 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009358 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009359 processPosition(mapper, 100, 200);
9360 processSync(mapper);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9362 ASSERT_EQ(frames, motionArgs.videoFrames);
9363
9364 // Subsequent touch events should not have any videoframes
9365 // This is implemented separately in FakeEventHub,
9366 // but that should match the behaviour of TouchVideoDevice.
9367 processPosition(mapper, 200, 200);
9368 processSync(mapper);
9369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9370 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9371}
9372
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009373TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009374 prepareAxes(POSITION);
9375 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009376 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009377 // Unrotated video frame
9378 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9379 NotifyMotionArgs motionArgs;
9380
9381 // Test all 4 orientations
9382 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009383 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9384 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9385 clearViewports();
9386 prepareDisplay(orientation);
9387 std::vector<TouchVideoFrame> frames{frame};
9388 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9389 processPosition(mapper, 100, 200);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9392 ASSERT_EQ(frames, motionArgs.videoFrames);
9393 }
9394}
9395
9396TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9397 prepareAxes(POSITION);
9398 addConfigurationProperty("touch.deviceType", "touchScreen");
9399 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9400 // orientation-aware are affected by display rotation.
9401 addConfigurationProperty("touch.orientationAware", "0");
9402 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9403 // Unrotated video frame
9404 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9405 NotifyMotionArgs motionArgs;
9406
9407 // Test all 4 orientations
9408 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009409 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9410 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9411 clearViewports();
9412 prepareDisplay(orientation);
9413 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009414 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009415 processPosition(mapper, 100, 200);
9416 processSync(mapper);
9417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009418 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9419 // compared to the display. This is so that when the window transform (which contains the
9420 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9421 // window's coordinate space.
9422 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009423 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009424
9425 // Release finger.
9426 processSync(mapper);
9427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009428 }
9429}
9430
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009431TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009432 prepareAxes(POSITION);
9433 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009434 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009435 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9436 // so mix these.
9437 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9438 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9439 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9440 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9441 NotifyMotionArgs motionArgs;
9442
9443 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009444 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009445 processPosition(mapper, 100, 200);
9446 processSync(mapper);
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009448 ASSERT_EQ(frames, motionArgs.videoFrames);
9449}
9450
9451TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9452 prepareAxes(POSITION);
9453 addConfigurationProperty("touch.deviceType", "touchScreen");
9454 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9455 // orientation-aware are affected by display rotation.
9456 addConfigurationProperty("touch.orientationAware", "0");
9457 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9458 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9459 // so mix these.
9460 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9461 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9462 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9463 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9464 NotifyMotionArgs motionArgs;
9465
9466 prepareDisplay(DISPLAY_ORIENTATION_90);
9467 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9468 processPosition(mapper, 100, 200);
9469 processSync(mapper);
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9471 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9472 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9473 // compared to the display. This is so that when the window transform (which contains the
9474 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9475 // window's coordinate space.
9476 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9477 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009478 ASSERT_EQ(frames, motionArgs.videoFrames);
9479}
9480
Arthur Hung9da14732019-09-02 16:16:58 +08009481/**
9482 * If we had defined port associations, but the viewport is not ready, the touch device would be
9483 * expected to be disabled, and it should be enabled after the viewport has found.
9484 */
9485TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009486 constexpr uint8_t hdmi2 = 1;
9487 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009488 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009489
9490 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9491
9492 addConfigurationProperty("touch.deviceType", "touchScreen");
9493 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009494 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009495
9496 ASSERT_EQ(mDevice->isEnabled(), false);
9497
9498 // Add display on hdmi2, the device should be enabled and can receive touch event.
9499 prepareSecondaryDisplay(type, hdmi2);
9500 ASSERT_EQ(mDevice->isEnabled(), true);
9501
9502 // Send a touch event.
9503 processPosition(mapper, 100, 100);
9504 processSync(mapper);
9505
9506 NotifyMotionArgs args;
9507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9508 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9509}
9510
Arthur Hung421eb1c2020-01-16 00:09:42 +08009511TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009512 addConfigurationProperty("touch.deviceType", "touchScreen");
9513 prepareDisplay(DISPLAY_ORIENTATION_0);
9514 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009515 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009516
9517 NotifyMotionArgs motionArgs;
9518
9519 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9520 // finger down
9521 processId(mapper, 1);
9522 processPosition(mapper, x1, y1);
9523 processSync(mapper);
9524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9525 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9527
9528 // finger move
9529 processId(mapper, 1);
9530 processPosition(mapper, x2, y2);
9531 processSync(mapper);
9532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9535
9536 // finger up.
9537 processId(mapper, -1);
9538 processSync(mapper);
9539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9540 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9542
9543 // new finger down
9544 processId(mapper, 1);
9545 processPosition(mapper, x3, y3);
9546 processSync(mapper);
9547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9548 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9550}
9551
9552/**
arthurhungcc7f9802020-04-30 17:55:40 +08009553 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9554 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009555 */
arthurhungcc7f9802020-04-30 17:55:40 +08009556TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009557 addConfigurationProperty("touch.deviceType", "touchScreen");
9558 prepareDisplay(DISPLAY_ORIENTATION_0);
9559 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009560 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009561
9562 NotifyMotionArgs motionArgs;
9563
9564 // default tool type is finger
9565 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009566 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009567 processPosition(mapper, x1, y1);
9568 processSync(mapper);
9569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9570 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9572
9573 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9574 processToolType(mapper, MT_TOOL_PALM);
9575 processSync(mapper);
9576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9577 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9578
9579 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009580 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009581 processPosition(mapper, x2, y2);
9582 processSync(mapper);
9583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9584
9585 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009586 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009587 processSync(mapper);
9588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9589
9590 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009591 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009592 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009593 processPosition(mapper, x3, y3);
9594 processSync(mapper);
9595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9596 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9598}
9599
arthurhungbf89a482020-04-17 17:37:55 +08009600/**
arthurhungcc7f9802020-04-30 17:55:40 +08009601 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9602 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009603 */
arthurhungcc7f9802020-04-30 17:55:40 +08009604TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009605 addConfigurationProperty("touch.deviceType", "touchScreen");
9606 prepareDisplay(DISPLAY_ORIENTATION_0);
9607 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9609
9610 NotifyMotionArgs motionArgs;
9611
9612 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009613 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9614 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009615 processPosition(mapper, x1, y1);
9616 processSync(mapper);
9617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9618 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9620
9621 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009622 processSlot(mapper, SECOND_SLOT);
9623 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009624 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009625 processSync(mapper);
9626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009627 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009628 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9629
9630 // If the tool type of the first finger changes to MT_TOOL_PALM,
9631 // we expect to receive ACTION_POINTER_UP with cancel flag.
9632 processSlot(mapper, FIRST_SLOT);
9633 processId(mapper, FIRST_TRACKING_ID);
9634 processToolType(mapper, MT_TOOL_PALM);
9635 processSync(mapper);
9636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009637 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009638 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9639
9640 // The following MOVE events of second finger should be processed.
9641 processSlot(mapper, SECOND_SLOT);
9642 processId(mapper, SECOND_TRACKING_ID);
9643 processPosition(mapper, x2 + 1, y2 + 1);
9644 processSync(mapper);
9645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9647 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9648
9649 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9650 // it. Second finger receive move.
9651 processSlot(mapper, FIRST_SLOT);
9652 processId(mapper, INVALID_TRACKING_ID);
9653 processSync(mapper);
9654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9656 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9657
9658 // Second finger keeps moving.
9659 processSlot(mapper, SECOND_SLOT);
9660 processId(mapper, SECOND_TRACKING_ID);
9661 processPosition(mapper, x2 + 2, y2 + 2);
9662 processSync(mapper);
9663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9664 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9665 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9666
9667 // Second finger up.
9668 processId(mapper, INVALID_TRACKING_ID);
9669 processSync(mapper);
9670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9672 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9673}
9674
9675/**
9676 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9677 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9678 */
9679TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9680 addConfigurationProperty("touch.deviceType", "touchScreen");
9681 prepareDisplay(DISPLAY_ORIENTATION_0);
9682 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9683 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9684
9685 NotifyMotionArgs motionArgs;
9686
9687 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9688 // First finger down.
9689 processId(mapper, FIRST_TRACKING_ID);
9690 processPosition(mapper, x1, y1);
9691 processSync(mapper);
9692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9693 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9695
9696 // Second finger down.
9697 processSlot(mapper, SECOND_SLOT);
9698 processId(mapper, SECOND_TRACKING_ID);
9699 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009700 processSync(mapper);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009702 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9704
arthurhungcc7f9802020-04-30 17:55:40 +08009705 // If the tool type of the first finger changes to MT_TOOL_PALM,
9706 // we expect to receive ACTION_POINTER_UP with cancel flag.
9707 processSlot(mapper, FIRST_SLOT);
9708 processId(mapper, FIRST_TRACKING_ID);
9709 processToolType(mapper, MT_TOOL_PALM);
9710 processSync(mapper);
9711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009712 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009713 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9714
9715 // Second finger keeps moving.
9716 processSlot(mapper, SECOND_SLOT);
9717 processId(mapper, SECOND_TRACKING_ID);
9718 processPosition(mapper, x2 + 1, y2 + 1);
9719 processSync(mapper);
9720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9722
9723 // second finger becomes palm, receive cancel due to only 1 finger is active.
9724 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009725 processToolType(mapper, MT_TOOL_PALM);
9726 processSync(mapper);
9727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9728 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9729
arthurhungcc7f9802020-04-30 17:55:40 +08009730 // third finger down.
9731 processSlot(mapper, THIRD_SLOT);
9732 processId(mapper, THIRD_TRACKING_ID);
9733 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009734 processPosition(mapper, x3, y3);
9735 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9737 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009739 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9740
9741 // third finger move
9742 processId(mapper, THIRD_TRACKING_ID);
9743 processPosition(mapper, x3 + 1, y3 + 1);
9744 processSync(mapper);
9745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9747
9748 // first finger up, third finger receive move.
9749 processSlot(mapper, FIRST_SLOT);
9750 processId(mapper, INVALID_TRACKING_ID);
9751 processSync(mapper);
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9754 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9755
9756 // second finger up, third finger receive move.
9757 processSlot(mapper, SECOND_SLOT);
9758 processId(mapper, INVALID_TRACKING_ID);
9759 processSync(mapper);
9760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9762 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9763
9764 // third finger up.
9765 processSlot(mapper, THIRD_SLOT);
9766 processId(mapper, INVALID_TRACKING_ID);
9767 processSync(mapper);
9768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9769 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9770 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9771}
9772
9773/**
9774 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9775 * and the active finger could still be allowed to receive the events
9776 */
9777TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9778 addConfigurationProperty("touch.deviceType", "touchScreen");
9779 prepareDisplay(DISPLAY_ORIENTATION_0);
9780 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9781 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9782
9783 NotifyMotionArgs motionArgs;
9784
9785 // default tool type is finger
9786 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9787 processId(mapper, FIRST_TRACKING_ID);
9788 processPosition(mapper, x1, y1);
9789 processSync(mapper);
9790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9791 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9793
9794 // Second finger down.
9795 processSlot(mapper, SECOND_SLOT);
9796 processId(mapper, SECOND_TRACKING_ID);
9797 processPosition(mapper, x2, y2);
9798 processSync(mapper);
9799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009800 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9802
9803 // If the tool type of the second finger changes to MT_TOOL_PALM,
9804 // we expect to receive ACTION_POINTER_UP with cancel flag.
9805 processId(mapper, SECOND_TRACKING_ID);
9806 processToolType(mapper, MT_TOOL_PALM);
9807 processSync(mapper);
9808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009809 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009810 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9811
9812 // The following MOVE event should be processed.
9813 processSlot(mapper, FIRST_SLOT);
9814 processId(mapper, FIRST_TRACKING_ID);
9815 processPosition(mapper, x1 + 1, y1 + 1);
9816 processSync(mapper);
9817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9819 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9820
9821 // second finger up.
9822 processSlot(mapper, SECOND_SLOT);
9823 processId(mapper, INVALID_TRACKING_ID);
9824 processSync(mapper);
9825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9827
9828 // first finger keep moving
9829 processSlot(mapper, FIRST_SLOT);
9830 processId(mapper, FIRST_TRACKING_ID);
9831 processPosition(mapper, x1 + 2, y1 + 2);
9832 processSync(mapper);
9833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9835
9836 // first finger up.
9837 processId(mapper, INVALID_TRACKING_ID);
9838 processSync(mapper);
9839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9841 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009842}
9843
Arthur Hung9ad18942021-06-19 02:04:46 +00009844/**
9845 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9846 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9847 * cause slot be valid again.
9848 */
9849TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9850 addConfigurationProperty("touch.deviceType", "touchScreen");
9851 prepareDisplay(DISPLAY_ORIENTATION_0);
9852 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9853 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9854
9855 NotifyMotionArgs motionArgs;
9856
9857 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9858 // First finger down.
9859 processId(mapper, FIRST_TRACKING_ID);
9860 processPosition(mapper, x1, y1);
9861 processPressure(mapper, RAW_PRESSURE_MAX);
9862 processSync(mapper);
9863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9864 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9865 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9866
9867 // First finger move.
9868 processId(mapper, FIRST_TRACKING_ID);
9869 processPosition(mapper, x1 + 1, y1 + 1);
9870 processPressure(mapper, RAW_PRESSURE_MAX);
9871 processSync(mapper);
9872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9874 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9875
9876 // Second finger down.
9877 processSlot(mapper, SECOND_SLOT);
9878 processId(mapper, SECOND_TRACKING_ID);
9879 processPosition(mapper, x2, y2);
9880 processPressure(mapper, RAW_PRESSURE_MAX);
9881 processSync(mapper);
9882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009883 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009884 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9885
9886 // second finger up with some unexpected data.
9887 processSlot(mapper, SECOND_SLOT);
9888 processId(mapper, INVALID_TRACKING_ID);
9889 processPosition(mapper, x2, y2);
9890 processSync(mapper);
9891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009892 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009893 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9894
9895 // first finger up with some unexpected data.
9896 processSlot(mapper, FIRST_SLOT);
9897 processId(mapper, INVALID_TRACKING_ID);
9898 processPosition(mapper, x2, y2);
9899 processPressure(mapper, RAW_PRESSURE_MAX);
9900 processSync(mapper);
9901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9902 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9903 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9904}
9905
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009906TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9907 addConfigurationProperty("touch.deviceType", "touchScreen");
9908 prepareDisplay(DISPLAY_ORIENTATION_0);
9909 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9911
9912 // First finger down.
9913 processId(mapper, FIRST_TRACKING_ID);
9914 processPosition(mapper, 100, 200);
9915 processPressure(mapper, RAW_PRESSURE_MAX);
9916 processSync(mapper);
9917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9918 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9919
9920 // Second finger down.
9921 processSlot(mapper, SECOND_SLOT);
9922 processId(mapper, SECOND_TRACKING_ID);
9923 processPosition(mapper, 300, 400);
9924 processPressure(mapper, RAW_PRESSURE_MAX);
9925 processSync(mapper);
9926 ASSERT_NO_FATAL_FAILURE(
9927 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9928
9929 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009930 // preserved. Resetting should cancel the ongoing gesture.
9931 resetMapper(mapper, ARBITRARY_TIME);
9932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9933 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009934
9935 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9936 // the existing touch state to generate a down event.
9937 processPosition(mapper, 301, 302);
9938 processSync(mapper);
9939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9940 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9942 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9943
9944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9945}
9946
9947TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9948 addConfigurationProperty("touch.deviceType", "touchScreen");
9949 prepareDisplay(DISPLAY_ORIENTATION_0);
9950 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9951 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9952
9953 // First finger touches down and releases.
9954 processId(mapper, FIRST_TRACKING_ID);
9955 processPosition(mapper, 100, 200);
9956 processPressure(mapper, RAW_PRESSURE_MAX);
9957 processSync(mapper);
9958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9959 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9960 processId(mapper, INVALID_TRACKING_ID);
9961 processSync(mapper);
9962 ASSERT_NO_FATAL_FAILURE(
9963 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9964
9965 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9966 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009967 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9969
9970 // Send an empty sync frame. Since there are no pointers, no events are generated.
9971 processSync(mapper);
9972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9973}
9974
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009975// --- MultiTouchInputMapperTest_ExternalDevice ---
9976
9977class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9978protected:
Chris Yea52ade12020-08-27 16:49:20 -07009979 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009980};
9981
9982/**
9983 * Expect fallback to internal viewport if device is external and external viewport is not present.
9984 */
9985TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9986 prepareAxes(POSITION);
9987 addConfigurationProperty("touch.deviceType", "touchScreen");
9988 prepareDisplay(DISPLAY_ORIENTATION_0);
9989 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9990
9991 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9992
9993 NotifyMotionArgs motionArgs;
9994
9995 // Expect the event to be sent to the internal viewport,
9996 // because an external viewport is not present.
9997 processPosition(mapper, 100, 100);
9998 processSync(mapper);
9999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10000 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10001
10002 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010003 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010004 processPosition(mapper, 100, 100);
10005 processSync(mapper);
10006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10007 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10008}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010009
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010010TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10011 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10012 std::shared_ptr<FakePointerController> fakePointerController =
10013 std::make_shared<FakePointerController>();
10014 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10015 fakePointerController->setPosition(0, 0);
10016 fakePointerController->setButtonState(0);
10017
10018 // prepare device and capture
10019 prepareDisplay(DISPLAY_ORIENTATION_0);
10020 prepareAxes(POSITION | ID | SLOT);
10021 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10022 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10023 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010024 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010025 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10026
10027 // captured touchpad should be a touchpad source
10028 NotifyDeviceResetArgs resetArgs;
10029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10030 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10031
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010032 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010033
10034 const InputDeviceInfo::MotionRange* relRangeX =
10035 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10036 ASSERT_NE(relRangeX, nullptr);
10037 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10038 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10039 const InputDeviceInfo::MotionRange* relRangeY =
10040 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10041 ASSERT_NE(relRangeY, nullptr);
10042 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10043 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10044
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010045 // run captured pointer tests - note that this is unscaled, so input listener events should be
10046 // identical to what the hardware sends (accounting for any
10047 // calibration).
10048 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010049 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010050 processId(mapper, 1);
10051 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10052 processKey(mapper, BTN_TOUCH, 1);
10053 processSync(mapper);
10054
10055 // expect coord[0] to contain initial location of touch 0
10056 NotifyMotionArgs args;
10057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10058 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10059 ASSERT_EQ(1U, args.pointerCount);
10060 ASSERT_EQ(0, args.pointerProperties[0].id);
10061 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10062 ASSERT_NO_FATAL_FAILURE(
10063 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10064
10065 // FINGER 1 DOWN
10066 processSlot(mapper, 1);
10067 processId(mapper, 2);
10068 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10069 processSync(mapper);
10070
10071 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010073 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010074 ASSERT_EQ(2U, args.pointerCount);
10075 ASSERT_EQ(0, args.pointerProperties[0].id);
10076 ASSERT_EQ(1, args.pointerProperties[1].id);
10077 ASSERT_NO_FATAL_FAILURE(
10078 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10079 ASSERT_NO_FATAL_FAILURE(
10080 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10081
10082 // FINGER 1 MOVE
10083 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10084 processSync(mapper);
10085
10086 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10087 // from move
10088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10090 ASSERT_NO_FATAL_FAILURE(
10091 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10092 ASSERT_NO_FATAL_FAILURE(
10093 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10094
10095 // FINGER 0 MOVE
10096 processSlot(mapper, 0);
10097 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10098 processSync(mapper);
10099
10100 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10102 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10103 ASSERT_NO_FATAL_FAILURE(
10104 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10105 ASSERT_NO_FATAL_FAILURE(
10106 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10107
10108 // BUTTON DOWN
10109 processKey(mapper, BTN_LEFT, 1);
10110 processSync(mapper);
10111
10112 // touchinputmapper design sends a move before button press
10113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10117
10118 // BUTTON UP
10119 processKey(mapper, BTN_LEFT, 0);
10120 processSync(mapper);
10121
10122 // touchinputmapper design sends a move after button release
10123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10127
10128 // FINGER 0 UP
10129 processId(mapper, -1);
10130 processSync(mapper);
10131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10132 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10133
10134 // FINGER 1 MOVE
10135 processSlot(mapper, 1);
10136 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10137 processSync(mapper);
10138
10139 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10142 ASSERT_EQ(1U, args.pointerCount);
10143 ASSERT_EQ(1, args.pointerProperties[0].id);
10144 ASSERT_NO_FATAL_FAILURE(
10145 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10146
10147 // FINGER 1 UP
10148 processId(mapper, -1);
10149 processKey(mapper, BTN_TOUCH, 0);
10150 processSync(mapper);
10151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10152 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10153
10154 // non captured touchpad should be a mouse source
10155 mFakePolicy->setPointerCapture(false);
10156 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10158 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
10159}
10160
10161TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10162 std::shared_ptr<FakePointerController> fakePointerController =
10163 std::make_shared<FakePointerController>();
10164 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10165 fakePointerController->setPosition(0, 0);
10166 fakePointerController->setButtonState(0);
10167
10168 // prepare device and capture
10169 prepareDisplay(DISPLAY_ORIENTATION_0);
10170 prepareAxes(POSITION | ID | SLOT);
10171 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10172 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010173 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010174 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10175 // run uncaptured pointer tests - pushes out generic events
10176 // FINGER 0 DOWN
10177 processId(mapper, 3);
10178 processPosition(mapper, 100, 100);
10179 processKey(mapper, BTN_TOUCH, 1);
10180 processSync(mapper);
10181
10182 // start at (100,100), cursor should be at (0,0) * scale
10183 NotifyMotionArgs args;
10184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10185 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10186 ASSERT_NO_FATAL_FAILURE(
10187 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10188
10189 // FINGER 0 MOVE
10190 processPosition(mapper, 200, 200);
10191 processSync(mapper);
10192
10193 // compute scaling to help with touch position checking
10194 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10195 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10196 float scale =
10197 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10198
10199 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10201 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10203 0, 0, 0, 0, 0, 0, 0));
10204}
10205
10206TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10207 std::shared_ptr<FakePointerController> fakePointerController =
10208 std::make_shared<FakePointerController>();
10209
10210 prepareDisplay(DISPLAY_ORIENTATION_0);
10211 prepareAxes(POSITION | ID | SLOT);
10212 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010213 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010214 mFakePolicy->setPointerCapture(false);
10215 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10216
10217 // uncaptured touchpad should be a pointer device
10218 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
10219
10220 // captured touchpad should be a touchpad device
10221 mFakePolicy->setPointerCapture(true);
10222 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10223 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10224}
10225
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010226// --- BluetoothMultiTouchInputMapperTest ---
10227
10228class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10229protected:
10230 void SetUp() override {
10231 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10232 }
10233};
10234
10235TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10236 addConfigurationProperty("touch.deviceType", "touchScreen");
10237 prepareDisplay(DISPLAY_ORIENTATION_0);
10238 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10239 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10240
10241 nsecs_t kernelEventTime = ARBITRARY_TIME;
10242 nsecs_t expectedEventTime = ARBITRARY_TIME;
10243 // Touch down.
10244 processId(mapper, FIRST_TRACKING_ID);
10245 processPosition(mapper, 100, 200);
10246 processPressure(mapper, RAW_PRESSURE_MAX);
10247 processSync(mapper, ARBITRARY_TIME);
10248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10249 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10250
10251 // Process several events that come in quick succession, according to their timestamps.
10252 for (int i = 0; i < 3; i++) {
10253 constexpr static nsecs_t delta = ms2ns(1);
10254 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10255 kernelEventTime += delta;
10256 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10257
10258 processPosition(mapper, 101 + i, 201 + i);
10259 processSync(mapper, kernelEventTime);
10260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10261 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10262 WithEventTime(expectedEventTime))));
10263 }
10264
10265 // Release the touch.
10266 processId(mapper, INVALID_TRACKING_ID);
10267 processPressure(mapper, RAW_PRESSURE_MIN);
10268 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10270 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10271 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10272}
10273
10274// --- MultiTouchPointerModeTest ---
10275
HQ Liue6983c72022-04-19 22:14:56 +000010276class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10277protected:
10278 float mPointerMovementScale;
10279 float mPointerXZoomScale;
10280 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10281 addConfigurationProperty("touch.deviceType", "pointer");
10282 std::shared_ptr<FakePointerController> fakePointerController =
10283 std::make_shared<FakePointerController>();
10284 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10285 fakePointerController->setPosition(0, 0);
10286 fakePointerController->setButtonState(0);
10287 prepareDisplay(DISPLAY_ORIENTATION_0);
10288
10289 prepareAxes(POSITION);
10290 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10291 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10292 // needs to be disabled, and the pointer gesture needs to be enabled.
10293 mFakePolicy->setPointerCapture(false);
10294 mFakePolicy->setPointerGestureEnabled(true);
10295 mFakePolicy->setPointerController(fakePointerController);
10296
10297 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10298 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10299 mPointerMovementScale =
10300 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10301 mPointerXZoomScale =
10302 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10303 }
10304
10305 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10306 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10307 /*flat*/ 0,
10308 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10309 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10310 /*flat*/ 0,
10311 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10312 }
10313};
10314
10315/**
10316 * Two fingers down on a pointer mode touch pad. The width
10317 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10318 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10319 * be greater than the both value to be freeform gesture, so that after two
10320 * fingers start to move downwards, the gesture should be swipe.
10321 */
10322TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10323 // The min freeform gesture width is 25units/mm x 30mm = 750
10324 // which is greater than fraction of the diagnal length of the touchpad (349).
10325 // Thus, MaxSwipWidth is 750.
10326 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10327 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10328 NotifyMotionArgs motionArgs;
10329
10330 // Two fingers down at once.
10331 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10332 // Pointer's initial position is used the [0,0] coordinate.
10333 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10334
10335 processId(mapper, FIRST_TRACKING_ID);
10336 processPosition(mapper, x1, y1);
10337 processMTSync(mapper);
10338 processId(mapper, SECOND_TRACKING_ID);
10339 processPosition(mapper, x2, y2);
10340 processMTSync(mapper);
10341 processSync(mapper);
10342
10343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10344 ASSERT_EQ(1U, motionArgs.pointerCount);
10345 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10346 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010347 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010348 ASSERT_NO_FATAL_FAILURE(
10349 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10350
10351 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10352 // that there should be 1 pointer.
10353 int32_t movingDistance = 200;
10354 y1 += movingDistance;
10355 y2 += movingDistance;
10356
10357 processId(mapper, FIRST_TRACKING_ID);
10358 processPosition(mapper, x1, y1);
10359 processMTSync(mapper);
10360 processId(mapper, SECOND_TRACKING_ID);
10361 processPosition(mapper, x2, y2);
10362 processMTSync(mapper);
10363 processSync(mapper);
10364
10365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10366 ASSERT_EQ(1U, motionArgs.pointerCount);
10367 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10368 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010369 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10371 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10372 0, 0, 0, 0));
10373}
10374
10375/**
10376 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10377 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10378 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10379 * value to be freeform gesture, so that after two fingers start to move downwards,
10380 * the gesture should be swipe.
10381 */
10382TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10383 // The min freeform gesture width is 5units/mm x 30mm = 150
10384 // which is greater than fraction of the diagnal length of the touchpad (349).
10385 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10386 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10387 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10388 NotifyMotionArgs motionArgs;
10389
10390 // Two fingers down at once.
10391 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10392 // Pointer's initial position is used the [0,0] coordinate.
10393 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10394
10395 processId(mapper, FIRST_TRACKING_ID);
10396 processPosition(mapper, x1, y1);
10397 processMTSync(mapper);
10398 processId(mapper, SECOND_TRACKING_ID);
10399 processPosition(mapper, x2, y2);
10400 processMTSync(mapper);
10401 processSync(mapper);
10402
10403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10404 ASSERT_EQ(1U, motionArgs.pointerCount);
10405 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010407 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010408 ASSERT_NO_FATAL_FAILURE(
10409 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10410
10411 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10412 // and there should be 1 pointer.
10413 int32_t movingDistance = 200;
10414 y1 += movingDistance;
10415 y2 += movingDistance;
10416
10417 processId(mapper, FIRST_TRACKING_ID);
10418 processPosition(mapper, x1, y1);
10419 processMTSync(mapper);
10420 processId(mapper, SECOND_TRACKING_ID);
10421 processPosition(mapper, x2, y2);
10422 processMTSync(mapper);
10423 processSync(mapper);
10424
10425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10426 ASSERT_EQ(1U, motionArgs.pointerCount);
10427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010429 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010430 // New coordinate is the scaled relative coordinate from the initial coordinate.
10431 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10432 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10433 0, 0, 0, 0));
10434}
10435
10436/**
10437 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10438 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10439 * freeform gestures after two fingers start to move downwards.
10440 */
10441TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10442 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10443 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10444
10445 NotifyMotionArgs motionArgs;
10446
10447 // Two fingers down at once. Wider than the max swipe width.
10448 // The gesture is expected to be PRESS, then transformed to FREEFORM
10449 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10450
10451 processId(mapper, FIRST_TRACKING_ID);
10452 processPosition(mapper, x1, y1);
10453 processMTSync(mapper);
10454 processId(mapper, SECOND_TRACKING_ID);
10455 processPosition(mapper, x2, y2);
10456 processMTSync(mapper);
10457 processSync(mapper);
10458
10459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10460 ASSERT_EQ(1U, motionArgs.pointerCount);
10461 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010463 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010464 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10465 ASSERT_NO_FATAL_FAILURE(
10466 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10467
10468 int32_t movingDistance = 200;
10469
10470 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10471 // then two down events for two pointers.
10472 y1 += movingDistance;
10473 y2 += movingDistance;
10474
10475 processId(mapper, FIRST_TRACKING_ID);
10476 processPosition(mapper, x1, y1);
10477 processMTSync(mapper);
10478 processId(mapper, SECOND_TRACKING_ID);
10479 processPosition(mapper, x2, y2);
10480 processMTSync(mapper);
10481 processSync(mapper);
10482
10483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10484 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10485 ASSERT_EQ(1U, motionArgs.pointerCount);
10486 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10489 ASSERT_EQ(1U, motionArgs.pointerCount);
10490 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010493 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010494 ASSERT_EQ(2U, motionArgs.pointerCount);
10495 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10496 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010497 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010498 // Two pointers' scaled relative coordinates from their initial centroid.
10499 // Initial y coordinates are 0 as y1 and y2 have the same value.
10500 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10501 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10502 // When pointers move, the new coordinates equal to the initial coordinates plus
10503 // scaled moving distance.
10504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10505 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10506 0, 0, 0, 0));
10507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10508 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10509 0, 0, 0, 0));
10510
10511 // Move two fingers down again, expect one MOVE motion event.
10512 y1 += movingDistance;
10513 y2 += movingDistance;
10514
10515 processId(mapper, FIRST_TRACKING_ID);
10516 processPosition(mapper, x1, y1);
10517 processMTSync(mapper);
10518 processId(mapper, SECOND_TRACKING_ID);
10519 processPosition(mapper, x2, y2);
10520 processMTSync(mapper);
10521 processSync(mapper);
10522
10523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10524 ASSERT_EQ(2U, motionArgs.pointerCount);
10525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10526 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010527 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10529 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10530 0, 0, 0, 0, 0));
10531 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10532 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10533 0, 0, 0, 0, 0));
10534}
10535
Harry Cutts39b7ca22022-10-05 15:55:48 +000010536TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10537 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10538 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10539 NotifyMotionArgs motionArgs;
10540
10541 // Place two fingers down.
10542 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10543
10544 processId(mapper, FIRST_TRACKING_ID);
10545 processPosition(mapper, x1, y1);
10546 processMTSync(mapper);
10547 processId(mapper, SECOND_TRACKING_ID);
10548 processPosition(mapper, x2, y2);
10549 processMTSync(mapper);
10550 processSync(mapper);
10551
10552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10553 ASSERT_EQ(1U, motionArgs.pointerCount);
10554 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10555 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10556 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10557 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10558
10559 // Move the two fingers down and to the left.
10560 int32_t movingDistance = 200;
10561 x1 -= movingDistance;
10562 y1 += movingDistance;
10563 x2 -= movingDistance;
10564 y2 += movingDistance;
10565
10566 processId(mapper, FIRST_TRACKING_ID);
10567 processPosition(mapper, x1, y1);
10568 processMTSync(mapper);
10569 processId(mapper, SECOND_TRACKING_ID);
10570 processPosition(mapper, x2, y2);
10571 processMTSync(mapper);
10572 processSync(mapper);
10573
10574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10575 ASSERT_EQ(1U, motionArgs.pointerCount);
10576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10577 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10578 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10579 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10580}
10581
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010582// --- JoystickInputMapperTest ---
10583
10584class JoystickInputMapperTest : public InputMapperTest {
10585protected:
10586 static const int32_t RAW_X_MIN;
10587 static const int32_t RAW_X_MAX;
10588 static const int32_t RAW_Y_MIN;
10589 static const int32_t RAW_Y_MAX;
10590
10591 void SetUp() override {
10592 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10593 }
10594 void prepareAxes() {
10595 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10596 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10597 }
10598
10599 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10600 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10601 }
10602
10603 void processSync(JoystickInputMapper& mapper) {
10604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10605 }
10606
10607 void prepareVirtualDisplay(int32_t orientation) {
10608 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10609 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10610 NO_PORT, ViewportType::VIRTUAL);
10611 }
10612};
10613
10614const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10615const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10616const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10617const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10618
10619TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10620 prepareAxes();
10621 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10622
10623 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10624
10625 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10626
10627 // Send an axis event
10628 processAxis(mapper, ABS_X, 100);
10629 processSync(mapper);
10630
10631 NotifyMotionArgs args;
10632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10633 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10634
10635 // Send another axis event
10636 processAxis(mapper, ABS_Y, 100);
10637 processSync(mapper);
10638
10639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10640 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10641}
10642
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010643// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010644
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010645class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010646protected:
10647 static const char* DEVICE_NAME;
10648 static const char* DEVICE_LOCATION;
10649 static const int32_t DEVICE_ID;
10650 static const int32_t DEVICE_GENERATION;
10651 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010652 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010653 static const int32_t EVENTHUB_ID;
10654
10655 std::shared_ptr<FakeEventHub> mFakeEventHub;
10656 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010657 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010658 std::unique_ptr<InstrumentedInputReader> mReader;
10659 std::shared_ptr<InputDevice> mDevice;
10660
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010661 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010662 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010663 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010664 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010665 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010666 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010667 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10668 }
10669
10670 void SetUp() override { SetUp(DEVICE_CLASSES); }
10671
10672 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010673 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010674 mFakePolicy.clear();
10675 }
10676
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010677 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010678 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10679 mReader->requestRefreshConfiguration(changes);
10680 mReader->loopOnce();
10681 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010682 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010683 }
10684
10685 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10686 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010687 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010688 InputDeviceIdentifier identifier;
10689 identifier.name = name;
10690 identifier.location = location;
10691 std::shared_ptr<InputDevice> device =
10692 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10693 identifier);
10694 mReader->pushNextDevice(device);
10695 mFakeEventHub->addDevice(eventHubId, name, classes);
10696 mReader->loopOnce();
10697 return device;
10698 }
10699
10700 template <class T, typename... Args>
10701 T& addControllerAndConfigure(Args... args) {
10702 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10703
10704 return controller;
10705 }
10706};
10707
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010708const char* PeripheralControllerTest::DEVICE_NAME = "device";
10709const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10710const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10711const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10712const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010713const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10714 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010715const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010716
10717// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010718class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010719protected:
10720 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010721 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010722 }
10723};
10724
10725TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010726 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010727
10728 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
10729 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
10730}
10731
10732TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010733 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010734
10735 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
10736 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
10737}
10738
10739// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010740class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010741protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010742 void SetUp() override {
10743 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10744 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010745};
10746
Chris Ye85758332021-05-16 23:05:17 -070010747TEST_F(LightControllerTest, MonoLight) {
10748 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010749 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010750 .maxBrightness = 255,
10751 .flags = InputLightClass::BRIGHTNESS,
10752 .path = ""};
10753 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010754
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010755 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010756 InputDeviceInfo info;
10757 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010758 std::vector<InputDeviceLightInfo> lights = info.getLights();
10759 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010760 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10761 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10762
10763 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10764 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10765}
10766
10767TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10768 RawLightInfo infoMono = {.id = 1,
10769 .name = "mono_keyboard_backlight",
10770 .maxBrightness = 255,
10771 .flags = InputLightClass::BRIGHTNESS |
10772 InputLightClass::KEYBOARD_BACKLIGHT,
10773 .path = ""};
10774 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10775
10776 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10777 InputDeviceInfo info;
10778 controller.populateDeviceInfo(&info);
10779 std::vector<InputDeviceLightInfo> lights = info.getLights();
10780 ASSERT_EQ(1U, lights.size());
10781 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10782 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010783
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010784 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10785 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010786}
10787
10788TEST_F(LightControllerTest, RGBLight) {
10789 RawLightInfo infoRed = {.id = 1,
10790 .name = "red",
10791 .maxBrightness = 255,
10792 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10793 .path = ""};
10794 RawLightInfo infoGreen = {.id = 2,
10795 .name = "green",
10796 .maxBrightness = 255,
10797 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10798 .path = ""};
10799 RawLightInfo infoBlue = {.id = 3,
10800 .name = "blue",
10801 .maxBrightness = 255,
10802 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10803 .path = ""};
10804 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10805 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10806 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10807
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010808 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010809 InputDeviceInfo info;
10810 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010811 std::vector<InputDeviceLightInfo> lights = info.getLights();
10812 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010813 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10814 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10815 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10816
10817 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10818 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10819}
10820
10821TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10822 RawLightInfo infoRed = {.id = 1,
10823 .name = "red_keyboard_backlight",
10824 .maxBrightness = 255,
10825 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10826 InputLightClass::KEYBOARD_BACKLIGHT,
10827 .path = ""};
10828 RawLightInfo infoGreen = {.id = 2,
10829 .name = "green_keyboard_backlight",
10830 .maxBrightness = 255,
10831 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10832 InputLightClass::KEYBOARD_BACKLIGHT,
10833 .path = ""};
10834 RawLightInfo infoBlue = {.id = 3,
10835 .name = "blue_keyboard_backlight",
10836 .maxBrightness = 255,
10837 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10838 InputLightClass::KEYBOARD_BACKLIGHT,
10839 .path = ""};
10840 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10841 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10842 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10843
10844 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10845 InputDeviceInfo info;
10846 controller.populateDeviceInfo(&info);
10847 std::vector<InputDeviceLightInfo> lights = info.getLights();
10848 ASSERT_EQ(1U, lights.size());
10849 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10850 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10851 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10852
10853 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10854 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10855}
10856
10857TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10858 RawLightInfo infoRed = {.id = 1,
10859 .name = "red",
10860 .maxBrightness = 255,
10861 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10862 .path = ""};
10863 RawLightInfo infoGreen = {.id = 2,
10864 .name = "green",
10865 .maxBrightness = 255,
10866 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10867 .path = ""};
10868 RawLightInfo infoBlue = {.id = 3,
10869 .name = "blue",
10870 .maxBrightness = 255,
10871 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10872 .path = ""};
10873 RawLightInfo infoGlobal = {.id = 3,
10874 .name = "global_keyboard_backlight",
10875 .maxBrightness = 255,
10876 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10877 InputLightClass::KEYBOARD_BACKLIGHT,
10878 .path = ""};
10879 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10880 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10881 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10882 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10883
10884 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10885 InputDeviceInfo info;
10886 controller.populateDeviceInfo(&info);
10887 std::vector<InputDeviceLightInfo> lights = info.getLights();
10888 ASSERT_EQ(1U, lights.size());
10889 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10890 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10891 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010892
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010893 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10894 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010895}
10896
10897TEST_F(LightControllerTest, MultiColorRGBLight) {
10898 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010899 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010900 .maxBrightness = 255,
10901 .flags = InputLightClass::BRIGHTNESS |
10902 InputLightClass::MULTI_INTENSITY |
10903 InputLightClass::MULTI_INDEX,
10904 .path = ""};
10905
10906 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10907
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010908 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010909 InputDeviceInfo info;
10910 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010911 std::vector<InputDeviceLightInfo> lights = info.getLights();
10912 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010913 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10914 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10915 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10916
10917 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10918 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10919}
10920
10921TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10922 RawLightInfo infoColor = {.id = 1,
10923 .name = "multi_color_keyboard_backlight",
10924 .maxBrightness = 255,
10925 .flags = InputLightClass::BRIGHTNESS |
10926 InputLightClass::MULTI_INTENSITY |
10927 InputLightClass::MULTI_INDEX |
10928 InputLightClass::KEYBOARD_BACKLIGHT,
10929 .path = ""};
10930
10931 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10932
10933 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10934 InputDeviceInfo info;
10935 controller.populateDeviceInfo(&info);
10936 std::vector<InputDeviceLightInfo> lights = info.getLights();
10937 ASSERT_EQ(1U, lights.size());
10938 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10939 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10940 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010941
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010942 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10943 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010944}
10945
10946TEST_F(LightControllerTest, PlayerIdLight) {
10947 RawLightInfo info1 = {.id = 1,
10948 .name = "player1",
10949 .maxBrightness = 255,
10950 .flags = InputLightClass::BRIGHTNESS,
10951 .path = ""};
10952 RawLightInfo info2 = {.id = 2,
10953 .name = "player2",
10954 .maxBrightness = 255,
10955 .flags = InputLightClass::BRIGHTNESS,
10956 .path = ""};
10957 RawLightInfo info3 = {.id = 3,
10958 .name = "player3",
10959 .maxBrightness = 255,
10960 .flags = InputLightClass::BRIGHTNESS,
10961 .path = ""};
10962 RawLightInfo info4 = {.id = 4,
10963 .name = "player4",
10964 .maxBrightness = 255,
10965 .flags = InputLightClass::BRIGHTNESS,
10966 .path = ""};
10967 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10968 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10969 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10970 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10971
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010972 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010973 InputDeviceInfo info;
10974 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010975 std::vector<InputDeviceLightInfo> lights = info.getLights();
10976 ASSERT_EQ(1U, lights.size());
10977 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010978 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10979 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010980
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010981 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10982 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10983 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010984}
10985
Michael Wrightd02c5b62014-02-10 15:10:22 -080010986} // namespace android