blob: 6b6d0bbfaa5602cbd80167f91e67364e6b91d8ae [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;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001352 std::optional<nsecs_t> mRequestedTimeout;
1353 std::vector<InputDeviceInfo> mExternalStylusDevices;
arthurhungdcef2dc2020-08-11 14:47:50 +08001354
1355 public:
1356 FakeInputReaderContext(InputReader* reader)
1357 : ContextImpl(reader),
1358 mGlobalMetaState(0),
1359 mUpdateGlobalMetaStateWasCalled(false),
1360 mGeneration(1) {}
1361
1362 virtual ~FakeInputReaderContext() {}
1363
1364 void assertUpdateGlobalMetaStateWasCalled() {
1365 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
1366 << "Expected updateGlobalMetaState() to have been called.";
1367 mUpdateGlobalMetaStateWasCalled = false;
1368 }
1369
1370 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
1371
1372 uint32_t getGeneration() { return mGeneration; }
1373
1374 void updateGlobalMetaState() override {
1375 mUpdateGlobalMetaStateWasCalled = true;
1376 ContextImpl::updateGlobalMetaState();
1377 }
1378
1379 int32_t getGlobalMetaState() override {
1380 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
1381 }
1382
1383 int32_t bumpGeneration() override {
1384 mGeneration = ContextImpl::bumpGeneration();
1385 return mGeneration;
1386 }
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00001387
1388 void requestTimeoutAtTime(nsecs_t when) override { mRequestedTimeout = when; }
1389
1390 void assertTimeoutWasRequested(nsecs_t when) {
1391 ASSERT_TRUE(mRequestedTimeout) << "Expected timeout at time " << when
1392 << " but there was no timeout requested.";
1393 ASSERT_EQ(when, *mRequestedTimeout);
1394 mRequestedTimeout.reset();
1395 }
1396
1397 void assertTimeoutWasNotRequested() {
1398 ASSERT_FALSE(mRequestedTimeout) << "Expected no timeout to have been requested,"
1399 " but one was requested at time "
1400 << *mRequestedTimeout;
1401 }
1402
1403 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {
1404 outDevices = mExternalStylusDevices;
1405 }
1406
1407 void setExternalStylusDevices(std::vector<InputDeviceInfo>&& devices) {
1408 mExternalStylusDevices = devices;
1409 }
arthurhungdcef2dc2020-08-11 14:47:50 +08001410 } mFakeContext;
1411
Michael Wrightd02c5b62014-02-10 15:10:22 -08001412 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +08001413
1414public:
1415 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -08001416};
1417
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001418// --- InputReaderPolicyTest ---
1419class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001420protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001421 sp<FakeInputReaderPolicy> mFakePolicy;
1422
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001423 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -07001424 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001425};
1426
1427/**
1428 * Check that empty set of viewports is an acceptable configuration.
1429 * Also try to get internal viewport two different ways - by type and by uniqueId.
1430 *
1431 * There will be confusion if two viewports with empty uniqueId and identical type are present.
1432 * Such configuration is not currently allowed.
1433 */
1434TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -07001435 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001436
1437 // We didn't add any viewports yet, so there shouldn't be any.
1438 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001439 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001440 ASSERT_FALSE(internalViewport);
1441
1442 // Add an internal viewport, then clear it
1443 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001444 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001445 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001446
1447 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001448 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001449 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001450 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001451
1452 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001453 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001454 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001455 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001456
1457 mFakePolicy->clearViewports();
1458 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001459 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001460 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001461 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001462 ASSERT_FALSE(internalViewport);
1463}
1464
1465TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
1466 const std::string internalUniqueId = "local:0";
1467 const std::string externalUniqueId = "local:1";
1468 const std::string virtualUniqueId1 = "virtual:2";
1469 const std::string virtualUniqueId2 = "virtual:3";
1470 constexpr int32_t virtualDisplayId1 = 2;
1471 constexpr int32_t virtualDisplayId2 = 3;
1472
1473 // Add an internal viewport
1474 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001475 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
1476 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001477 // Add an external viewport
1478 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001479 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
1480 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001481 // Add an virtual viewport
1482 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001483 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
1484 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001485 // Add another virtual viewport
1486 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001487 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
1488 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001489
1490 // Check matching by type for internal
1491 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001492 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001493 ASSERT_TRUE(internalViewport);
1494 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
1495
1496 // Check matching by type for external
1497 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001498 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001499 ASSERT_TRUE(externalViewport);
1500 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
1501
1502 // Check matching by uniqueId for virtual viewport #1
1503 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001504 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001505 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001506 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001507 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
1508 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
1509
1510 // Check matching by uniqueId for virtual viewport #2
1511 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001512 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001513 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001514 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001515 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
1516 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
1517}
1518
1519
1520/**
1521 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
1522 * that lookup works by checking display id.
1523 * Check that 2 viewports of each kind is possible, for all existing viewport types.
1524 */
1525TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
1526 const std::string uniqueId1 = "uniqueId1";
1527 const std::string uniqueId2 = "uniqueId2";
1528 constexpr int32_t displayId1 = 2;
1529 constexpr int32_t displayId2 = 3;
1530
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001531 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
1532 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001533 for (const ViewportType& type : types) {
1534 mFakePolicy->clearViewports();
1535 // Add a viewport
1536 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001537 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
1538 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001539 // Add another viewport
1540 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001541 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
1542 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001543
1544 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001545 std::optional<DisplayViewport> viewport1 =
1546 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001547 ASSERT_TRUE(viewport1);
1548 ASSERT_EQ(displayId1, viewport1->displayId);
1549 ASSERT_EQ(type, viewport1->type);
1550
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001551 std::optional<DisplayViewport> viewport2 =
1552 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001553 ASSERT_TRUE(viewport2);
1554 ASSERT_EQ(displayId2, viewport2->displayId);
1555 ASSERT_EQ(type, viewport2->type);
1556
1557 // When there are multiple viewports of the same kind, and uniqueId is not specified
1558 // in the call to getDisplayViewport, then that situation is not supported.
1559 // The viewports can be stored in any order, so we cannot rely on the order, since that
1560 // is just implementation detail.
1561 // However, we can check that it still returns *a* viewport, we just cannot assert
1562 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001563 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07001564 ASSERT_TRUE(someViewport);
1565 }
1566}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001567
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001568/**
Michael Wrightdde67b82020-10-27 16:09:22 +00001569 * When we have multiple internal displays make sure we always return the default display when
1570 * querying by type.
1571 */
1572TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
1573 const std::string uniqueId1 = "uniqueId1";
1574 const std::string uniqueId2 = "uniqueId2";
1575 constexpr int32_t nonDefaultDisplayId = 2;
1576 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
1577 "Test display ID should not be ADISPLAY_ID_DEFAULT");
1578
1579 // Add the default display first and ensure it gets returned.
1580 mFakePolicy->clearViewports();
1581 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001582 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001583 ViewportType::INTERNAL);
1584 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001585 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001586 ViewportType::INTERNAL);
1587
1588 std::optional<DisplayViewport> viewport =
1589 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1590 ASSERT_TRUE(viewport);
1591 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1592 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1593
1594 // Add the default display second to make sure order doesn't matter.
1595 mFakePolicy->clearViewports();
1596 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001597 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001598 ViewportType::INTERNAL);
1599 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001600 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +00001601 ViewportType::INTERNAL);
1602
1603 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
1604 ASSERT_TRUE(viewport);
1605 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
1606 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
1607}
1608
1609/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001610 * Check getDisplayViewportByPort
1611 */
1612TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001613 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001614 const std::string uniqueId1 = "uniqueId1";
1615 const std::string uniqueId2 = "uniqueId2";
1616 constexpr int32_t displayId1 = 1;
1617 constexpr int32_t displayId2 = 2;
1618 const uint8_t hdmi1 = 0;
1619 const uint8_t hdmi2 = 1;
1620 const uint8_t hdmi3 = 2;
1621
1622 mFakePolicy->clearViewports();
1623 // Add a viewport that's associated with some display port that's not of interest.
1624 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001625 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
1626 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001627 // Add another viewport, connected to HDMI1 port
1628 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001629 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
1630 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07001631
1632 // Check that correct display viewport was returned by comparing the display ports.
1633 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
1634 ASSERT_TRUE(hdmi1Viewport);
1635 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1636 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1637
1638 // Check that we can still get the same viewport using the uniqueId
1639 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
1640 ASSERT_TRUE(hdmi1Viewport);
1641 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
1642 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
1643 ASSERT_EQ(type, hdmi1Viewport->type);
1644
1645 // Check that we cannot find a port with "HDMI2", because we never added one
1646 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
1647 ASSERT_FALSE(hdmi2Viewport);
1648}
1649
Michael Wrightd02c5b62014-02-10 15:10:22 -08001650// --- InputReaderTest ---
1651
1652class InputReaderTest : public testing::Test {
1653protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001654 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001655 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001656 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +00001657 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -08001658
Chris Yea52ade12020-08-27 16:49:20 -07001659 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07001660 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001661 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001662 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001663
Prabir Pradhan28efc192019-11-05 01:10:04 +00001664 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001665 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001666 }
1667
Chris Yea52ade12020-08-27 16:49:20 -07001668 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001669 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001670 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001671 }
1672
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001673 void addDevice(int32_t eventHubId, const std::string& name,
1674 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001675 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001676
1677 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001678 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001679 }
1680 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001681 mReader->loopOnce();
1682 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001683 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1684 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001685 }
1686
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001687 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001688 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001689 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001690 }
1691
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001692 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001693 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001694 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001695 }
1696
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001697 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -07001698 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001699 ftl::Flags<InputDeviceClass> classes,
1700 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001701 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
1703 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001704 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001705 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001706 return mapper;
1707 }
1708};
1709
Chris Ye98d3f532020-10-01 21:48:59 -07001710TEST_F(InputReaderTest, PolicyGetInputDevices) {
1711 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001712 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -07001713 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -08001714
1715 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -07001716 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001717 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001718 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001719 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001720 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
1721 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001722 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -08001723}
1724
Chris Yee7310032020-09-22 15:36:28 -07001725TEST_F(InputReaderTest, GetMergedInputDevices) {
1726 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1727 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1728 // Add two subdevices to device
1729 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1730 // Must add at least one mapper or the device will be ignored!
1731 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1732 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1733
1734 // Push same device instance for next device to be added, so they'll have same identifier.
1735 mReader->pushNextDevice(device);
1736 mReader->pushNextDevice(device);
1737 ASSERT_NO_FATAL_FAILURE(
1738 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
1739 ASSERT_NO_FATAL_FAILURE(
1740 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1741
1742 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00001743 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -07001744}
1745
Chris Yee14523a2020-12-19 13:46:00 -08001746TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
1747 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1748 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1749 // Add two subdevices to device
1750 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1751 // Must add at least one mapper or the device will be ignored!
1752 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1753 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1754
1755 // Push same device instance for next device to be added, so they'll have same identifier.
1756 mReader->pushNextDevice(device);
1757 mReader->pushNextDevice(device);
1758 // Sensor device is initially disabled
1759 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
1760 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
1761 nullptr));
1762 // Device is disabled because the only sub device is a sensor device and disabled initially.
1763 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1764 ASSERT_FALSE(device->isEnabled());
1765 ASSERT_NO_FATAL_FAILURE(
1766 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
1767 // The merged device is enabled if any sub device is enabled
1768 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1769 ASSERT_TRUE(device->isEnabled());
1770}
1771
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001772TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001773 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001774 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001775 constexpr int32_t eventHubId = 1;
1776 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001777 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001778 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001779 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001780 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001781
Yi Kong9b14ac62018-07-17 13:48:38 -07001782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001783
1784 NotifyDeviceResetArgs resetArgs;
1785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001786 ASSERT_EQ(deviceId, resetArgs.deviceId);
1787
1788 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001789 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001790 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001791
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001793 ASSERT_EQ(deviceId, resetArgs.deviceId);
1794 ASSERT_EQ(device->isEnabled(), false);
1795
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001796 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001797 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001800 ASSERT_EQ(device->isEnabled(), false);
1801
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001802 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001803 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001805 ASSERT_EQ(deviceId, resetArgs.deviceId);
1806 ASSERT_EQ(device->isEnabled(), true);
1807}
1808
Michael Wrightd02c5b62014-02-10 15:10:22 -08001809TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001810 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001811 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001812 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001813 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001814 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001815 AINPUT_SOURCE_KEYBOARD, nullptr);
1816 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001817
1818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1819 AINPUT_SOURCE_ANY, AKEYCODE_A))
1820 << "Should return unknown when the device id is >= 0 but unknown.";
1821
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001822 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1823 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1824 << "Should return unknown when the device id is valid but the sources are not "
1825 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001827 ASSERT_EQ(AKEY_STATE_DOWN,
1828 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1829 AKEYCODE_A))
1830 << "Should return value provided by mapper when device id is valid and the device "
1831 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001832
1833 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1834 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1835 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1836
1837 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1838 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1839 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1840}
1841
Philip Junker4af3b3d2021-12-14 10:36:55 +01001842TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1843 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1844 constexpr int32_t eventHubId = 1;
1845 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1846 InputDeviceClass::KEYBOARD,
1847 AINPUT_SOURCE_KEYBOARD, nullptr);
1848 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1849
1850 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1851 << "Should return unknown when the device with the specified id is not found.";
1852
1853 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1854 << "Should return correct mapping when device id is valid and mapping exists.";
1855
1856 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1857 << "Should return the location key code when device id is valid and there's no "
1858 "mapping.";
1859}
1860
1861TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1862 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1863 constexpr int32_t eventHubId = 1;
1864 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1865 InputDeviceClass::JOYSTICK,
1866 AINPUT_SOURCE_GAMEPAD, nullptr);
1867 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1868
1869 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1870 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1871}
1872
Michael Wrightd02c5b62014-02-10 15:10:22 -08001873TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001874 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001875 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001876 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001877 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001878 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001879 AINPUT_SOURCE_KEYBOARD, nullptr);
1880 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001881
1882 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1883 AINPUT_SOURCE_ANY, KEY_A))
1884 << "Should return unknown when the device id is >= 0 but unknown.";
1885
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001886 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1887 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1888 << "Should return unknown when the device id is valid but the sources are not "
1889 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001890
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001891 ASSERT_EQ(AKEY_STATE_DOWN,
1892 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1893 KEY_A))
1894 << "Should return value provided by mapper when device id is valid and the device "
1895 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001896
1897 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1898 AINPUT_SOURCE_TRACKBALL, KEY_A))
1899 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1900
1901 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1902 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1903 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1904}
1905
1906TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001907 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001908 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001909 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001910 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001911 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001912 AINPUT_SOURCE_KEYBOARD, nullptr);
1913 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001914
1915 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1916 AINPUT_SOURCE_ANY, SW_LID))
1917 << "Should return unknown when the device id is >= 0 but unknown.";
1918
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001919 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1920 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1921 << "Should return unknown when the device id is valid but the sources are not "
1922 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001923
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001924 ASSERT_EQ(AKEY_STATE_DOWN,
1925 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1926 SW_LID))
1927 << "Should return value provided by mapper when device id is valid and the device "
1928 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001929
1930 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1931 AINPUT_SOURCE_TRACKBALL, SW_LID))
1932 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1933
1934 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1935 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1936 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1937}
1938
1939TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001940 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001941 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001942 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001943 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001944 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001945 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001947 mapper.addSupportedKeyCode(AKEYCODE_A);
1948 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001949
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001950 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001951 uint8_t flags[4] = { 0, 0, 0, 1 };
1952
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001953 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001954 << "Should return false when device id is >= 0 but unknown.";
1955 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1956
1957 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001958 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001959 << "Should return false when device id is valid but the sources are not supported by "
1960 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001961 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1962
1963 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001964 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001965 keyCodes, flags))
1966 << "Should return value provided by mapper when device id is valid and the device "
1967 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001968 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1969
1970 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001971 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1972 << "Should return false when the device id is < 0 but the sources are not supported by "
1973 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001974 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1975
1976 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001977 ASSERT_TRUE(
1978 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1979 << "Should return value provided by mapper when device id is < 0 and one of the "
1980 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001981 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1982}
1983
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001984TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001985 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001986 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001987
1988 NotifyConfigurationChangedArgs args;
1989
1990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1991 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1992}
1993
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001994TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001995 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001996 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001997 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001998 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001999 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002000 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002001 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002002 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002003
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002004 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002005 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002006 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
2007
2008 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002009 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002010 ASSERT_EQ(when, event.when);
2011 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002012 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002013 ASSERT_EQ(EV_KEY, event.type);
2014 ASSERT_EQ(KEY_A, event.code);
2015 ASSERT_EQ(1, event.value);
2016}
2017
Garfield Tan1c7bc862020-01-28 13:24:04 -08002018TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002019 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002020 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002021 constexpr int32_t eventHubId = 1;
2022 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08002023 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002024 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002025 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002026 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08002027
2028 NotifyDeviceResetArgs resetArgs;
2029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002030 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002032 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002033 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002035 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002036 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002038 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002039 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002041 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002042 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002043
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002044 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002045 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08002047 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002048 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08002049}
2050
Garfield Tan1c7bc862020-01-28 13:24:04 -08002051TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
2052 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002053 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08002054 constexpr int32_t eventHubId = 1;
2055 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2056 // Must add at least one mapper or the device will be ignored!
2057 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002058 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002059 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
2060
2061 NotifyDeviceResetArgs resetArgs;
2062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2063 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
2064}
2065
Arthur Hungc23540e2018-11-29 20:42:11 +08002066TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002067 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002068 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002069 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08002070 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002071 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2072 FakeInputMapper& mapper =
2073 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002074 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08002075
2076 const uint8_t hdmi1 = 1;
2077
2078 // Associated touch screen with second display.
2079 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
2080
2081 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00002082 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08002083 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002084 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002085 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002086 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002087 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002088 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08002089 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00002090 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00002091
2092 // Add the device, and make sure all of the callbacks are triggered.
2093 // The device is added after the input port associations are processed since
2094 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002095 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07002096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00002097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002098 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08002099
Arthur Hung2c9a3342019-07-23 14:18:59 +08002100 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08002101 ASSERT_EQ(deviceId, device->getId());
2102 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
2103 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08002104
2105 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002106 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00002107 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08002108 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08002109}
2110
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002111TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
2112 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002113 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002114 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2115 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2116 // Must add at least one mapper or the device will be ignored!
2117 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2118 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2119 mReader->pushNextDevice(device);
2120 mReader->pushNextDevice(device);
2121 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2122 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2123
2124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
2125
2126 NotifyDeviceResetArgs resetArgs;
2127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2128 ASSERT_EQ(deviceId, resetArgs.deviceId);
2129 ASSERT_TRUE(device->isEnabled());
2130 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2131 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2132
2133 disableDevice(deviceId);
2134 mReader->loopOnce();
2135
2136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2137 ASSERT_EQ(deviceId, resetArgs.deviceId);
2138 ASSERT_FALSE(device->isEnabled());
2139 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2140 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2141
2142 enableDevice(deviceId);
2143 mReader->loopOnce();
2144
2145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2146 ASSERT_EQ(deviceId, resetArgs.deviceId);
2147 ASSERT_TRUE(device->isEnabled());
2148 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
2149 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
2150}
2151
2152TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
2153 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002154 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08002155 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
2156 // Add two subdevices to device
2157 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
2158 FakeInputMapper& mapperDevice1 =
2159 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
2160 FakeInputMapper& mapperDevice2 =
2161 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
2162 mReader->pushNextDevice(device);
2163 mReader->pushNextDevice(device);
2164 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
2165 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
2166
2167 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2168 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
2169
2170 ASSERT_EQ(AKEY_STATE_DOWN,
2171 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
2172 ASSERT_EQ(AKEY_STATE_DOWN,
2173 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
2174 ASSERT_EQ(AKEY_STATE_UNKNOWN,
2175 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
2176}
2177
Prabir Pradhan7e186182020-11-10 13:56:45 -08002178TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
2179 NotifyPointerCaptureChangedArgs args;
2180
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002181 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08002182 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2183 mReader->loopOnce();
2184 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002185 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
2186 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002187
2188 mFakePolicy->setPointerCapture(false);
2189 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2190 mReader->loopOnce();
2191 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002192 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08002193
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002194 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08002195 // does not change.
2196 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
2197 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00002198 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08002199}
2200
Chris Ye87143712020-11-10 05:05:58 +00002201class FakeVibratorInputMapper : public FakeInputMapper {
2202public:
2203 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
2204 : FakeInputMapper(deviceContext, sources) {}
2205
2206 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
2207};
2208
2209TEST_F(InputReaderTest, VibratorGetVibratorIds) {
2210 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002211 ftl::Flags<InputDeviceClass> deviceClass =
2212 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00002213 constexpr int32_t eventHubId = 1;
2214 const char* DEVICE_LOCATION = "BLUETOOTH";
2215 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2216 FakeVibratorInputMapper& mapper =
2217 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
2218 mReader->pushNextDevice(device);
2219
2220 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2221 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
2222
2223 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2224 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
2225}
2226
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002227// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08002228
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002229class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08002230public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002231 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002232
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002233 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08002234
Andy Chenf9f1a022022-08-29 20:07:10 -04002235 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
2236
Chris Yee2b1e5c2021-03-10 22:45:12 -08002237 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
2238
2239 void dump(std::string& dump) override {}
2240
2241 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
2242 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002243 }
2244
Chris Yee2b1e5c2021-03-10 22:45:12 -08002245 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
2246 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08002247 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002248
2249 bool setLightColor(int32_t lightId, int32_t color) override {
2250 getDeviceContext().setLightBrightness(lightId, color >> 24);
2251 return true;
2252 }
2253
2254 std::optional<int32_t> getLightColor(int32_t lightId) override {
2255 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
2256 if (!result.has_value()) {
2257 return std::nullopt;
2258 }
2259 return result.value() << 24;
2260 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08002261
2262 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
2263
2264 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
2265
2266private:
2267 InputDeviceContext& mDeviceContext;
2268 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
2269 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04002270 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08002271};
2272
Chris Yee2b1e5c2021-03-10 22:45:12 -08002273TEST_F(InputReaderTest, BatteryGetCapacity) {
2274 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002275 ftl::Flags<InputDeviceClass> deviceClass =
2276 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002277 constexpr int32_t eventHubId = 1;
2278 const char* DEVICE_LOCATION = "BLUETOOTH";
2279 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002280 FakePeripheralController& controller =
2281 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002282 mReader->pushNextDevice(device);
2283
2284 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2285
2286 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY), BATTERY_CAPACITY);
2287 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), BATTERY_CAPACITY);
2288}
2289
2290TEST_F(InputReaderTest, BatteryGetStatus) {
2291 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002292 ftl::Flags<InputDeviceClass> deviceClass =
2293 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08002294 constexpr int32_t eventHubId = 1;
2295 const char* DEVICE_LOCATION = "BLUETOOTH";
2296 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002297 FakePeripheralController& controller =
2298 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08002299 mReader->pushNextDevice(device);
2300
2301 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2302
2303 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY), BATTERY_STATUS);
2304 ASSERT_EQ(mReader->getBatteryStatus(deviceId), BATTERY_STATUS);
2305}
2306
Prabir Pradhane287ecd2022-09-07 21:18:05 +00002307TEST_F(InputReaderTest, BatteryGetDevicePath) {
2308 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
2309 ftl::Flags<InputDeviceClass> deviceClass =
2310 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
2311 constexpr int32_t eventHubId = 1;
2312 const char* DEVICE_LOCATION = "BLUETOOTH";
2313 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
2314 device->addController<FakePeripheralController>(eventHubId);
2315 mReader->pushNextDevice(device);
2316
2317 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
2318
2319 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), BATTERY_DEVPATH);
2320}
2321
Chris Ye3fdbfef2021-01-06 18:45:18 -08002322TEST_F(InputReaderTest, LightGetColor) {
2323 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002324 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08002325 constexpr int32_t eventHubId = 1;
2326 const char* DEVICE_LOCATION = "BLUETOOTH";
2327 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07002328 FakePeripheralController& controller =
2329 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002330 mReader->pushNextDevice(device);
2331 RawLightInfo info = {.id = 1,
2332 .name = "Mono",
2333 .maxBrightness = 255,
2334 .flags = InputLightClass::BRIGHTNESS,
2335 .path = ""};
2336 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
2337 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
2338
2339 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08002340
Chris Yee2b1e5c2021-03-10 22:45:12 -08002341 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
2342 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08002343 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
2344 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
2345}
2346
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002347// --- InputReaderIntegrationTest ---
2348
2349// These tests create and interact with the InputReader only through its interface.
2350// The InputReader is started during SetUp(), which starts its processing in its own
2351// thread. The tests use linux uinput to emulate input devices.
2352// NOTE: Interacting with the physical device while these tests are running may cause
2353// the tests to fail.
2354class InputReaderIntegrationTest : public testing::Test {
2355protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002356 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002357 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002358 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002359
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002360 std::shared_ptr<FakePointerController> mFakePointerController;
2361
Chris Yea52ade12020-08-27 16:49:20 -07002362 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002363#if !defined(__ANDROID__)
2364 GTEST_SKIP();
2365#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002366 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00002367 mFakePointerController = std::make_shared<FakePointerController>();
2368 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002369 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
2370 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002371
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002372 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
2373 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002374 ASSERT_EQ(mReader->start(), OK);
2375
2376 // Since this test is run on a real device, all the input devices connected
2377 // to the test device will show up in mReader. We wait for those input devices to
2378 // show up before beginning the tests.
2379 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2381 }
2382
Chris Yea52ade12020-08-27 16:49:20 -07002383 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002384#if !defined(__ANDROID__)
2385 return;
2386#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002387 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002388 mReader.reset();
2389 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002390 mFakePolicy.clear();
2391 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00002392
2393 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
2394 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
2395 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
2396 [&name](const InputDeviceInfo& info) {
2397 return info.getIdentifier().name == name;
2398 });
2399 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
2400 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002401};
2402
2403TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
2404 // An invalid input device that is only used for this test.
2405 class InvalidUinputDevice : public UinputDevice {
2406 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00002407 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002408
2409 private:
2410 void configureDevice(int fd, uinput_user_dev* device) override {}
2411 };
2412
2413 const size_t numDevices = mFakePolicy->getInputDevices().size();
2414
2415 // UinputDevice does not set any event or key bits, so InputReader should not
2416 // consider it as a valid device.
2417 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
2418 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2420 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2421
2422 invalidDevice.reset();
2423 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
2424 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
2425 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
2426}
2427
2428TEST_F(InputReaderIntegrationTest, AddNewDevice) {
2429 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
2430
2431 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2432 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2433 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2434 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
2435
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002436 const auto device = findDeviceByName(keyboard->getName());
2437 ASSERT_TRUE(device.has_value());
2438 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2439 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
2440 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002441
2442 keyboard.reset();
2443 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2445 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
2446}
2447
2448TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
2449 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
2450 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2451
2452 NotifyConfigurationChangedArgs configChangedArgs;
2453 ASSERT_NO_FATAL_FAILURE(
2454 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002455 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002456 nsecs_t prevTimestamp = configChangedArgs.eventTime;
2457
2458 NotifyKeyArgs keyArgs;
2459 keyboard->pressAndReleaseHomeKey();
2460 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2461 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002462 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08002463 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002464 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002465 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002466 prevTimestamp = keyArgs.eventTime;
2467
2468 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
2469 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08002470 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002471 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002472 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08002473}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002474
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002475TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
2476 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2477 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2478
2479 const auto device = findDeviceByName(stylus->getName());
2480 ASSERT_TRUE(device.has_value());
2481
Prabir Pradhana3621852022-10-14 18:57:23 +00002482 // An external stylus with buttons should also be recognized as a keyboard.
2483 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002484 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
2485 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
2486
2487 const auto DOWN =
2488 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
2489 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
2490
2491 stylus->pressAndReleaseKey(BTN_STYLUS);
2492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2493 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2494 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2495 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2496
2497 stylus->pressAndReleaseKey(BTN_STYLUS2);
2498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2499 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2501 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
2502
2503 stylus->pressAndReleaseKey(BTN_STYLUS3);
2504 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2505 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2507 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
2508}
2509
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07002510/**
2511 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
2512 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
2513 * are passed to the listener.
2514 */
2515static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
2516TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
2517 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
2518 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2519 NotifyKeyArgs keyArgs;
2520
2521 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
2522 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2523 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2524 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
2525
2526 controller->pressAndReleaseKey(BTN_GEAR_UP);
2527 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
2528 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
2529 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
2530}
2531
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002532// --- TouchIntegrationTest ---
2533
Arthur Hungaab25622020-01-16 11:22:11 +08002534class TouchIntegrationTest : public InputReaderIntegrationTest {
2535protected:
Arthur Hungaab25622020-01-16 11:22:11 +08002536 const std::string UNIQUE_ID = "local:0";
2537
Chris Yea52ade12020-08-27 16:49:20 -07002538 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07002539#if !defined(__ANDROID__)
2540 GTEST_SKIP();
2541#endif
Arthur Hungaab25622020-01-16 11:22:11 +08002542 InputReaderIntegrationTest::SetUp();
2543 // At least add an internal display.
2544 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2545 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002546 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08002547
2548 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
2549 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2550 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00002551 const auto info = findDeviceByName(mDevice->getName());
2552 ASSERT_TRUE(info);
2553 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08002554 }
2555
2556 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2557 int32_t orientation, const std::string& uniqueId,
2558 std::optional<uint8_t> physicalPort,
2559 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002560 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2561 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08002562 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2563 }
2564
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002565 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
2566 NotifyMotionArgs args;
2567 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2568 EXPECT_EQ(action, args.action);
2569 ASSERT_EQ(points.size(), args.pointerCount);
2570 for (size_t i = 0; i < args.pointerCount; i++) {
2571 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
2572 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
2573 }
2574 }
2575
Arthur Hungaab25622020-01-16 11:22:11 +08002576 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00002577 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08002578};
2579
Prabir Pradhanf9a41282022-10-25 17:15:50 +00002580TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
2581 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
2582 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
2583 // presses).
2584 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
2585 mDeviceInfo.getSources());
2586}
2587
Arthur Hungaab25622020-01-16 11:22:11 +08002588TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
2589 NotifyMotionArgs args;
2590 const Point centerPoint = mDevice->getCenterPoint();
2591
2592 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002593 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002594 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002595 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002596 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2597 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2598
2599 // ACTION_MOVE
2600 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002601 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002602 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2604
2605 // ACTION_UP
2606 mDevice->sendUp();
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));
2609 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2610}
2611
2612TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
2613 NotifyMotionArgs args;
2614 const Point centerPoint = mDevice->getCenterPoint();
2615
2616 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00002617 mDevice->sendSlot(FIRST_SLOT);
2618 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002619 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002620 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002621 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2622 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2623
2624 // ACTION_POINTER_DOWN (Second slot)
2625 const Point secondPoint = centerPoint + Point(100, 100);
2626 mDevice->sendSlot(SECOND_SLOT);
2627 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002628 mDevice->sendDown(secondPoint);
2629 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002630 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002631 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002632
2633 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002634 mDevice->sendMove(secondPoint + Point(1, 1));
2635 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002636 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2638
2639 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08002640 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002641 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002642 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002643 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002644
2645 // ACTION_UP
2646 mDevice->sendSlot(FIRST_SLOT);
2647 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002648 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002649 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2650 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
2651}
2652
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002653/**
2654 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
2655 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
2656 * data?
2657 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
2658 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
2659 * for Pointer 0 only is generated after.
2660 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
2661 * events, we will not miss any information.
2662 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
2663 * event generated afterwards that contains the newest movement of pointer 0.
2664 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
2665 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
2666 * losing information about non-palm pointers.
2667 */
2668TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
2669 NotifyMotionArgs args;
2670 const Point centerPoint = mDevice->getCenterPoint();
2671
2672 // ACTION_DOWN
2673 mDevice->sendSlot(FIRST_SLOT);
2674 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2675 mDevice->sendDown(centerPoint);
2676 mDevice->sendSync();
2677 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2678
2679 // ACTION_POINTER_DOWN (Second slot)
2680 const Point secondPoint = centerPoint + Point(100, 100);
2681 mDevice->sendSlot(SECOND_SLOT);
2682 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2683 mDevice->sendDown(secondPoint);
2684 mDevice->sendSync();
2685 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2686
2687 // ACTION_MOVE (First slot)
2688 mDevice->sendSlot(FIRST_SLOT);
2689 mDevice->sendMove(centerPoint + Point(5, 5));
2690 // ACTION_POINTER_UP (Second slot)
2691 mDevice->sendSlot(SECOND_SLOT);
2692 mDevice->sendPointerUp();
2693 // Send a single sync for the above 2 pointer updates
2694 mDevice->sendSync();
2695
2696 // First, we should get POINTER_UP for the second pointer
2697 assertReceivedMotion(ACTION_POINTER_1_UP,
2698 {/*first pointer */ centerPoint + Point(5, 5),
2699 /*second pointer*/ secondPoint});
2700
2701 // Next, the MOVE event for the first pointer
2702 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2703}
2704
2705/**
2706 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
2707 * move, and then it will go up, all in the same frame.
2708 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
2709 * gets sent to the listener.
2710 */
2711TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
2712 NotifyMotionArgs args;
2713 const Point centerPoint = mDevice->getCenterPoint();
2714
2715 // ACTION_DOWN
2716 mDevice->sendSlot(FIRST_SLOT);
2717 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2718 mDevice->sendDown(centerPoint);
2719 mDevice->sendSync();
2720 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
2721
2722 // ACTION_POINTER_DOWN (Second slot)
2723 const Point secondPoint = centerPoint + Point(100, 100);
2724 mDevice->sendSlot(SECOND_SLOT);
2725 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2726 mDevice->sendDown(secondPoint);
2727 mDevice->sendSync();
2728 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
2729
2730 // ACTION_MOVE (First slot)
2731 mDevice->sendSlot(FIRST_SLOT);
2732 mDevice->sendMove(centerPoint + Point(5, 5));
2733 // ACTION_POINTER_UP (Second slot)
2734 mDevice->sendSlot(SECOND_SLOT);
2735 mDevice->sendMove(secondPoint + Point(6, 6));
2736 mDevice->sendPointerUp();
2737 // Send a single sync for the above 2 pointer updates
2738 mDevice->sendSync();
2739
2740 // First, we should get POINTER_UP for the second pointer
2741 // The movement of the second pointer during the liftoff frame is ignored.
2742 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
2743 assertReceivedMotion(ACTION_POINTER_1_UP,
2744 {/*first pointer */ centerPoint + Point(5, 5),
2745 /*second pointer*/ secondPoint});
2746
2747 // Next, the MOVE event for the first pointer
2748 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
2749}
2750
Arthur Hungaab25622020-01-16 11:22:11 +08002751TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
2752 NotifyMotionArgs args;
2753 const Point centerPoint = mDevice->getCenterPoint();
2754
2755 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08002756 mDevice->sendSlot(FIRST_SLOT);
2757 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08002758 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002759 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002760 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2761 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
2762
arthurhungcc7f9802020-04-30 17:55:40 +08002763 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002764 const Point secondPoint = centerPoint + Point(100, 100);
2765 mDevice->sendSlot(SECOND_SLOT);
2766 mDevice->sendTrackingId(SECOND_TRACKING_ID);
2767 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002768 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002770 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002771
arthurhungcc7f9802020-04-30 17:55:40 +08002772 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002773 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002774 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002775 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
2777
arthurhungcc7f9802020-04-30 17:55:40 +08002778 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2779 // a palm event.
2780 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002781 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002782 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002783 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002784 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002785 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002786
arthurhungcc7f9802020-04-30 17:55:40 +08002787 // Send up to second slot, expect first slot send moving.
2788 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002789 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002790 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002792
arthurhungcc7f9802020-04-30 17:55:40 +08002793 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002794 mDevice->sendSlot(FIRST_SLOT);
2795 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002796 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002797
arthurhungcc7f9802020-04-30 17:55:40 +08002798 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2799 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002800}
2801
Prabir Pradhanda20b172022-09-26 17:01:18 +00002802TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2803 const Point centerPoint = mDevice->getCenterPoint();
2804
2805 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2806 mDevice->sendSlot(FIRST_SLOT);
2807 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2808 mDevice->sendToolType(MT_TOOL_PEN);
2809 mDevice->sendDown(centerPoint);
2810 mDevice->sendSync();
2811 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2812 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2813 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2814
2815 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2816
2817 // Release the stylus touch.
2818 mDevice->sendUp();
2819 mDevice->sendSync();
2820 ASSERT_NO_FATAL_FAILURE(
2821 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2822
2823 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2824
2825 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2826 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2827 mDevice->sendToolType(MT_TOOL_FINGER);
2828 mDevice->sendDown(centerPoint);
2829 mDevice->sendSync();
2830 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2831 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2832 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2833
2834 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2835
2836 mDevice->sendUp();
2837 mDevice->sendSync();
2838 ASSERT_NO_FATAL_FAILURE(
2839 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2840
2841 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2842 // The policy should be notified of the stylus presence.
2843 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2844 mDevice->sendToolType(MT_TOOL_PEN);
2845 mDevice->sendMove(centerPoint);
2846 mDevice->sendSync();
2847 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2848 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2849 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2850
2851 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2852}
2853
Prabir Pradhan124ea442022-10-28 20:27:44 +00002854// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002855
Prabir Pradhan124ea442022-10-28 20:27:44 +00002856// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2857// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2858// stylus.
2859template <typename UinputStylusDevice>
2860class StylusButtonIntegrationTest : public TouchIntegrationTest {
2861protected:
2862 void SetUp() override {
2863#if !defined(__ANDROID__)
2864 GTEST_SKIP();
2865#endif
2866 TouchIntegrationTest::SetUp();
2867 mTouchscreen = mDevice.get();
2868 mTouchscreenInfo = mDeviceInfo;
2869
2870 setUpStylusDevice();
2871 }
2872
2873 UinputStylusDevice* mStylus{nullptr};
2874 InputDeviceInfo mStylusInfo{};
2875
2876 UinputTouchScreen* mTouchscreen{nullptr};
2877 InputDeviceInfo mTouchscreenInfo{};
2878
2879private:
2880 // When we are attempting to test stylus button events that are sent from the touchscreen,
2881 // use the same Uinput device for the touchscreen and the stylus.
2882 template <typename T = UinputStylusDevice>
2883 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2884 mStylus = mDevice.get();
2885 mStylusInfo = mDeviceInfo;
2886 }
2887
2888 // When we are attempting to stylus buttons from an external stylus being merged with touches
2889 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2890 template <typename T = UinputStylusDevice>
2891 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2892 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2893 mStylus = mStylusDeviceLifecycleTracker.get();
2894 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2895 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2896 const auto info = findDeviceByName(mStylus->getName());
2897 ASSERT_TRUE(info);
2898 mStylusInfo = *info;
2899 }
2900
2901 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2902
2903 // Hide the base class's device to expose it with a different name for readability.
2904 using TouchIntegrationTest::mDevice;
2905 using TouchIntegrationTest::mDeviceInfo;
2906};
2907
2908using StylusButtonIntegrationTestTypes =
2909 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2910TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2911
2912TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
2913 const auto stylusId = TestFixture::mStylusInfo.getId();
2914
2915 TestFixture::mStylus->pressKey(BTN_STYLUS);
2916 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2917 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2918 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2919
2920 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2921 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002922 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002923 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002924}
2925
Prabir Pradhan124ea442022-10-28 20:27:44 +00002926TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
2927 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2928 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2929 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002930
2931 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002932 TestFixture::mStylus->pressKey(BTN_STYLUS);
2933 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002934 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002935 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002936
2937 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002938 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2939 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2940 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2941 TestFixture::mTouchscreen->sendDown(centerPoint);
2942 TestFixture::mTouchscreen->sendSync();
2943 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002944 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2945 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002946 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2947 WithDeviceId(touchscreenId))));
2948 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002949 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2950 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002951 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2952 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002953
Prabir Pradhan124ea442022-10-28 20:27:44 +00002954 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2955 TestFixture::mTouchscreen->sendSync();
2956 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002957 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002958 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2959 WithDeviceId(touchscreenId))));
2960 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002961 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002962 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2963 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002964
2965 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002966 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2967 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002968 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002969 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002970}
2971
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002972TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
2973 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2974 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2975 const auto stylusId = TestFixture::mStylusInfo.getId();
2976 auto toolTypeDevice =
2977 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
2978
2979 // Press the stylus button.
2980 TestFixture::mStylus->pressKey(BTN_STYLUS);
2981 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2982 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2983 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2984
2985 // Start hovering with the stylus.
2986 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2987 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2988 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2989 TestFixture::mTouchscreen->sendMove(centerPoint);
2990 TestFixture::mTouchscreen->sendSync();
2991 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2992 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2993 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2994 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2995 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2997 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2998 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2999 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3000
3001 // Touch down with the stylus.
3002 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
3003 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
3004 TestFixture::mTouchscreen->sendDown(centerPoint);
3005 TestFixture::mTouchscreen->sendSync();
3006 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3007 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3008 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3009
3010 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3011 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3012 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3013
3014 // Stop touching with the stylus, and start hovering.
3015 TestFixture::mTouchscreen->sendUp();
3016 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
3017 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
3018 TestFixture::mTouchscreen->sendMove(centerPoint);
3019 TestFixture::mTouchscreen->sendSync();
3020 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3021 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
3022 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3023 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3024 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
3025 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3026 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3027 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
3028 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3029
3030 // Stop hovering.
3031 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
3032 TestFixture::mTouchscreen->sendSync();
3033 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3034 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
3035 WithButtonState(0))));
3036 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
3037 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
3038 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
3039 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
3040
3041 // Release the stylus button.
3042 TestFixture::mStylus->releaseKey(BTN_STYLUS);
3043 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
3044 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
3045 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3046}
3047
Prabir Pradhan124ea442022-10-28 20:27:44 +00003048TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
3049 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
3050 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
3051 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003052
3053 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003054 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
3055 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
3056 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
3057 TestFixture::mTouchscreen->sendDown(centerPoint);
3058 TestFixture::mTouchscreen->sendSync();
3059 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003060 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003061 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3062 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003063
3064 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003065 TestFixture::mStylus->pressKey(BTN_STYLUS);
3066 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003067 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003068 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3069 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003070 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
3071 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003072 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
3073 WithDeviceId(touchscreenId))));
3074 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003075 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
3076 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003077 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
3078 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003079
Prabir Pradhan124ea442022-10-28 20:27:44 +00003080 TestFixture::mStylus->releaseKey(BTN_STYLUS);
3081 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003082 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003083 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
3084 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003085 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003086 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3087 WithDeviceId(touchscreenId))));
3088 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003089 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003090 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3091 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003092
3093 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00003094 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
3095 TestFixture::mTouchscreen->sendSync();
3096 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003097 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00003098 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3099 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00003100}
3101
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003102// --- ExternalStylusIntegrationTest ---
3103
3104// Verify the behavior of an external stylus. An external stylus can report pressure or button
3105// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
3106// ongoing stylus gesture that is being emitted by the touchscreen.
3107using ExternalStylusIntegrationTest = TouchIntegrationTest;
3108
3109TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
3110 const Point centerPoint = mDevice->getCenterPoint();
3111
3112 // Create an external stylus capable of reporting pressure data that
3113 // should be fused with a touch pointer.
3114 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3115 createUinputDevice<UinputExternalStylusWithPressure>();
3116 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3117 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3118 const auto stylusInfo = findDeviceByName(stylus->getName());
3119 ASSERT_TRUE(stylusInfo);
3120
3121 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3122
3123 const auto touchscreenId = mDeviceInfo.getId();
3124
3125 // Set a pressure value on the stylus. It doesn't generate any events.
3126 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
3127 stylus->setPressure(100);
3128 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3129
3130 // Start a finger gesture, and ensure it shows up as stylus gesture
3131 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003132 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07003133 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003134 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003135 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136 mDevice->sendSync();
3137 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3138 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003139 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3140 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003141
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003142 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
3143 // event with the updated pressure.
3144 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00003145 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3146 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003147 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3148 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003149
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003150 // The external stylus did not generate any events.
3151 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3152 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3153}
3154
3155TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
3156 const Point centerPoint = mDevice->getCenterPoint();
3157
3158 // Create an external stylus capable of reporting pressure data that
3159 // should be fused with a touch pointer.
3160 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
3161 createUinputDevice<UinputExternalStylusWithPressure>();
3162 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3163 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3164 const auto stylusInfo = findDeviceByName(stylus->getName());
3165 ASSERT_TRUE(stylusInfo);
3166
3167 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3168
3169 const auto touchscreenId = mDeviceInfo.getId();
3170
3171 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
3172 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00003173 // Send a non-zero value first to prevent the kernel from consuming the zero event.
3174 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003175 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00003176 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003177
3178 // Start a finger gesture. The touch device will withhold generating any touches for
3179 // up to 72 milliseconds while waiting for pressure data from the external stylus.
3180 mDevice->sendSlot(FIRST_SLOT);
3181 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3182 mDevice->sendToolType(MT_TOOL_FINGER);
3183 mDevice->sendDown(centerPoint);
3184 auto waitUntil = std::chrono::system_clock::now() +
3185 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07003186 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003187 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003189 // Since the external stylus did not report a pressure value within the timeout,
3190 // it shows up as a finger pointer.
3191 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3192 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3193 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
3194 WithPressure(1.f))));
3195
3196 // Change the pressure on the external stylus. Since the pressure was not present at the start
3197 // of the gesture, it is ignored for now.
3198 stylus->setPressure(200);
3199 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3200
3201 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08003202 mDevice->sendTrackingId(INVALID_TRACKING_ID);
3203 mDevice->sendSync();
3204 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3205 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00003206 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
3207
3208 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
3209 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3210 mDevice->sendToolType(MT_TOOL_FINGER);
3211 mDevice->sendDown(centerPoint);
3212 mDevice->sendSync();
3213 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
3214 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3215 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
3216 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
3217
3218 // The external stylus did not generate any events.
3219 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3220 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003222
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00003223TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
3224 const Point centerPoint = mDevice->getCenterPoint();
3225
3226 // Create an external stylus device that does not support pressure. It should not affect any
3227 // touch pointers.
3228 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
3229 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
3230 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
3231 const auto stylusInfo = findDeviceByName(stylus->getName());
3232 ASSERT_TRUE(stylusInfo);
3233
3234 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
3235
3236 const auto touchscreenId = mDeviceInfo.getId();
3237
3238 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
3239 // pressure data from the external stylus.
3240 mDevice->sendSlot(FIRST_SLOT);
3241 mDevice->sendTrackingId(FIRST_TRACKING_ID);
3242 mDevice->sendToolType(MT_TOOL_FINGER);
3243 mDevice->sendDown(centerPoint);
3244 auto waitUntil = std::chrono::system_clock::now() +
3245 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
3246 mDevice->sendSync();
3247 ASSERT_NO_FATAL_FAILURE(
3248 mTestListener
3249 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
3250 WithToolType(
3251 AMOTION_EVENT_TOOL_TYPE_FINGER),
3252 WithButtonState(0),
3253 WithDeviceId(touchscreenId),
3254 WithPressure(1.f)),
3255 waitUntil));
3256
3257 // The external stylus did not generate any events.
3258 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
3259 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
3260}
3261
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262// --- InputDeviceTest ---
3263class InputDeviceTest : public testing::Test {
3264protected:
3265 static const char* DEVICE_NAME;
3266 static const char* DEVICE_LOCATION;
3267 static const int32_t DEVICE_ID;
3268 static const int32_t DEVICE_GENERATION;
3269 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003270 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 static const int32_t EVENTHUB_ID;
3272 static const std::string DEVICE_BLUETOOTH_ADDRESS;
3273
3274 std::shared_ptr<FakeEventHub> mFakeEventHub;
3275 sp<FakeInputReaderPolicy> mFakePolicy;
3276 std::unique_ptr<TestInputListener> mFakeListener;
3277 std::unique_ptr<InstrumentedInputReader> mReader;
3278 std::shared_ptr<InputDevice> mDevice;
3279
3280 void SetUp() override {
3281 mFakeEventHub = std::make_unique<FakeEventHub>();
3282 mFakePolicy = sp<FakeInputReaderPolicy>::make();
3283 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003284 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003286 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003287 identifier.name = DEVICE_NAME;
3288 identifier.location = DEVICE_LOCATION;
3289 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
3290 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
3291 identifier);
3292 mReader->pushNextDevice(mDevice);
3293 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003294 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003296
3297 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003298 mFakeListener.reset();
3299 mFakePolicy.clear();
3300 }
3301};
3302
3303const char* InputDeviceTest::DEVICE_NAME = "device";
3304const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
3305const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
3306const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003308const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
3309 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003310const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
3312
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003313TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003315 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
3316 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003317}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003318
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003320 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
3321
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322 // Configuration
3323 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003324 InputReaderConfiguration config;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003325 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
3326
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
3328}
3329
3330TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
3331 ASSERT_EQ(mDevice->isEnabled(), false);
3332}
3333
3334TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
3335 // Configuration.
3336 InputReaderConfiguration config;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00003337 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01003339 // Reset.
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 unused += mDevice->reset(ARBITRARY_TIME);
3341
3342 NotifyDeviceResetArgs resetArgs;
3343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3344 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
3345 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3346
3347 // Metadata.
3348 ASSERT_TRUE(mDevice->isIgnored());
3349 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
3350
3351 InputDeviceInfo info = mDevice->getDeviceInfo();
3352 ASSERT_EQ(DEVICE_ID, info.getId());
3353 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
3354 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
3355 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
3356
3357 // State queries.
3358 ASSERT_EQ(0, mDevice->getMetaState());
3359
3360 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003361 << "Ignored device should return unknown key code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003363 << "Ignored device should return unknown scan code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
3365 << "Ignored device should return unknown switch state.";
3366
3367 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
3368 uint8_t flags[2] = { 0, 1 };
3369 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003370 << "Ignored device should never mark any key codes.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
3372 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
3373}
3374
3375TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
3376 // Configuration.
3377 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
3378
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003379 FakeInputMapper& mapper1 =
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003380 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003381 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003382 mapper1.setMetaState(AMETA_ALT_ON);
3383 mapper1.addSupportedKeyCode(AKEYCODE_A);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003384 mapper1.addSupportedKeyCode(AKEYCODE_B);
3385 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003386 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
3387 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
3388 mapper1.setScanCodeState(3, AKEY_STATE_UP);
3389 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003390
Arthur Hung2c9a3342019-07-23 14:18:59 +08003391 FakeInputMapper& mapper2 =
3392 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003393 mapper2.setMetaState(AMETA_SHIFT_ON);
3394
Arthur Hung2c9a3342019-07-23 14:18:59 +08003395 InputReaderConfiguration config;
3396 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
3397
3398 std::string propertyValue;
3399 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
3400 << "Device should have read configuration during configuration phase.";
3401 ASSERT_EQ("value", propertyValue);
3402
3403 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003404 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
3405
Arthur Hung2c9a3342019-07-23 14:18:59 +08003406 // Reset
3407 unused += mDevice->reset(ARBITRARY_TIME);
3408 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
3409 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
3410
3411 NotifyDeviceResetArgs resetArgs;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
3413 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003414 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
3415
Arthur Hung2c9a3342019-07-23 14:18:59 +08003416 // Metadata.
3417 ASSERT_FALSE(mDevice->isIgnored());
3418 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
3419
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003420 InputDeviceInfo info = mDevice->getDeviceInfo();
3421 ASSERT_EQ(DEVICE_ID, info.getId());
Arthur Hung2c9a3342019-07-23 14:18:59 +08003422 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
3423 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
3424 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003425
3426 // State queries.
Arthur Hung2c9a3342019-07-23 14:18:59 +08003427 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
3428 << "Should query mappers and combine meta states.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08003429
Christine Franks1ba71cc2021-04-07 14:37:42 -07003430 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3431 << "Should return unknown key code state when source not supported.";
3432 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3433 << "Should return unknown scan code state when source not supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003434 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
3435 << "Should return unknown switch state when source not supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07003436
3437 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
3438 << "Should query mapper when source is supported.";
3439 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
Christine Franks2a2293c2022-01-18 11:51:16 -08003440 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003441 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
3442 << "Should query mapper when source is supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07003443
3444 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
3445 uint8_t flags[4] = { 0, 0, 0, 1 };
3446 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
3447 << "Should do nothing when source is unsupported.";
3448 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003449 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
3450 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07003451 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
3452
3453 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
3454 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003455 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
3456 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07003457 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
3458 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
3459
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003460 // Event handling.
3461 RawEvent event;
Christine Franks1ba71cc2021-04-07 14:37:42 -07003462 event.deviceId = EVENTHUB_ID;
3463 unused += mDevice->process(&event, 1);
3464
Christine Franks2a2293c2022-01-18 11:51:16 -08003465 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
3466 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
3467}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003468
3469// A single input device is associated with a specific display. Check that:
Christine Franks2a2293c2022-01-18 11:51:16 -08003470// 1. Device is disabled if the viewport corresponding to the associated display is not found
Christine Franks2a2293c2022-01-18 11:51:16 -08003471// 2. Device is disabled when setEnabled API is called
3472TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
3473 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
3474
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003475 // First Configuration.
3476 std::list<NotifyArgs> unused =
Christine Franks2a2293c2022-01-18 11:51:16 -08003477 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3478
3479 // Device should be enabled by default.
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07003480 ASSERT_TRUE(mDevice->isEnabled());
3481
3482 // Prepare associated info.
3483 constexpr uint8_t hdmi = 1;
3484 const std::string UNIQUE_ID = "local:1";
3485
3486 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
3487 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3488 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3489 // Device should be disabled because it is associated with a specific display via
3490 // input port <-> display port association, but the corresponding display is not found
3491 ASSERT_FALSE(mDevice->isEnabled());
3492
3493 // Prepare displays.
3494 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightd02c5b62014-02-10 15:10:22 -08003495 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
3496 ViewportType::INTERNAL);
3497 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3498 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3499 ASSERT_TRUE(mDevice->isEnabled());
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003500
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501 // Device should be disabled after set disable.
3502 mFakePolicy->addDisabledDevice(mDevice->getId());
3503 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003504 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003505 ASSERT_FALSE(mDevice->isEnabled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003507 // Device should still be disabled even found the associated display.
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003509 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
arthurhungdcef2dc2020-08-11 14:47:50 +08003510 ASSERT_FALSE(mDevice->isEnabled());
3511}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003512
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003513TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07003514 // Device should be enabled by default.
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07003515 mFakePolicy->clearViewports();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003516 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
arthurhungdcef2dc2020-08-11 14:47:50 +08003517 std::list<NotifyArgs> unused =
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003518 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
arthurhungdcef2dc2020-08-11 14:47:50 +08003519 ASSERT_TRUE(mDevice->isEnabled());
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003520
3521 // Device should be disabled because it is associated with a specific display, but the
Michael Wrightd02c5b62014-02-10 15:10:22 -08003522 // corresponding display is not found.
3523 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003524 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003525 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3526 ASSERT_FALSE(mDevice->isEnabled());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003527
Chris Yea52ade12020-08-27 16:49:20 -07003528 // Device should be enabled when a display is found.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07003529 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531 NO_PORT, ViewportType::INTERNAL);
3532 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3533 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07003534 ASSERT_TRUE(mDevice->isEnabled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
3536 // Device should be disabled after set disable.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003537 mFakePolicy->addDisabledDevice(mDevice->getId());
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00003538 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3539 InputReaderConfiguration::CHANGE_ENABLED_STATE);
3540 ASSERT_FALSE(mDevice->isEnabled());
3541
arthurhungdcef2dc2020-08-11 14:47:50 +08003542 // Device should still be disabled even found the associated display.
3543 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08003544 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003545 ASSERT_FALSE(mDevice->isEnabled());
3546}
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003547
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003548TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
3549 mFakePolicy->clearViewports();
3550 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003551 std::list<NotifyArgs> unused =
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003552 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003553
3554 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
arthurhungdcef2dc2020-08-11 14:47:50 +08003555 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3556 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003557 NO_PORT, ViewportType::INTERNAL);
arthurhungdcef2dc2020-08-11 14:47:50 +08003558 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3559 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3560 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
3561}
3562
3563/**
3564 * This test reproduces a crash caused by a dangling reference that remains after device is added
3565 * and removed. The reference is accessed in InputDevice::dump(..);
3566 */
3567TEST_F(InputDeviceTest, DumpDoesNotCrash) {
3568 constexpr int32_t TEST_EVENTHUB_ID = 10;
3569 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003570
3571 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003572 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08003573 device.removeEventHubDevice(TEST_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003574 std::string dumpStr, eventHubDevStr;
3575 device.dump(dumpStr, eventHubDevStr);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003576}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003577
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003578TEST_F(InputDeviceTest, GetBluetoothAddress) {
3579 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3580 ASSERT_TRUE(address);
3581 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3582}
3583
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584// --- InputMapperTest ---
3585
3586class InputMapperTest : public testing::Test {
3587protected:
3588 static const char* DEVICE_NAME;
3589 static const char* DEVICE_LOCATION;
3590 static const int32_t DEVICE_ID;
3591 static const int32_t DEVICE_GENERATION;
3592 static const int32_t DEVICE_CONTROLLER_NUMBER;
3593 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
3594 static const int32_t EVENTHUB_ID;
3595
3596 std::shared_ptr<FakeEventHub> mFakeEventHub;
3597 sp<FakeInputReaderPolicy> mFakePolicy;
3598 std::unique_ptr<TestInputListener> mFakeListener;
3599 std::unique_ptr<InstrumentedInputReader> mReader;
3600 std::shared_ptr<InputDevice> mDevice;
3601
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003602 virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603 mFakeEventHub = std::make_unique<FakeEventHub>();
3604 mFakePolicy = sp<FakeInputReaderPolicy>::make();
3605 mFakeListener = std::make_unique<TestInputListener>();
3606 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
3607 *mFakeListener);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003608 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes, bus);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609 // Consume the device reset notification generated when adding a new device.
3610 mFakeListener->assertNotifyDeviceResetWasCalled();
3611 }
3612
3613 void SetUp() override {
3614 SetUp(DEVICE_CLASSES);
3615 }
3616
3617 void TearDown() override {
3618 mFakeListener.reset();
3619 mFakePolicy.clear();
3620 }
3621
3622 void addConfigurationProperty(const char* key, const char* value) {
3623 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
3624 }
3625
3626 std::list<NotifyArgs> configureDevice(uint32_t changes) {
3627 if (!changes ||
3628 (changes &
3629 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
3630 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
3631 mReader->requestRefreshConfiguration(changes);
3632 mReader->loopOnce();
3633 }
3634 std::list<NotifyArgs> out =
3635 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
3636 // Loop the reader to flush the input listener queue.
3637 for (const NotifyArgs& args : out) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003638 mFakeListener->notify(args);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639 }
3640 mReader->loopOnce();
3641 return out;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003642 }
3643
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003644 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003645 const std::string& location, int32_t eventHubId,
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003646 ftl::Flags<InputDeviceClass> classes, int bus = 0) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07003647 InputDeviceIdentifier identifier;
3648 identifier.name = name;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003649 identifier.location = location;
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003650 identifier.bus = bus;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003651 std::shared_ptr<InputDevice> device =
Michael Wrightd02c5b62014-02-10 15:10:22 -08003652 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
3653 identifier);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003654 mReader->pushNextDevice(device);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00003655 mFakeEventHub->addDevice(eventHubId, name, classes, bus);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656 mReader->loopOnce();
3657 return device;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003658 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659
3660 template <class T, typename... Args>
3661 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003662 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003663 configureDevice(0);
3664 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07003665 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003666 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003667 for (const NotifyArgs& loopArgs : resetArgList) {
3668 mFakeListener->notify(loopArgs);
3669 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003670 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671 return mapper;
3672 }
3673
3674 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
3675 int32_t orientation, const std::string& uniqueId,
3676 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00003677 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
3678 uniqueId, physicalPort, viewportType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
3680 }
3681
3682 void clearViewports() {
3683 mFakePolicy->clearViewports();
3684 }
3685
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003686 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
3687 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688 RawEvent event;
3689 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691 event.deviceId = mapper.getDeviceContext().getEventHubId();
3692 event.type = type;
3693 event.code = code;
3694 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003695 std::list<NotifyArgs> processArgList = mapper.process(&event);
3696 for (const NotifyArgs& args : processArgList) {
3697 mFakeListener->notify(args);
3698 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00003699 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08003700 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003701 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003702 }
3703
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00003704 void resetMapper(InputMapper& mapper, nsecs_t when) {
3705 const auto resetArgs = mapper.reset(when);
3706 for (const auto args : resetArgs) {
3707 mFakeListener->notify(args);
3708 }
3709 // Loop the reader to flush the input listener queue.
3710 mReader->loopOnce();
3711 }
3712
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00003713 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when) {
3714 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
3715 for (const NotifyArgs& args : generatedArgs) {
3716 mFakeListener->notify(args);
3717 }
3718 // Loop the reader to flush the input listener queue.
3719 mReader->loopOnce();
3720 return generatedArgs;
3721 }
3722
Michael Wrightd02c5b62014-02-10 15:10:22 -08003723 static void assertMotionRange(const InputDeviceInfo& info,
3724 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
3725 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07003726 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
3728 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
3729 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
3730 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
3731 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
3732 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
3733 }
3734
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003735 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
3736 float size, float touchMajor, float touchMinor, float toolMajor,
3737 float toolMinor, float orientation, float distance,
3738 float scaledAxisEpsilon = 1.f) {
3739 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
3740 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
3742 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003743 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
3744 scaledAxisEpsilon);
3745 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
3746 scaledAxisEpsilon);
3747 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
3748 scaledAxisEpsilon);
3749 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
3750 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
3752 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
3753 }
3754
Michael Wright17db18e2020-06-26 20:51:44 +01003755 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01003757 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758 ASSERT_NEAR(x, actualX, 1);
3759 ASSERT_NEAR(y, actualY, 1);
3760 }
3761};
3762
3763const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003764const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003765const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003766const int32_t InputMapperTest::DEVICE_GENERATION = 2;
3767const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003768const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
3769 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003770const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771
3772// --- SwitchInputMapperTest ---
3773
3774class SwitchInputMapperTest : public InputMapperTest {
3775protected:
3776};
3777
3778TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003779 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003781 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782}
3783
3784TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003785 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003787 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003788 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003790 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003791 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792}
3793
3794TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003796 std::list<NotifyArgs> out;
3797 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3798 ASSERT_TRUE(out.empty());
3799 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3800 ASSERT_TRUE(out.empty());
3801 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3802 ASSERT_TRUE(out.empty());
3803 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003805 ASSERT_EQ(1u, out.size());
3806 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003807 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003808 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3809 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003810 args.switchMask);
3811 ASSERT_EQ(uint32_t(0), args.policyFlags);
3812}
3813
Chris Ye87143712020-11-10 05:05:58 +00003814// --- VibratorInputMapperTest ---
3815class VibratorInputMapperTest : public InputMapperTest {
3816protected:
3817 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3818};
3819
3820TEST_F(VibratorInputMapperTest, GetSources) {
3821 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3822
3823 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3824}
3825
3826TEST_F(VibratorInputMapperTest, GetVibratorIds) {
3827 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3828
3829 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3830}
3831
3832TEST_F(VibratorInputMapperTest, Vibrate) {
3833 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003834 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00003835 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
3836
3837 VibrationElement pattern(2);
3838 VibrationSequence sequence(2);
3839 pattern.duration = std::chrono::milliseconds(200);
3840 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
3841 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3842 sequence.addElement(pattern);
3843 pattern.duration = std::chrono::milliseconds(500);
3844 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
3845 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
3846 sequence.addElement(pattern);
3847
3848 std::vector<int64_t> timings = {0, 1};
3849 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3850
3851 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003852 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003853 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003854 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003855 // Verify vibrator state listener was notified.
3856 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003857 ASSERT_EQ(1u, out.size());
3858 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3859 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3860 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003861 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003862 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003863 ASSERT_FALSE(mapper.isVibrating());
3864 // Verify vibrator state listener was notified.
3865 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003866 ASSERT_EQ(1u, out.size());
3867 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3868 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3869 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003870}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003871
Chris Yef59a2f42020-10-16 12:55:26 -07003872// --- SensorInputMapperTest ---
3873
3874class SensorInputMapperTest : public InputMapperTest {
3875protected:
3876 static const int32_t ACCEL_RAW_MIN;
3877 static const int32_t ACCEL_RAW_MAX;
3878 static const int32_t ACCEL_RAW_FUZZ;
3879 static const int32_t ACCEL_RAW_FLAT;
3880 static const int32_t ACCEL_RAW_RESOLUTION;
3881
3882 static const int32_t GYRO_RAW_MIN;
3883 static const int32_t GYRO_RAW_MAX;
3884 static const int32_t GYRO_RAW_FUZZ;
3885 static const int32_t GYRO_RAW_FLAT;
3886 static const int32_t GYRO_RAW_RESOLUTION;
3887
3888 static const float GRAVITY_MS2_UNIT;
3889 static const float DEGREE_RADIAN_UNIT;
3890
3891 void prepareAccelAxes();
3892 void prepareGyroAxes();
3893 void setAccelProperties();
3894 void setGyroProperties();
3895 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3896};
3897
3898const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3899const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3900const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3901const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3902const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3903
3904const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3905const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3906const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3907const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3908const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3909
3910const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3911const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3912
3913void SensorInputMapperTest::prepareAccelAxes() {
3914 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3915 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3916 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3917 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3919 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3920}
3921
3922void SensorInputMapperTest::prepareGyroAxes() {
3923 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3924 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3925 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3926 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3927 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3928 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3929}
3930
3931void SensorInputMapperTest::setAccelProperties() {
3932 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3933 /* sensorDataIndex */ 0);
3934 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3935 /* sensorDataIndex */ 1);
3936 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3937 /* sensorDataIndex */ 2);
3938 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3939 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3940 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3941 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3942 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3943}
3944
3945void SensorInputMapperTest::setGyroProperties() {
3946 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3947 /* sensorDataIndex */ 0);
3948 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3949 /* sensorDataIndex */ 1);
3950 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3951 /* sensorDataIndex */ 2);
3952 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3953 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3954 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3955 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3956 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3957}
3958
3959TEST_F(SensorInputMapperTest, GetSources) {
3960 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3961
3962 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3963}
3964
3965TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3966 setAccelProperties();
3967 prepareAccelAxes();
3968 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3969
3970 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3971 std::chrono::microseconds(10000),
3972 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003973 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003979
3980 NotifySensorArgs args;
3981 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3982 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3983 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3984
3985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3986 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3987 ASSERT_EQ(args.deviceId, DEVICE_ID);
3988 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3989 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3990 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3991 ASSERT_EQ(args.values, values);
3992 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3993}
3994
3995TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3996 setGyroProperties();
3997 prepareGyroAxes();
3998 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3999
4000 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
4001 std::chrono::microseconds(10000),
4002 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08004003 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
4005 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
4006 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
4007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
4008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07004009
4010 NotifySensorArgs args;
4011 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
4012 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
4013 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
4014
4015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
4016 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
4017 ASSERT_EQ(args.deviceId, DEVICE_ID);
4018 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
4019 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
4020 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
4021 ASSERT_EQ(args.values, values);
4022 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
4023}
4024
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025// --- KeyboardInputMapperTest ---
4026
4027class KeyboardInputMapperTest : public InputMapperTest {
4028protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004029 const std::string UNIQUE_ID = "local:0";
4030
4031 void prepareDisplay(int32_t orientation);
4032
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004033 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08004034 int32_t originalKeyCode, int32_t rotatedKeyCode,
4035 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036};
4037
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004038/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
4039 * orientation.
4040 */
4041void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004042 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4043 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004044}
4045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004046void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08004047 int32_t originalScanCode, int32_t originalKeyCode,
4048 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 NotifyKeyArgs args;
4050
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4053 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4054 ASSERT_EQ(originalScanCode, args.scanCode);
4055 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004056 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4060 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4061 ASSERT_EQ(originalScanCode, args.scanCode);
4062 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004063 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064}
4065
Michael Wrightd02c5b62014-02-10 15:10:22 -08004066TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004067 KeyboardInputMapper& mapper =
4068 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4069 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004071 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072}
4073
4074TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
4075 const int32_t USAGE_A = 0x070004;
4076 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004077 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4078 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07004079 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
4080 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
4081 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004083 KeyboardInputMapper& mapper =
4084 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4085 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004086 // Initial metastate is AMETA_NONE.
4087 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088
4089 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004091 NotifyKeyArgs args;
4092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4093 ASSERT_EQ(DEVICE_ID, args.deviceId);
4094 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4095 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4096 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4097 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4098 ASSERT_EQ(KEY_HOME, args.scanCode);
4099 ASSERT_EQ(AMETA_NONE, args.metaState);
4100 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4101 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4102 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4103
4104 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004105 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4107 ASSERT_EQ(DEVICE_ID, args.deviceId);
4108 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4109 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4110 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4111 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4112 ASSERT_EQ(KEY_HOME, args.scanCode);
4113 ASSERT_EQ(AMETA_NONE, args.metaState);
4114 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4115 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4116 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4117
4118 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
4120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4122 ASSERT_EQ(DEVICE_ID, args.deviceId);
4123 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4124 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4125 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4126 ASSERT_EQ(AKEYCODE_A, args.keyCode);
4127 ASSERT_EQ(0, args.scanCode);
4128 ASSERT_EQ(AMETA_NONE, args.metaState);
4129 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4130 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4131 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4132
4133 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
4135 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4137 ASSERT_EQ(DEVICE_ID, args.deviceId);
4138 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4139 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4140 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4141 ASSERT_EQ(AKEYCODE_A, args.keyCode);
4142 ASSERT_EQ(0, args.scanCode);
4143 ASSERT_EQ(AMETA_NONE, args.metaState);
4144 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4145 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4146 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4147
4148 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
4150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4152 ASSERT_EQ(DEVICE_ID, args.deviceId);
4153 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4154 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4155 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4156 ASSERT_EQ(0, args.keyCode);
4157 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
4158 ASSERT_EQ(AMETA_NONE, args.metaState);
4159 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4160 ASSERT_EQ(0U, args.policyFlags);
4161 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4162
4163 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
4165 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(DEVICE_ID, args.deviceId);
4168 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4169 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4170 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4171 ASSERT_EQ(0, args.keyCode);
4172 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
4173 ASSERT_EQ(AMETA_NONE, args.metaState);
4174 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4175 ASSERT_EQ(0U, args.policyFlags);
4176 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4177}
4178
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004179/**
4180 * Ensure that the readTime is set to the time when the EV_KEY is received.
4181 */
4182TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
4183 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4184
4185 KeyboardInputMapper& mapper =
4186 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4187 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4188 NotifyKeyArgs args;
4189
4190 // Key down
4191 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
4192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4193 ASSERT_EQ(12, args.readTime);
4194
4195 // Key up
4196 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
4197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4198 ASSERT_EQ(15, args.readTime);
4199}
4200
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004202 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
4203 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004204 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
4205 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
4206 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004208 KeyboardInputMapper& mapper =
4209 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4210 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211
Arthur Hung95f68612022-04-07 14:08:22 +08004212 // Initial metastate is AMETA_NONE.
4213 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214
4215 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004217 NotifyKeyArgs args;
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004220 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004221 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004222
4223 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004224 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4226 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228
4229 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004230 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4232 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004233 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234
4235 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004236 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4238 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004239 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08004240 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241}
4242
4243TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004244 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4245 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4246 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4247 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004249 KeyboardInputMapper& mapper =
4250 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4251 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004253 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4255 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
4256 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4257 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
4258 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4259 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
4260 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
4261 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
4262}
4263
4264TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004265 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4266 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4267 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4268 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004271 KeyboardInputMapper& mapper =
4272 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4273 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004275 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004276 ASSERT_NO_FATAL_FAILURE(
4277 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4278 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4279 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4280 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4281 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4282 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4283 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004285 clearViewports();
4286 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004287 ASSERT_NO_FATAL_FAILURE(
4288 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4289 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4290 AKEYCODE_DPAD_UP, DISPLAY_ID));
4291 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4292 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4293 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4294 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004296 clearViewports();
4297 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004298 ASSERT_NO_FATAL_FAILURE(
4299 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4300 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4301 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4302 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4303 AKEYCODE_DPAD_UP, DISPLAY_ID));
4304 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4305 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004307 clearViewports();
4308 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004309 ASSERT_NO_FATAL_FAILURE(
4310 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4311 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4312 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4313 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4314 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4315 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4316 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317
4318 // Special case: if orientation changes while key is down, we still emit the same keycode
4319 // in the key up as we did in the key down.
4320 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004321 clearViewports();
4322 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4325 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4326 ASSERT_EQ(KEY_UP, args.scanCode);
4327 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4328
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004329 clearViewports();
4330 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4333 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4334 ASSERT_EQ(KEY_UP, args.scanCode);
4335 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
4336}
4337
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004338TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
4339 // If the keyboard is not orientation aware,
4340 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004341 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004342
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004343 KeyboardInputMapper& mapper =
4344 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4345 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004346 NotifyKeyArgs args;
4347
4348 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4353 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4354
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004355 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4360 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
4361}
4362
4363TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
4364 // If the keyboard is orientation aware,
4365 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004366 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004367
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004368 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004369 KeyboardInputMapper& mapper =
4370 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4371 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004372 NotifyKeyArgs args;
4373
4374 // Display id should be ADISPLAY_ID_NONE without any display configuration.
4375 // ^--- already checked by the previous test
4376
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004377 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004378 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4383 ASSERT_EQ(DISPLAY_ID, args.displayId);
4384
4385 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004386 clearViewports();
4387 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004388 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004389 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01004392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4393 ASSERT_EQ(newDisplayId, args.displayId);
4394}
4395
Michael Wrightd02c5b62014-02-10 15:10:22 -08004396TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004397 KeyboardInputMapper& mapper =
4398 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4399 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004401 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004402 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004404 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004405 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406}
4407
Philip Junker4af3b3d2021-12-14 10:36:55 +01004408TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
4409 KeyboardInputMapper& mapper =
4410 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4411 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4412
4413 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
4414 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
4415 << "If a mapping is available, the result is equal to the mapping";
4416
4417 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
4418 << "If no mapping is available, the result is the key location";
4419}
4420
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004422 KeyboardInputMapper& mapper =
4423 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4424 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004426 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004427 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004428
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004429 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004430 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431}
4432
4433TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004434 KeyboardInputMapper& mapper =
4435 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4436 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004438 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004441 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442 ASSERT_TRUE(flags[0]);
4443 ASSERT_FALSE(flags[1]);
4444}
4445
4446TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004447 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4448 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4449 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4450 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4451 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4452 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004454 KeyboardInputMapper& mapper =
4455 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4456 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004457 // Initial metastate is AMETA_NONE.
4458 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004459
4460 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004461 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4462 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4463 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464
4465 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004468 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4469 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4470 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004471 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472
4473 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004476 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4477 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4478 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004479 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480
4481 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004484 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4485 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4486 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004487 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488
4489 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004492 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4493 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4494 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004495 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496
4497 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004500 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4501 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4502 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004503 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504
4505 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004508 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4509 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4510 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004511 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512}
4513
Chris Yea52ade12020-08-27 16:49:20 -07004514TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
4515 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
4516 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
4517 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
4518 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
4519
4520 KeyboardInputMapper& mapper =
4521 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4522 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4523
Chris Yea52ade12020-08-27 16:49:20 -07004524 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004525 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07004526 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4527 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
4528 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
4529 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4530
4531 NotifyKeyArgs args;
4532 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07004534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4535 ASSERT_EQ(AMETA_NONE, args.metaState);
4536 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4537 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4538 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4539
4540 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004541 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07004542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4543 ASSERT_EQ(AMETA_NONE, args.metaState);
4544 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
4545 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4546 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
4547}
4548
Arthur Hung2c9a3342019-07-23 14:18:59 +08004549TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
4550 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004551 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4552 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4553 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4554 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004555
4556 // keyboard 2.
4557 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08004558 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08004559 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004560 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08004561 std::shared_ptr<InputDevice> device2 =
4562 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004563 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08004564
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004565 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
4566 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
4567 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4568 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004569
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004570 KeyboardInputMapper& mapper =
4571 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4572 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004573
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004574 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004575 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004576 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004577 std::list<NotifyArgs> unused =
4578 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4579 0 /*changes*/);
4580 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004581
4582 // Prepared displays and associated info.
4583 constexpr uint8_t hdmi1 = 0;
4584 constexpr uint8_t hdmi2 = 1;
4585 const std::string SECONDARY_UNIQUE_ID = "local:1";
4586
4587 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
4588 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
4589
4590 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004591 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4592 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004593 ASSERT_FALSE(device2->isEnabled());
4594
4595 // Prepare second display.
4596 constexpr int32_t newDisplayId = 2;
4597 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004598 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004599 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004600 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004601 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004602 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4603 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08004604
4605 // Device should be enabled after the associated display is found.
4606 ASSERT_TRUE(mDevice->isEnabled());
4607 ASSERT_TRUE(device2->isEnabled());
4608
4609 // Test pad key events
4610 ASSERT_NO_FATAL_FAILURE(
4611 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
4612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4613 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
4614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4615 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
4616 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4617 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
4618
4619 ASSERT_NO_FATAL_FAILURE(
4620 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
4621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
4622 AKEYCODE_DPAD_RIGHT, newDisplayId));
4623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
4624 AKEYCODE_DPAD_DOWN, newDisplayId));
4625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
4626 AKEYCODE_DPAD_LEFT, newDisplayId));
4627}
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628
arthurhungc903df12020-08-11 15:08:42 +08004629TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
4630 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4631 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4632 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4633 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4636
4637 KeyboardInputMapper& mapper =
4638 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4639 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004640 // Initial metastate is AMETA_NONE.
4641 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004642
4643 // Initialization should have turned all of the lights off.
4644 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4645 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4646 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4647
4648 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004651 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4652 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4653
4654 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004657 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4658 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
4659
4660 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08004663 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4664 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
4665
4666 mFakeEventHub->removeDevice(EVENTHUB_ID);
4667 mReader->loopOnce();
4668
4669 // keyboard 2 should default toggle keys.
4670 const std::string USB2 = "USB2";
4671 const std::string DEVICE_NAME2 = "KEYBOARD2";
4672 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4673 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4674 std::shared_ptr<InputDevice> device2 =
4675 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07004676 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08004677 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4678 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4679 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4680 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4681 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4682 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4683
arthurhung6fe95782020-10-05 22:41:16 +08004684 KeyboardInputMapper& mapper2 =
4685 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4686 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004687 std::list<NotifyArgs> unused =
4688 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4689 0 /*changes*/);
4690 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004691
4692 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4693 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4694 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004695 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4696 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004697}
4698
Arthur Hungcb40a002021-08-03 14:31:01 +00004699TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4700 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4701 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4702 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4703
4704 // Suppose we have two mappers. (DPAD + KEYBOARD)
4705 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
4706 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4707 KeyboardInputMapper& mapper =
4708 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4709 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004710 // Initial metastate is AMETA_NONE.
4711 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004712
4713 mReader->toggleCapsLockState(DEVICE_ID);
4714 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4715}
4716
Arthur Hungfb3cc112022-04-13 07:39:50 +00004717TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4718 // keyboard 1.
4719 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4720 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4721 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4722 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4723 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4724 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4725
4726 KeyboardInputMapper& mapper1 =
4727 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4728 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4729
4730 // keyboard 2.
4731 const std::string USB2 = "USB2";
4732 const std::string DEVICE_NAME2 = "KEYBOARD2";
4733 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4734 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4735 std::shared_ptr<InputDevice> device2 =
4736 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4737 ftl::Flags<InputDeviceClass>(0));
4738 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4739 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4740 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4741 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4742 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4743 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4744
4745 KeyboardInputMapper& mapper2 =
4746 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
4747 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004748 std::list<NotifyArgs> unused =
4749 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4750 0 /*changes*/);
4751 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004752
Arthur Hung95f68612022-04-07 14:08:22 +08004753 // Initial metastate is AMETA_NONE.
4754 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4755 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4756
4757 // Toggle num lock on and off.
4758 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4759 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004760 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4761 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4762 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4763
4764 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4765 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4766 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4767 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4768 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4769
4770 // Toggle caps lock on and off.
4771 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4772 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4773 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4774 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4775 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4776
4777 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4778 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4779 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4780 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4781 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4782
4783 // Toggle scroll lock on and off.
4784 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4785 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4786 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4787 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4788 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4789
4790 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4791 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4792 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4793 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4794 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4795}
4796
Arthur Hung2141d542022-08-23 07:45:21 +00004797TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4798 const int32_t USAGE_A = 0x070004;
4799 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4800 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4801
4802 KeyboardInputMapper& mapper =
4803 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4804 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4805 // Key down by scan code.
4806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4807 NotifyKeyArgs args;
4808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4809 ASSERT_EQ(DEVICE_ID, args.deviceId);
4810 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4811 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4812 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4813 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4814 ASSERT_EQ(KEY_HOME, args.scanCode);
4815 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4816
4817 // Disable device, it should synthesize cancellation events for down events.
4818 mFakePolicy->addDisabledDevice(DEVICE_ID);
4819 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
4820
4821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4822 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4823 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4824 ASSERT_EQ(KEY_HOME, args.scanCode);
4825 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4826}
4827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004828// --- KeyboardInputMapperTest_ExternalDevice ---
4829
4830class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4831protected:
Chris Yea52ade12020-08-27 16:49:20 -07004832 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004833};
4834
4835TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004836 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
4837 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07004838
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004839 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4840 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4841 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4842 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004843
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004844 KeyboardInputMapper& mapper =
4845 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4846 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004847
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004848 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004849 NotifyKeyArgs args;
4850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4851 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4852
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004853 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4855 ASSERT_EQ(uint32_t(0), args.policyFlags);
4856
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4859 ASSERT_EQ(uint32_t(0), args.policyFlags);
4860
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004861 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4863 ASSERT_EQ(uint32_t(0), args.policyFlags);
4864
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4867 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4868
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004869 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4871 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4872}
4873
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004874TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004875 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004876
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004877 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4878 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4879 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004880
Powei Fengd041c5d2019-05-03 17:11:33 -07004881 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004882 KeyboardInputMapper& mapper =
4883 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4884 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004885
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004887 NotifyKeyArgs args;
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4889 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4890
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004891 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4893 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4894
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4897 ASSERT_EQ(uint32_t(0), args.policyFlags);
4898
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004899 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4901 ASSERT_EQ(uint32_t(0), args.policyFlags);
4902
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4905 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4906
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004907 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4909 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4910}
4911
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912// --- CursorInputMapperTest ---
4913
4914class CursorInputMapperTest : public InputMapperTest {
4915protected:
4916 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4917
Michael Wright17db18e2020-06-26 20:51:44 +01004918 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919
Chris Yea52ade12020-08-27 16:49:20 -07004920 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004921 InputMapperTest::SetUp();
4922
Michael Wright17db18e2020-06-26 20:51:44 +01004923 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004924 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004925 }
4926
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004927 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4928 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004929
4930 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004931 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4932 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4933 }
4934
4935 void prepareSecondaryDisplay() {
4936 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4937 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
4938 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004939 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004940
4941 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4942 float pressure) {
4943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4944 0.0f, 0.0f, 0.0f, EPSILON));
4945 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946};
4947
4948const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
4949
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004950void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4951 int32_t originalY, int32_t rotatedX,
4952 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953 NotifyMotionArgs args;
4954
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4959 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004960 ASSERT_NO_FATAL_FAILURE(
4961 assertCursorPointerCoords(args.pointerCoords[0],
4962 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4963 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964}
4965
4966TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004968 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004970 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004971}
4972
4973TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004975 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004977 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978}
4979
4980TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004982 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004983
4984 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004985 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986
4987 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004988 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4989 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4991 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4992
4993 // When the bounds are set, then there should be a valid motion range.
4994 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4995
4996 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998
4999 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
5000 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
5001 1, 800 - 1, 0.0f, 0.0f));
5002 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
5003 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
5004 2, 480 - 1, 0.0f, 0.0f));
5005 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
5006 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
5007 0.0f, 1.0f, 0.0f, 0.0f));
5008}
5009
5010TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005012 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013
5014 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005015 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016
5017 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5018 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
5019 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
5020 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5021 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
5022 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
5023 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
5024 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
5025 0.0f, 1.0f, 0.0f, 0.0f));
5026}
5027
5028TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005030 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005031
arthurhungdcef2dc2020-08-11 14:47:50 +08005032 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005033
5034 NotifyMotionArgs args;
5035
5036 // Button press.
5037 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5041 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5042 ASSERT_EQ(DEVICE_ID, args.deviceId);
5043 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5044 ASSERT_EQ(uint32_t(0), args.policyFlags);
5045 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5046 ASSERT_EQ(0, args.flags);
5047 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5048 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
5049 ASSERT_EQ(0, args.edgeFlags);
5050 ASSERT_EQ(uint32_t(1), args.pointerCount);
5051 ASSERT_EQ(0, args.pointerProperties[0].id);
5052 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005053 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5055 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5056 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5057
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5059 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5060 ASSERT_EQ(DEVICE_ID, args.deviceId);
5061 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5062 ASSERT_EQ(uint32_t(0), args.policyFlags);
5063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5064 ASSERT_EQ(0, args.flags);
5065 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5066 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
5067 ASSERT_EQ(0, args.edgeFlags);
5068 ASSERT_EQ(uint32_t(1), args.pointerCount);
5069 ASSERT_EQ(0, args.pointerProperties[0].id);
5070 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005071 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005072 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5073 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5074 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5075
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005077 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5078 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5080 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
5081 ASSERT_EQ(DEVICE_ID, args.deviceId);
5082 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5083 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5085 ASSERT_EQ(0, args.flags);
5086 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5087 ASSERT_EQ(0, args.buttonState);
5088 ASSERT_EQ(0, args.edgeFlags);
5089 ASSERT_EQ(uint32_t(1), args.pointerCount);
5090 ASSERT_EQ(0, args.pointerProperties[0].id);
5091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005092 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005093 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5094 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5095 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5096
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5098 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
5099 ASSERT_EQ(DEVICE_ID, args.deviceId);
5100 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
5101 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005102 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5103 ASSERT_EQ(0, args.flags);
5104 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5105 ASSERT_EQ(0, args.buttonState);
5106 ASSERT_EQ(0, args.edgeFlags);
5107 ASSERT_EQ(uint32_t(1), args.pointerCount);
5108 ASSERT_EQ(0, args.pointerProperties[0].id);
5109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005110 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
5112 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
5113 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5114}
5115
5116TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005118 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119
5120 NotifyMotionArgs args;
5121
5122 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005127 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5128 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
5129 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130
5131 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
5133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005136 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
5137 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138}
5139
5140TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005142 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005143
5144 NotifyMotionArgs args;
5145
5146 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5150 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005151 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005152
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005155 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005156
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005162 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005163
5164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005165 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005166 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005167}
5168
5169TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005171 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172
5173 NotifyMotionArgs args;
5174
5175 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
5178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5181 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005182 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5183 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5184 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5187 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005188 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5189 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5190 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005191
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
5194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
5195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005198 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
5199 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
5200 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201
5202 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005207 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005208
5209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005210 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005211 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212}
5213
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005214TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005215 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005217 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5218 // need to be rotated.
5219 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005220 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005221
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005222 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005223 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5224 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5225 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5226 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5227 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5228 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5229 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5230 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5231}
5232
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005233TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005234 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005235 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005236 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5237 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005238 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005240 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005241 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005242 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
5243 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
5244 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
5245 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
5246 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
5247 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
5248 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
5249 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
5250
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005251 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005252 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005253 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
5254 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
5255 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
5256 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
5257 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
5258 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
5259 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
5260 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005261
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005262 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005263 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
5265 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
5266 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
5267 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
5268 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
5269 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
5270 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
5271 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
5272
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005273 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005274 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005275 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
5276 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
5277 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
5278 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
5279 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
5280 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
5281 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
5282 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005283}
5284
5285TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005287 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005288
5289 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5290 mFakePointerController->setPosition(100, 200);
5291 mFakePointerController->setButtonState(0);
5292
5293 NotifyMotionArgs motionArgs;
5294 NotifyKeyArgs keyArgs;
5295
5296 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
5298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5300 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5301 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5302 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005303 ASSERT_NO_FATAL_FAILURE(
5304 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5307 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5308 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5309 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005310 ASSERT_NO_FATAL_FAILURE(
5311 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005312
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
5314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005317 ASSERT_EQ(0, motionArgs.buttonState);
5318 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005319 ASSERT_NO_FATAL_FAILURE(
5320 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321
5322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005323 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324 ASSERT_EQ(0, motionArgs.buttonState);
5325 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005326 ASSERT_NO_FATAL_FAILURE(
5327 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005328
5329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005331 ASSERT_EQ(0, motionArgs.buttonState);
5332 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005333 ASSERT_NO_FATAL_FAILURE(
5334 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005335
5336 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
5338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
5339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5341 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5342 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5343 motionArgs.buttonState);
5344 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5345 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005346 ASSERT_NO_FATAL_FAILURE(
5347 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005348
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5350 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5351 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5352 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5353 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005354 ASSERT_NO_FATAL_FAILURE(
5355 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005356
5357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5358 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5359 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5360 motionArgs.buttonState);
5361 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5362 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005363 ASSERT_NO_FATAL_FAILURE(
5364 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005365
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005369 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5371 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005372 ASSERT_NO_FATAL_FAILURE(
5373 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005374
5375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005377 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5378 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005379 ASSERT_NO_FATAL_FAILURE(
5380 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005385 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5386 ASSERT_EQ(0, motionArgs.buttonState);
5387 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005388 ASSERT_NO_FATAL_FAILURE(
5389 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005392
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005394 ASSERT_EQ(0, motionArgs.buttonState);
5395 ASSERT_EQ(0, mFakePointerController->getButtonState());
5396 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005397 ASSERT_NO_FATAL_FAILURE(
5398 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005399
Michael Wrightd02c5b62014-02-10 15:10:22 -08005400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5401 ASSERT_EQ(0, motionArgs.buttonState);
5402 ASSERT_EQ(0, mFakePointerController->getButtonState());
5403 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005404 ASSERT_NO_FATAL_FAILURE(
5405 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005406
5407 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5411 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5412 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005413
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005415 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5417 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005418 ASSERT_NO_FATAL_FAILURE(
5419 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005420
5421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5423 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5424 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005425 ASSERT_NO_FATAL_FAILURE(
5426 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005427
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005431 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005432 ASSERT_EQ(0, motionArgs.buttonState);
5433 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005434 ASSERT_NO_FATAL_FAILURE(
5435 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005436
5437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005439 ASSERT_EQ(0, motionArgs.buttonState);
5440 ASSERT_EQ(0, mFakePointerController->getButtonState());
5441
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005442 ASSERT_NO_FATAL_FAILURE(
5443 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5445 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5446 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5447
5448 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5452 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5453 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005454
Michael Wrightd02c5b62014-02-10 15:10:22 -08005455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005457 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5458 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005459 ASSERT_NO_FATAL_FAILURE(
5460 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005461
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5464 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5465 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005466 ASSERT_NO_FATAL_FAILURE(
5467 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005472 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473 ASSERT_EQ(0, motionArgs.buttonState);
5474 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005475 ASSERT_NO_FATAL_FAILURE(
5476 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005477
5478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5480 ASSERT_EQ(0, motionArgs.buttonState);
5481 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005482 ASSERT_NO_FATAL_FAILURE(
5483 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005484
Michael Wrightd02c5b62014-02-10 15:10:22 -08005485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5486 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5487 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5488
5489 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5493 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5494 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005495
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005497 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5499 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005500 ASSERT_NO_FATAL_FAILURE(
5501 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005502
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5505 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5506 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005507 ASSERT_NO_FATAL_FAILURE(
5508 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005509
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005513 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005514 ASSERT_EQ(0, motionArgs.buttonState);
5515 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005516 ASSERT_NO_FATAL_FAILURE(
5517 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005518
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5520 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5521 ASSERT_EQ(0, motionArgs.buttonState);
5522 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005523 ASSERT_NO_FATAL_FAILURE(
5524 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005525
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5527 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5528 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5529
5530 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5534 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5535 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005536
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5540 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005541 ASSERT_NO_FATAL_FAILURE(
5542 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005543
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5546 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5547 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005548 ASSERT_NO_FATAL_FAILURE(
5549 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005550
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005554 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555 ASSERT_EQ(0, motionArgs.buttonState);
5556 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005557 ASSERT_NO_FATAL_FAILURE(
5558 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005559
5560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5561 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5562 ASSERT_EQ(0, motionArgs.buttonState);
5563 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07005564 ASSERT_NO_FATAL_FAILURE(
5565 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005566
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5568 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5569 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5570}
5571
5572TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005574 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005575
5576 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5577 mFakePointerController->setPosition(100, 200);
5578 mFakePointerController->setButtonState(0);
5579
5580 NotifyMotionArgs args;
5581
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005586 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5587 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5589 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 +01005590 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005591}
5592
5593TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005594 addConfigurationProperty("cursor.mode", "pointer");
5595 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005596 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005597
5598 NotifyDeviceResetArgs resetArgs;
5599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5600 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5601 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5602
5603 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5604 mFakePointerController->setPosition(100, 200);
5605 mFakePointerController->setButtonState(0);
5606
5607 NotifyMotionArgs args;
5608
5609 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5614 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5617 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 +01005618 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005619
5620 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
5622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5624 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5625 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5627 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5629 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
5631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5632 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5633
5634 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005635 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
5636 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5638 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
5640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5641 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5643 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
5645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5646 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5647
5648 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
5650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
5651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5653 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5656 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 +01005657 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005658
5659 // Disable pointer capture and check that the device generation got bumped
5660 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08005661 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005662 mFakePolicy->setPointerCapture(false);
5663 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08005664 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005667 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5668
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5671 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08005672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5673 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5676 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 +01005677 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678}
5679
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00005680/**
5681 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
5682 * pointer acceleration or speed processing should not be applied.
5683 */
5684TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
5685 addConfigurationProperty("cursor.mode", "pointer");
5686 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
5687 100.f /*high threshold*/, 10.f /*acceleration*/);
5688 mFakePolicy->setVelocityControlParams(testParams);
5689 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5690
5691 NotifyDeviceResetArgs resetArgs;
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5693 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5694 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5695
5696 NotifyMotionArgs args;
5697
5698 // Move and verify scale is applied.
5699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5703 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5704 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5705 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5706 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5707 ASSERT_GT(relX, 10);
5708 ASSERT_GT(relY, 20);
5709
5710 // Enable Pointer Capture
5711 mFakePolicy->setPointerCapture(true);
5712 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5713 NotifyPointerCaptureChangedArgs captureArgs;
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5715 ASSERT_TRUE(captureArgs.request.enable);
5716
5717 // Move and verify scale is not applied.
5718 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5722 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5724 ASSERT_EQ(10, args.pointerCoords[0].getX());
5725 ASSERT_EQ(20, args.pointerCoords[0].getY());
5726}
5727
Prabir Pradhan208360b2022-06-24 18:37:04 +00005728TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
5729 addConfigurationProperty("cursor.mode", "pointer");
5730 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5731
5732 NotifyDeviceResetArgs resetArgs;
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5734 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5735 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5736
5737 // Ensure the display is rotated.
5738 prepareDisplay(DISPLAY_ORIENTATION_90);
5739
5740 NotifyMotionArgs args;
5741
5742 // Verify that the coordinates are rotated.
5743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5747 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5749 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
5750 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
5751
5752 // Enable Pointer Capture.
5753 mFakePolicy->setPointerCapture(true);
5754 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
5755 NotifyPointerCaptureChangedArgs captureArgs;
5756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5757 ASSERT_TRUE(captureArgs.request.enable);
5758
5759 // Move and verify rotation is not applied.
5760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5764 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5766 ASSERT_EQ(10, args.pointerCoords[0].getX());
5767 ASSERT_EQ(20, args.pointerCoords[0].getY());
5768}
5769
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005770TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005771 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005772
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005773 // Set up the default display.
5774 prepareDisplay(DISPLAY_ORIENTATION_90);
5775
5776 // Set up the secondary display as the display on which the pointer should be shown.
5777 // The InputDevice is not associated with any display.
5778 prepareSecondaryDisplay();
5779 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08005780 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5781
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005782 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005783 mFakePointerController->setPosition(100, 200);
5784 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005785
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005786 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5788 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005791 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5792 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5793 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01005794 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005795}
5796
5797TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
5798 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5799
5800 // Set up the default display.
5801 prepareDisplay(DISPLAY_ORIENTATION_90);
5802
5803 // Set up the secondary display as the display on which the pointer should be shown,
5804 // and associate the InputDevice with the secondary display.
5805 prepareSecondaryDisplay();
5806 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5807 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5808 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5809
5810 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5811 mFakePointerController->setPosition(100, 200);
5812 mFakePointerController->setButtonState(0);
5813
5814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5815 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00005818 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5819 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5820 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00005821 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
5822}
5823
5824TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
5825 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5826
5827 // Set up the default display as the display on which the pointer should be shown.
5828 prepareDisplay(DISPLAY_ORIENTATION_90);
5829 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5830
5831 // Associate the InputDevice with the secondary display.
5832 prepareSecondaryDisplay();
5833 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5834 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5835
5836 // The mapper should not generate any events because it is associated with a display that is
5837 // different from the pointer display.
5838 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005842}
5843
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005844// --- BluetoothCursorInputMapperTest ---
5845
5846class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
5847protected:
5848 void SetUp() override {
5849 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
5850
5851 mFakePointerController = std::make_shared<FakePointerController>();
5852 mFakePolicy->setPointerController(mFakePointerController);
5853 }
5854};
5855
5856TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
5857 addConfigurationProperty("cursor.mode", "pointer");
5858 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5859
5860 nsecs_t kernelEventTime = ARBITRARY_TIME;
5861 nsecs_t expectedEventTime = ARBITRARY_TIME;
5862 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5863 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5865 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5866 WithEventTime(expectedEventTime))));
5867
5868 // Process several events that come in quick succession, according to their timestamps.
5869 for (int i = 0; i < 3; i++) {
5870 constexpr static nsecs_t delta = ms2ns(1);
5871 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5872 kernelEventTime += delta;
5873 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5874
5875 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5876 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5878 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5879 WithEventTime(expectedEventTime))));
5880 }
5881}
5882
5883TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
5884 addConfigurationProperty("cursor.mode", "pointer");
5885 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5886
5887 nsecs_t expectedEventTime = ARBITRARY_TIME;
5888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5891 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5892 WithEventTime(expectedEventTime))));
5893
5894 // Process several events with the same timestamp from the kernel.
5895 // Ensure that we do not generate events too far into the future.
5896 constexpr static int32_t numEvents =
5897 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5898 for (int i = 0; i < numEvents; i++) {
5899 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5900
5901 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5904 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5905 WithEventTime(expectedEventTime))));
5906 }
5907
5908 // By processing more events with the same timestamp, we should not generate events with a
5909 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5910 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5911 for (int i = 0; i < 3; i++) {
5912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5913 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5915 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5916 WithEventTime(cappedEventTime))));
5917 }
5918}
5919
5920TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
5921 addConfigurationProperty("cursor.mode", "pointer");
5922 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
5923
5924 nsecs_t kernelEventTime = ARBITRARY_TIME;
5925 nsecs_t expectedEventTime = ARBITRARY_TIME;
5926 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5927 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5929 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5930 WithEventTime(expectedEventTime))));
5931
5932 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5933 // smoothening is not needed, its timestamp is not affected.
5934 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5935 expectedEventTime = kernelEventTime;
5936
5937 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5938 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5940 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5941 WithEventTime(expectedEventTime))));
5942}
5943
Michael Wrightd02c5b62014-02-10 15:10:22 -08005944// --- TouchInputMapperTest ---
5945
5946class TouchInputMapperTest : public InputMapperTest {
5947protected:
5948 static const int32_t RAW_X_MIN;
5949 static const int32_t RAW_X_MAX;
5950 static const int32_t RAW_Y_MIN;
5951 static const int32_t RAW_Y_MAX;
5952 static const int32_t RAW_TOUCH_MIN;
5953 static const int32_t RAW_TOUCH_MAX;
5954 static const int32_t RAW_TOOL_MIN;
5955 static const int32_t RAW_TOOL_MAX;
5956 static const int32_t RAW_PRESSURE_MIN;
5957 static const int32_t RAW_PRESSURE_MAX;
5958 static const int32_t RAW_ORIENTATION_MIN;
5959 static const int32_t RAW_ORIENTATION_MAX;
5960 static const int32_t RAW_DISTANCE_MIN;
5961 static const int32_t RAW_DISTANCE_MAX;
5962 static const int32_t RAW_TILT_MIN;
5963 static const int32_t RAW_TILT_MAX;
5964 static const int32_t RAW_ID_MIN;
5965 static const int32_t RAW_ID_MAX;
5966 static const int32_t RAW_SLOT_MIN;
5967 static const int32_t RAW_SLOT_MAX;
5968 static const float X_PRECISION;
5969 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005970 static const float X_PRECISION_VIRTUAL;
5971 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972
5973 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005974 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005975
5976 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5977
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005978 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005979 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005980
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981 enum Axes {
5982 POSITION = 1 << 0,
5983 TOUCH = 1 << 1,
5984 TOOL = 1 << 2,
5985 PRESSURE = 1 << 3,
5986 ORIENTATION = 1 << 4,
5987 MINOR = 1 << 5,
5988 ID = 1 << 6,
5989 DISTANCE = 1 << 7,
5990 TILT = 1 << 8,
5991 SLOT = 1 << 9,
5992 TOOL_TYPE = 1 << 10,
5993 };
5994
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005995 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5996 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005997 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005999 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006000 int32_t toRawX(float displayX);
6001 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006002 int32_t toRotatedRawX(float displayX);
6003 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07006004 float toCookedX(float rawX, float rawY);
6005 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006006 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006007 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006008 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006009 float toDisplayY(int32_t rawY, int32_t displayHeight);
6010
Michael Wrightd02c5b62014-02-10 15:10:22 -08006011};
6012
6013const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
6014const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
6015const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
6016const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
6017const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
6018const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
6019const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
6020const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00006021const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
6022const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
6024const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
6025const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
6026const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
6027const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
6028const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
6029const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
6030const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
6031const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
6032const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
6033const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
6034const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07006035const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
6036 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
6037const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
6038 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07006039const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
6040 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041
6042const float TouchInputMapperTest::GEOMETRIC_SCALE =
6043 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
6044 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
6045
6046const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
6047 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
6048 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
6049};
6050
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006051void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006052 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
6053 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07006054}
6055
6056void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
6057 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
6058 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059}
6060
Santos Cordonfa5cf462017-04-05 10:37:00 -07006061void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01006062 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
6063 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
6064 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006065}
6066
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006068 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
6069 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
6070 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
6071 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006072}
6073
Jason Gerecke489fda82012-09-07 17:19:40 -07006074void TouchInputMapperTest::prepareLocationCalibration() {
6075 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
6076}
6077
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078int32_t TouchInputMapperTest::toRawX(float displayX) {
6079 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
6080}
6081
6082int32_t TouchInputMapperTest::toRawY(float displayY) {
6083 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
6084}
6085
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006086int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
6087 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
6088}
6089
6090int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
6091 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
6092}
6093
Jason Gerecke489fda82012-09-07 17:19:40 -07006094float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
6095 AFFINE_TRANSFORM.applyTo(rawX, rawY);
6096 return rawX;
6097}
6098
6099float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
6100 AFFINE_TRANSFORM.applyTo(rawX, rawY);
6101 return rawY;
6102}
6103
Michael Wrightd02c5b62014-02-10 15:10:22 -08006104float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006105 return toDisplayX(rawX, DISPLAY_WIDTH);
6106}
6107
6108float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
6109 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006110}
6111
6112float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006113 return toDisplayY(rawY, DISPLAY_HEIGHT);
6114}
6115
6116float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
6117 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118}
6119
6120
6121// --- SingleTouchInputMapperTest ---
6122
6123class SingleTouchInputMapperTest : public TouchInputMapperTest {
6124protected:
6125 void prepareButtons();
6126 void prepareAxes(int axes);
6127
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006128 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
6129 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
6130 void processUp(SingleTouchInputMapper& mappery);
6131 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
6132 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
6133 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
6134 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
6135 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
6136 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006137};
6138
6139void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006140 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141}
6142
6143void SingleTouchInputMapperTest::prepareAxes(int axes) {
6144 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006145 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006147 }
6148 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006149 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
6150 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006151 }
6152 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006153 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
6154 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155 }
6156 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006157 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
6158 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159 }
6160 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006161 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
6162 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006163 }
6164}
6165
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006166void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
6169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170}
6171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006172void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
6174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175}
6176
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006177void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179}
6180
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006181void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006183}
6184
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006185void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
6186 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006188}
6189
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006190void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192}
6193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006194void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
6195 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
6197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006198}
6199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006200void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
6201 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006203}
6204
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006205void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207}
6208
Michael Wrightd02c5b62014-02-10 15:10:22 -08006209TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 prepareButtons();
6211 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006212 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213
Harry Cutts16a24cc2022-10-26 15:22:19 +00006214 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006215}
6216
Michael Wrightd02c5b62014-02-10 15:10:22 -08006217TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218 prepareButtons();
6219 prepareAxes(POSITION);
6220 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006221 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006222
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006223 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224}
6225
6226TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006227 addConfigurationProperty("touch.deviceType", "touchScreen");
6228 prepareDisplay(DISPLAY_ORIENTATION_0);
6229 prepareButtons();
6230 prepareAxes(POSITION);
6231 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006232 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006233
6234 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006235 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236
6237 // Virtual key is down.
6238 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6239 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6240 processDown(mapper, x, y);
6241 processSync(mapper);
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6243
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006244 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006245
6246 // Virtual key is up.
6247 processUp(mapper);
6248 processSync(mapper);
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6250
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006251 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252}
6253
6254TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006255 addConfigurationProperty("touch.deviceType", "touchScreen");
6256 prepareDisplay(DISPLAY_ORIENTATION_0);
6257 prepareButtons();
6258 prepareAxes(POSITION);
6259 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006260 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006261
6262 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006263 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264
6265 // Virtual key is down.
6266 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6267 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6268 processDown(mapper, x, y);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6271
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006272 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006273
6274 // Virtual key is up.
6275 processUp(mapper);
6276 processSync(mapper);
6277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6278
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006279 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280}
6281
6282TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 addConfigurationProperty("touch.deviceType", "touchScreen");
6284 prepareDisplay(DISPLAY_ORIENTATION_0);
6285 prepareButtons();
6286 prepareAxes(POSITION);
6287 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006288 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289
Michael Wrightd02c5b62014-02-10 15:10:22 -08006290 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07006291 ASSERT_TRUE(
6292 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293 ASSERT_TRUE(flags[0]);
6294 ASSERT_FALSE(flags[1]);
6295}
6296
6297TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298 addConfigurationProperty("touch.deviceType", "touchScreen");
6299 prepareDisplay(DISPLAY_ORIENTATION_0);
6300 prepareButtons();
6301 prepareAxes(POSITION);
6302 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006303 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006304
arthurhungdcef2dc2020-08-11 14:47:50 +08006305 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306
6307 NotifyKeyArgs args;
6308
6309 // Press virtual key.
6310 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6311 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6312 processDown(mapper, x, y);
6313 processSync(mapper);
6314
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6316 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6317 ASSERT_EQ(DEVICE_ID, args.deviceId);
6318 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6319 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6320 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
6321 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6322 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6323 ASSERT_EQ(KEY_HOME, args.scanCode);
6324 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6325 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6326
6327 // Release virtual key.
6328 processUp(mapper);
6329 processSync(mapper);
6330
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6332 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6333 ASSERT_EQ(DEVICE_ID, args.deviceId);
6334 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6335 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6336 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
6337 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6338 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6339 ASSERT_EQ(KEY_HOME, args.scanCode);
6340 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6341 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6342
6343 // Should not have sent any motions.
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6345}
6346
6347TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006348 addConfigurationProperty("touch.deviceType", "touchScreen");
6349 prepareDisplay(DISPLAY_ORIENTATION_0);
6350 prepareButtons();
6351 prepareAxes(POSITION);
6352 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006353 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006354
arthurhungdcef2dc2020-08-11 14:47:50 +08006355 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006356
6357 NotifyKeyArgs keyArgs;
6358
6359 // Press virtual key.
6360 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6361 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6362 processDown(mapper, x, y);
6363 processSync(mapper);
6364
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6366 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6367 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6368 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6369 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6370 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6371 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
6372 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6373 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6374 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6375 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6376
6377 // Move out of bounds. This should generate a cancel and a pointer down since we moved
6378 // into the display area.
6379 y -= 100;
6380 processMove(mapper, x, y);
6381 processSync(mapper);
6382
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6384 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6385 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6386 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6387 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6388 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6389 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
6390 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
6391 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6392 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6393 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6394 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6395
6396 NotifyMotionArgs motionArgs;
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6399 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6400 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6401 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6403 ASSERT_EQ(0, motionArgs.flags);
6404 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6405 ASSERT_EQ(0, motionArgs.buttonState);
6406 ASSERT_EQ(0, motionArgs.edgeFlags);
6407 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6408 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6411 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6412 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6413 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6415
6416 // Keep moving out of bounds. Should generate a pointer move.
6417 y -= 50;
6418 processMove(mapper, x, y);
6419 processSync(mapper);
6420
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6423 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6424 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6425 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6427 ASSERT_EQ(0, motionArgs.flags);
6428 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6429 ASSERT_EQ(0, motionArgs.buttonState);
6430 ASSERT_EQ(0, motionArgs.edgeFlags);
6431 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6432 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6434 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6435 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6436 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6437 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6438 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6439
6440 // Release out of bounds. Should generate a pointer up.
6441 processUp(mapper);
6442 processSync(mapper);
6443
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6446 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6447 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6448 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6450 ASSERT_EQ(0, motionArgs.flags);
6451 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6452 ASSERT_EQ(0, motionArgs.buttonState);
6453 ASSERT_EQ(0, motionArgs.edgeFlags);
6454 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6455 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6456 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6458 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6459 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6460 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6461 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6462
6463 // Should not have sent any more keys or motions.
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6466}
6467
6468TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006469 addConfigurationProperty("touch.deviceType", "touchScreen");
6470 prepareDisplay(DISPLAY_ORIENTATION_0);
6471 prepareButtons();
6472 prepareAxes(POSITION);
6473 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006474 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006475
arthurhungdcef2dc2020-08-11 14:47:50 +08006476 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006477
6478 NotifyMotionArgs motionArgs;
6479
6480 // Initially go down out of bounds.
6481 int32_t x = -10;
6482 int32_t y = -10;
6483 processDown(mapper, x, y);
6484 processSync(mapper);
6485
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6487
6488 // Move into the display area. Should generate a pointer down.
6489 x = 50;
6490 y = 75;
6491 processMove(mapper, x, y);
6492 processSync(mapper);
6493
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6495 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6496 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6497 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6498 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6499 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6500 ASSERT_EQ(0, motionArgs.flags);
6501 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6502 ASSERT_EQ(0, motionArgs.buttonState);
6503 ASSERT_EQ(0, motionArgs.edgeFlags);
6504 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6505 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6508 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6509 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6510 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6511 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6512
6513 // Release. Should generate a pointer up.
6514 processUp(mapper);
6515 processSync(mapper);
6516
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6519 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6520 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6521 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6522 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6523 ASSERT_EQ(0, motionArgs.flags);
6524 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6525 ASSERT_EQ(0, motionArgs.buttonState);
6526 ASSERT_EQ(0, motionArgs.edgeFlags);
6527 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6528 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6531 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6532 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6533 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6534 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6535
6536 // Should not have sent any more keys or motions.
6537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6539}
6540
Santos Cordonfa5cf462017-04-05 10:37:00 -07006541TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006542 addConfigurationProperty("touch.deviceType", "touchScreen");
6543 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6544
6545 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
6546 prepareButtons();
6547 prepareAxes(POSITION);
6548 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006549 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006550
arthurhungdcef2dc2020-08-11 14:47:50 +08006551 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006552
6553 NotifyMotionArgs motionArgs;
6554
6555 // Down.
6556 int32_t x = 100;
6557 int32_t y = 125;
6558 processDown(mapper, x, y);
6559 processSync(mapper);
6560
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6563 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6564 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6565 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6566 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6567 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6568 ASSERT_EQ(0, motionArgs.flags);
6569 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6570 ASSERT_EQ(0, motionArgs.buttonState);
6571 ASSERT_EQ(0, motionArgs.edgeFlags);
6572 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6573 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6574 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6576 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6577 1, 0, 0, 0, 0, 0, 0, 0));
6578 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6579 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6580 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6581
6582 // Move.
6583 x += 50;
6584 y += 75;
6585 processMove(mapper, x, y);
6586 processSync(mapper);
6587
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6589 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6590 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6591 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6592 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6593 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6595 ASSERT_EQ(0, motionArgs.flags);
6596 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6597 ASSERT_EQ(0, motionArgs.buttonState);
6598 ASSERT_EQ(0, motionArgs.edgeFlags);
6599 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6600 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6603 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6604 1, 0, 0, 0, 0, 0, 0, 0));
6605 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6606 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6607 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6608
6609 // Up.
6610 processUp(mapper);
6611 processSync(mapper);
6612
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6614 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6615 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6616 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6617 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6618 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6619 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6620 ASSERT_EQ(0, motionArgs.flags);
6621 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6622 ASSERT_EQ(0, motionArgs.buttonState);
6623 ASSERT_EQ(0, motionArgs.edgeFlags);
6624 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6625 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6628 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6629 1, 0, 0, 0, 0, 0, 0, 0));
6630 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6631 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6632 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6633
6634 // Should not have sent any more keys or motions.
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6637}
6638
Michael Wrightd02c5b62014-02-10 15:10:22 -08006639TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006640 addConfigurationProperty("touch.deviceType", "touchScreen");
6641 prepareDisplay(DISPLAY_ORIENTATION_0);
6642 prepareButtons();
6643 prepareAxes(POSITION);
6644 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006645 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006646
arthurhungdcef2dc2020-08-11 14:47:50 +08006647 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006648
6649 NotifyMotionArgs motionArgs;
6650
6651 // Down.
6652 int32_t x = 100;
6653 int32_t y = 125;
6654 processDown(mapper, x, y);
6655 processSync(mapper);
6656
6657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6659 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6660 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6661 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6663 ASSERT_EQ(0, motionArgs.flags);
6664 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6665 ASSERT_EQ(0, motionArgs.buttonState);
6666 ASSERT_EQ(0, motionArgs.edgeFlags);
6667 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6668 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6669 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6671 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6672 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6673 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6674 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6675
6676 // Move.
6677 x += 50;
6678 y += 75;
6679 processMove(mapper, x, y);
6680 processSync(mapper);
6681
6682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6683 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6684 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6685 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6686 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6687 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6688 ASSERT_EQ(0, motionArgs.flags);
6689 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6690 ASSERT_EQ(0, motionArgs.buttonState);
6691 ASSERT_EQ(0, motionArgs.edgeFlags);
6692 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6693 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6696 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6697 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6698 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6699 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6700
6701 // Up.
6702 processUp(mapper);
6703 processSync(mapper);
6704
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6706 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6707 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6708 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6709 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6710 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6711 ASSERT_EQ(0, motionArgs.flags);
6712 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6713 ASSERT_EQ(0, motionArgs.buttonState);
6714 ASSERT_EQ(0, motionArgs.edgeFlags);
6715 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6716 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6719 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6720 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6721 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6722 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6723
6724 // Should not have sent any more keys or motions.
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6727}
6728
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006729TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006730 addConfigurationProperty("touch.deviceType", "touchScreen");
6731 prepareButtons();
6732 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006733 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6734 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006735 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006736
6737 NotifyMotionArgs args;
6738
6739 // Rotation 90.
6740 prepareDisplay(DISPLAY_ORIENTATION_90);
6741 processDown(mapper, toRawX(50), toRawY(75));
6742 processSync(mapper);
6743
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6745 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6746 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6747
6748 processUp(mapper);
6749 processSync(mapper);
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6751}
6752
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006753TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006754 addConfigurationProperty("touch.deviceType", "touchScreen");
6755 prepareButtons();
6756 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006757 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6758 // orientation-aware are affected by display rotation.
6759 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006760 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006761
6762 NotifyMotionArgs args;
6763
6764 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006765 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006766 prepareDisplay(DISPLAY_ORIENTATION_0);
6767 processDown(mapper, toRawX(50), toRawY(75));
6768 processSync(mapper);
6769
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6771 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6772 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6773
6774 processUp(mapper);
6775 processSync(mapper);
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6777
6778 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006779 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006780 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006781 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006782 processSync(mapper);
6783
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6785 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6786 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6787
6788 processUp(mapper);
6789 processSync(mapper);
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6791
6792 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006793 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006794 prepareDisplay(DISPLAY_ORIENTATION_180);
6795 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6796 processSync(mapper);
6797
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6799 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6800 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6801
6802 processUp(mapper);
6803 processSync(mapper);
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6805
6806 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006807 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006808 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006809 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006810 processSync(mapper);
6811
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6813 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6814 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6815
6816 processUp(mapper);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6819}
6820
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006821TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6822 addConfigurationProperty("touch.deviceType", "touchScreen");
6823 prepareButtons();
6824 prepareAxes(POSITION);
6825 addConfigurationProperty("touch.orientationAware", "1");
6826 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6827 clearViewports();
6828 prepareDisplay(DISPLAY_ORIENTATION_0);
6829 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6830 NotifyMotionArgs args;
6831
6832 // Orientation 0.
6833 processDown(mapper, toRawX(50), toRawY(75));
6834 processSync(mapper);
6835
6836 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6837 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6838 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6839
6840 processUp(mapper);
6841 processSync(mapper);
6842 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6843}
6844
6845TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6846 addConfigurationProperty("touch.deviceType", "touchScreen");
6847 prepareButtons();
6848 prepareAxes(POSITION);
6849 addConfigurationProperty("touch.orientationAware", "1");
6850 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6851 clearViewports();
6852 prepareDisplay(DISPLAY_ORIENTATION_0);
6853 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6854 NotifyMotionArgs args;
6855
6856 // Orientation 90.
6857 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6858 processSync(mapper);
6859
6860 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6861 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6862 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6863
6864 processUp(mapper);
6865 processSync(mapper);
6866 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6867}
6868
6869TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6870 addConfigurationProperty("touch.deviceType", "touchScreen");
6871 prepareButtons();
6872 prepareAxes(POSITION);
6873 addConfigurationProperty("touch.orientationAware", "1");
6874 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6875 clearViewports();
6876 prepareDisplay(DISPLAY_ORIENTATION_0);
6877 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6878 NotifyMotionArgs args;
6879
6880 // Orientation 180.
6881 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6882 processSync(mapper);
6883
6884 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6885 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6886 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6887
6888 processUp(mapper);
6889 processSync(mapper);
6890 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6891}
6892
6893TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6894 addConfigurationProperty("touch.deviceType", "touchScreen");
6895 prepareButtons();
6896 prepareAxes(POSITION);
6897 addConfigurationProperty("touch.orientationAware", "1");
6898 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6899 clearViewports();
6900 prepareDisplay(DISPLAY_ORIENTATION_0);
6901 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6902 NotifyMotionArgs args;
6903
6904 // Orientation 270.
6905 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6906 processSync(mapper);
6907
6908 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6909 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6910 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6911
6912 processUp(mapper);
6913 processSync(mapper);
6914 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6915}
6916
6917TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6918 addConfigurationProperty("touch.deviceType", "touchScreen");
6919 prepareButtons();
6920 prepareAxes(POSITION);
6921 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6922 // orientation-aware are affected by display rotation.
6923 addConfigurationProperty("touch.orientationAware", "0");
6924 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6925 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6926
6927 NotifyMotionArgs args;
6928
6929 // Orientation 90, Rotation 0.
6930 clearViewports();
6931 prepareDisplay(DISPLAY_ORIENTATION_0);
6932 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6933 processSync(mapper);
6934
6935 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6936 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6937 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6938
6939 processUp(mapper);
6940 processSync(mapper);
6941 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6942
6943 // Orientation 90, Rotation 90.
6944 clearViewports();
6945 prepareDisplay(DISPLAY_ORIENTATION_90);
6946 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
6947 processSync(mapper);
6948
6949 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6950 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6951 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6952
6953 processUp(mapper);
6954 processSync(mapper);
6955 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6956
6957 // Orientation 90, Rotation 180.
6958 clearViewports();
6959 prepareDisplay(DISPLAY_ORIENTATION_180);
6960 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6961 processSync(mapper);
6962
6963 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6964 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6965 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6966
6967 processUp(mapper);
6968 processSync(mapper);
6969 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6970
6971 // Orientation 90, Rotation 270.
6972 clearViewports();
6973 prepareDisplay(DISPLAY_ORIENTATION_270);
6974 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6975 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6976 processSync(mapper);
6977
6978 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6979 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6980 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6981
6982 processUp(mapper);
6983 processSync(mapper);
6984 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6985}
6986
Michael Wrightd02c5b62014-02-10 15:10:22 -08006987TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006988 addConfigurationProperty("touch.deviceType", "touchScreen");
6989 prepareDisplay(DISPLAY_ORIENTATION_0);
6990 prepareButtons();
6991 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006992 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006993
6994 // These calculations are based on the input device calibration documentation.
6995 int32_t rawX = 100;
6996 int32_t rawY = 200;
6997 int32_t rawPressure = 10;
6998 int32_t rawToolMajor = 12;
6999 int32_t rawDistance = 2;
7000 int32_t rawTiltX = 30;
7001 int32_t rawTiltY = 110;
7002
7003 float x = toDisplayX(rawX);
7004 float y = toDisplayY(rawY);
7005 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7006 float size = float(rawToolMajor) / RAW_TOOL_MAX;
7007 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
7008 float distance = float(rawDistance);
7009
7010 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
7011 float tiltScale = M_PI / 180;
7012 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
7013 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
7014 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
7015 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
7016
7017 processDown(mapper, rawX, rawY);
7018 processPressure(mapper, rawPressure);
7019 processToolMajor(mapper, rawToolMajor);
7020 processDistance(mapper, rawDistance);
7021 processTilt(mapper, rawTiltX, rawTiltY);
7022 processSync(mapper);
7023
7024 NotifyMotionArgs args;
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7026 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7027 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
7028 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
7029}
7030
Jason Gerecke489fda82012-09-07 17:19:40 -07007031TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07007032 addConfigurationProperty("touch.deviceType", "touchScreen");
7033 prepareDisplay(DISPLAY_ORIENTATION_0);
7034 prepareLocationCalibration();
7035 prepareButtons();
7036 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007037 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07007038
7039 int32_t rawX = 100;
7040 int32_t rawY = 200;
7041
7042 float x = toDisplayX(toCookedX(rawX, rawY));
7043 float y = toDisplayY(toCookedY(rawX, rawY));
7044
7045 processDown(mapper, rawX, rawY);
7046 processSync(mapper);
7047
7048 NotifyMotionArgs args;
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7051 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
7052}
7053
Michael Wrightd02c5b62014-02-10 15:10:22 -08007054TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055 addConfigurationProperty("touch.deviceType", "touchScreen");
7056 prepareDisplay(DISPLAY_ORIENTATION_0);
7057 prepareButtons();
7058 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007059 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007060
7061 NotifyMotionArgs motionArgs;
7062 NotifyKeyArgs keyArgs;
7063
7064 processDown(mapper, 100, 200);
7065 processSync(mapper);
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7067 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7068 ASSERT_EQ(0, motionArgs.buttonState);
7069
7070 // press BTN_LEFT, release BTN_LEFT
7071 processKey(mapper, BTN_LEFT, 1);
7072 processSync(mapper);
7073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7075 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7076
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7078 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7079 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7080
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081 processKey(mapper, BTN_LEFT, 0);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007086
7087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007089 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090
7091 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7092 processKey(mapper, BTN_RIGHT, 1);
7093 processKey(mapper, BTN_MIDDLE, 1);
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7097 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7098 motionArgs.buttonState);
7099
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7101 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7102 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7103
7104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7106 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7107 motionArgs.buttonState);
7108
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109 processKey(mapper, BTN_RIGHT, 0);
7110 processSync(mapper);
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007112 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007114
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007117 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007118
7119 processKey(mapper, BTN_MIDDLE, 0);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007124
7125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007127 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128
7129 // press BTN_BACK, release BTN_BACK
7130 processKey(mapper, BTN_BACK, 1);
7131 processSync(mapper);
7132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7133 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7134 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007135
Michael Wrightd02c5b62014-02-10 15:10:22 -08007136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007138 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7139
7140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7142 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143
7144 processKey(mapper, BTN_BACK, 0);
7145 processSync(mapper);
7146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007147 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007148 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007149
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007152 ASSERT_EQ(0, motionArgs.buttonState);
7153
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7155 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7156 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7157
7158 // press BTN_SIDE, release BTN_SIDE
7159 processKey(mapper, BTN_SIDE, 1);
7160 processSync(mapper);
7161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7162 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7163 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007164
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007167 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7168
7169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7170 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7171 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172
7173 processKey(mapper, BTN_SIDE, 0);
7174 processSync(mapper);
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007176 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007178
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007181 ASSERT_EQ(0, motionArgs.buttonState);
7182
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7184 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7185 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7186
7187 // press BTN_FORWARD, release BTN_FORWARD
7188 processKey(mapper, BTN_FORWARD, 1);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7191 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7192 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007193
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007196 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7197
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7200 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007201
7202 processKey(mapper, BTN_FORWARD, 0);
7203 processSync(mapper);
7204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007205 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007207
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007210 ASSERT_EQ(0, motionArgs.buttonState);
7211
Michael Wrightd02c5b62014-02-10 15:10:22 -08007212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7213 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7214 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7215
7216 // press BTN_EXTRA, release BTN_EXTRA
7217 processKey(mapper, BTN_EXTRA, 1);
7218 processSync(mapper);
7219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7220 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7221 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007222
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007224 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007225 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7226
7227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7228 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7229 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230
7231 processKey(mapper, BTN_EXTRA, 0);
7232 processSync(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007234 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007235 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007236
7237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007239 ASSERT_EQ(0, motionArgs.buttonState);
7240
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7242 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7243 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7244
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7246
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247 // press BTN_STYLUS, release BTN_STYLUS
7248 processKey(mapper, BTN_STYLUS, 1);
7249 processSync(mapper);
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007252 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7253
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7256 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007257
7258 processKey(mapper, BTN_STYLUS, 0);
7259 processSync(mapper);
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007262 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007263
7264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007266 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007267
7268 // press BTN_STYLUS2, release BTN_STYLUS2
7269 processKey(mapper, BTN_STYLUS2, 1);
7270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007273 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7274
7275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7276 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7277 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007278
7279 processKey(mapper, BTN_STYLUS2, 0);
7280 processSync(mapper);
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007282 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007283 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007284
7285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007286 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007287 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007288
7289 // release touch
7290 processUp(mapper);
7291 processSync(mapper);
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7294 ASSERT_EQ(0, motionArgs.buttonState);
7295}
7296
7297TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007298 addConfigurationProperty("touch.deviceType", "touchScreen");
7299 prepareDisplay(DISPLAY_ORIENTATION_0);
7300 prepareButtons();
7301 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007302 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007303
7304 NotifyMotionArgs motionArgs;
7305
7306 // default tool type is finger
7307 processDown(mapper, 100, 200);
7308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7310 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7312
7313 // eraser
7314 processKey(mapper, BTN_TOOL_RUBBER, 1);
7315 processSync(mapper);
7316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7318 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7319
7320 // stylus
7321 processKey(mapper, BTN_TOOL_RUBBER, 0);
7322 processKey(mapper, BTN_TOOL_PEN, 1);
7323 processSync(mapper);
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7327
7328 // brush
7329 processKey(mapper, BTN_TOOL_PEN, 0);
7330 processKey(mapper, BTN_TOOL_BRUSH, 1);
7331 processSync(mapper);
7332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7335
7336 // pencil
7337 processKey(mapper, BTN_TOOL_BRUSH, 0);
7338 processKey(mapper, BTN_TOOL_PENCIL, 1);
7339 processSync(mapper);
7340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7343
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007344 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007345 processKey(mapper, BTN_TOOL_PENCIL, 0);
7346 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7347 processSync(mapper);
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7351
7352 // mouse
7353 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7354 processKey(mapper, BTN_TOOL_MOUSE, 1);
7355 processSync(mapper);
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7359
7360 // lens
7361 processKey(mapper, BTN_TOOL_MOUSE, 0);
7362 processKey(mapper, BTN_TOOL_LENS, 1);
7363 processSync(mapper);
7364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7366 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7367
7368 // double-tap
7369 processKey(mapper, BTN_TOOL_LENS, 0);
7370 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7371 processSync(mapper);
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7374 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7375
7376 // triple-tap
7377 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7378 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7379 processSync(mapper);
7380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7382 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7383
7384 // quad-tap
7385 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7386 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7387 processSync(mapper);
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7391
7392 // finger
7393 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7394 processKey(mapper, BTN_TOOL_FINGER, 1);
7395 processSync(mapper);
7396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7399
7400 // stylus trumps finger
7401 processKey(mapper, BTN_TOOL_PEN, 1);
7402 processSync(mapper);
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7405 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
7406
7407 // eraser trumps stylus
7408 processKey(mapper, BTN_TOOL_RUBBER, 1);
7409 processSync(mapper);
7410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7412 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
7413
7414 // mouse trumps eraser
7415 processKey(mapper, BTN_TOOL_MOUSE, 1);
7416 processSync(mapper);
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
7420
7421 // back to default tool type
7422 processKey(mapper, BTN_TOOL_MOUSE, 0);
7423 processKey(mapper, BTN_TOOL_RUBBER, 0);
7424 processKey(mapper, BTN_TOOL_PEN, 0);
7425 processKey(mapper, BTN_TOOL_FINGER, 0);
7426 processSync(mapper);
7427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7430}
7431
7432TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007433 addConfigurationProperty("touch.deviceType", "touchScreen");
7434 prepareDisplay(DISPLAY_ORIENTATION_0);
7435 prepareButtons();
7436 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007437 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007438 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439
7440 NotifyMotionArgs motionArgs;
7441
7442 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7443 processKey(mapper, BTN_TOOL_FINGER, 1);
7444 processMove(mapper, 100, 200);
7445 processSync(mapper);
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7447 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7449 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7450
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7454 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7455
7456 // move a little
7457 processMove(mapper, 150, 250);
7458 processSync(mapper);
7459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7460 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7462 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7463
7464 // down when BTN_TOUCH is pressed, pressure defaults to 1
7465 processKey(mapper, BTN_TOUCH, 1);
7466 processSync(mapper);
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7470 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7471
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7475 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7476
7477 // up when BTN_TOUCH is released, hover restored
7478 processKey(mapper, BTN_TOUCH, 0);
7479 processSync(mapper);
7480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7481 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7483 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7484
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7488 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7489
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7493 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7494
7495 // exit hover when pointer goes away
7496 processKey(mapper, BTN_TOOL_FINGER, 0);
7497 processSync(mapper);
7498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7501 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7502}
7503
7504TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007505 addConfigurationProperty("touch.deviceType", "touchScreen");
7506 prepareDisplay(DISPLAY_ORIENTATION_0);
7507 prepareButtons();
7508 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007509 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007510
7511 NotifyMotionArgs motionArgs;
7512
7513 // initially hovering because pressure is 0
7514 processDown(mapper, 100, 200);
7515 processPressure(mapper, 0);
7516 processSync(mapper);
7517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7518 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7520 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7521
7522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7525 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7526
7527 // move a little
7528 processMove(mapper, 150, 250);
7529 processSync(mapper);
7530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7531 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7533 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7534
7535 // down when pressure is non-zero
7536 processPressure(mapper, RAW_PRESSURE_MAX);
7537 processSync(mapper);
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7539 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7540 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7541 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7542
7543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7544 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7546 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7547
7548 // up when pressure becomes 0, hover restored
7549 processPressure(mapper, 0);
7550 processSync(mapper);
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7552 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7554 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7555
7556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7559 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7560
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7562 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7564 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7565
7566 // exit hover when pointer goes away
7567 processUp(mapper);
7568 processSync(mapper);
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7572 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7573}
7574
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007575TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7576 addConfigurationProperty("touch.deviceType", "touchScreen");
7577 prepareDisplay(DISPLAY_ORIENTATION_0);
7578 prepareButtons();
7579 prepareAxes(POSITION | PRESSURE);
7580 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7581
7582 // Touch down.
7583 processDown(mapper, 100, 200);
7584 processPressure(mapper, 1);
7585 processSync(mapper);
7586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7587 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7588
7589 // Reset the mapper. This should cancel the ongoing gesture.
7590 resetMapper(mapper, ARBITRARY_TIME);
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7592 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7593
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7595}
7596
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007597TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7598 addConfigurationProperty("touch.deviceType", "touchScreen");
7599 prepareDisplay(DISPLAY_ORIENTATION_0);
7600 prepareButtons();
7601 prepareAxes(POSITION | PRESSURE);
7602 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7603
7604 // Set the initial state for the touch pointer.
7605 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7606 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7607 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7608 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7609
7610 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007611 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7612 // does not generate any events.
7613 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007614
7615 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7616 // the recreated touch state to generate a down event.
7617 processSync(mapper);
7618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7619 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7620
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7622}
7623
lilinnan687e58f2022-07-19 16:00:50 +08007624TEST_F(SingleTouchInputMapperTest,
7625 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7626 addConfigurationProperty("touch.deviceType", "touchScreen");
7627 prepareDisplay(DISPLAY_ORIENTATION_0);
7628 prepareButtons();
7629 prepareAxes(POSITION);
7630 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7631 NotifyMotionArgs motionArgs;
7632
7633 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007634 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007635 processSync(mapper);
7636
7637 // We should receive a down event
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7639 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7640
7641 // Change display id
7642 clearViewports();
7643 prepareSecondaryDisplay(ViewportType::INTERNAL);
7644
7645 // We should receive a cancel event
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7647 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7648 // Then receive reset called
7649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7650}
7651
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007652TEST_F(SingleTouchInputMapperTest,
7653 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7654 addConfigurationProperty("touch.deviceType", "touchScreen");
7655 prepareDisplay(DISPLAY_ORIENTATION_0);
7656 prepareButtons();
7657 prepareAxes(POSITION);
7658 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7660 NotifyMotionArgs motionArgs;
7661
7662 // Start a new gesture.
7663 processDown(mapper, 100, 200);
7664 processSync(mapper);
7665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7666 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7667
7668 // Make the viewport inactive. This will put the device in disabled mode.
7669 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7670 viewport->isActive = false;
7671 mFakePolicy->updateViewport(*viewport);
7672 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7673
7674 // We should receive a cancel event for the ongoing gesture.
7675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7676 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7677 // Then we should be notified that the device was reset.
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7679
7680 // No events are generated while the viewport is inactive.
7681 processMove(mapper, 101, 201);
7682 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007683 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007684 processSync(mapper);
7685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7686
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007687 // Start a new gesture while the viewport is still inactive.
7688 processDown(mapper, 300, 400);
7689 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7690 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7691 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7692 processSync(mapper);
7693
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007694 // Make the viewport active again. The device should resume processing events.
7695 viewport->isActive = true;
7696 mFakePolicy->updateViewport(*viewport);
7697 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7698
7699 // The device is reset because it changes back to direct mode, without generating any events.
7700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7702
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007703 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007704 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7706 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007707
7708 // No more events.
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7711}
7712
Prabir Pradhan211ba622022-10-31 21:09:21 +00007713TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7714 addConfigurationProperty("touch.deviceType", "touchScreen");
7715 prepareDisplay(DISPLAY_ORIENTATION_0);
7716 prepareButtons();
7717 prepareAxes(POSITION);
7718 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7720
7721 // Press a stylus button.
7722 processKey(mapper, BTN_STYLUS, 1);
7723 processSync(mapper);
7724
7725 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7726 processDown(mapper, 100, 200);
7727 processSync(mapper);
7728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7729 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7730 WithCoords(toDisplayX(100), toDisplayY(200)),
7731 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7733 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7734 WithCoords(toDisplayX(100), toDisplayY(200)),
7735 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7736
7737 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7738 // the button has not actually been released, since there will be no pointers through which the
7739 // button state can be reported. The event is generated at the location of the pointer before
7740 // it went up.
7741 processUp(mapper);
7742 processSync(mapper);
7743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7744 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7745 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7747 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7748 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7749}
7750
Prabir Pradhan5632d622021-09-06 07:57:20 -07007751// --- TouchDisplayProjectionTest ---
7752
7753class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7754public:
7755 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7756 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7757 // rotated equivalent of the given un-rotated physical display bounds.
7758 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
7759 uint32_t inverseRotationFlags;
7760 auto width = DISPLAY_WIDTH;
7761 auto height = DISPLAY_HEIGHT;
7762 switch (orientation) {
7763 case DISPLAY_ORIENTATION_90:
7764 inverseRotationFlags = ui::Transform::ROT_270;
7765 std::swap(width, height);
7766 break;
7767 case DISPLAY_ORIENTATION_180:
7768 inverseRotationFlags = ui::Transform::ROT_180;
7769 break;
7770 case DISPLAY_ORIENTATION_270:
7771 inverseRotationFlags = ui::Transform::ROT_90;
7772 std::swap(width, height);
7773 break;
7774 case DISPLAY_ORIENTATION_0:
7775 inverseRotationFlags = ui::Transform::ROT_0;
7776 break;
7777 default:
7778 FAIL() << "Invalid orientation: " << orientation;
7779 }
7780
7781 const ui::Transform rotation(inverseRotationFlags, width, height);
7782 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7783
7784 std::optional<DisplayViewport> internalViewport =
7785 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7786 DisplayViewport& v = *internalViewport;
7787 v.displayId = DISPLAY_ID;
7788 v.orientation = orientation;
7789
7790 v.logicalLeft = 0;
7791 v.logicalTop = 0;
7792 v.logicalRight = 100;
7793 v.logicalBottom = 100;
7794
7795 v.physicalLeft = rotatedPhysicalDisplay.left;
7796 v.physicalTop = rotatedPhysicalDisplay.top;
7797 v.physicalRight = rotatedPhysicalDisplay.right;
7798 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7799
7800 v.deviceWidth = width;
7801 v.deviceHeight = height;
7802
7803 v.isActive = true;
7804 v.uniqueId = UNIQUE_ID;
7805 v.type = ViewportType::INTERNAL;
7806 mFakePolicy->updateViewport(v);
7807 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
7808 }
7809
7810 void assertReceivedMove(const Point& point) {
7811 NotifyMotionArgs motionArgs;
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7813 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7814 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7816 1, 0, 0, 0, 0, 0, 0, 0));
7817 }
7818};
7819
7820TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7821 addConfigurationProperty("touch.deviceType", "touchScreen");
7822 prepareDisplay(DISPLAY_ORIENTATION_0);
7823
7824 prepareButtons();
7825 prepareAxes(POSITION);
7826 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7827
7828 NotifyMotionArgs motionArgs;
7829
7830 // Configure the DisplayViewport such that the logical display maps to a subsection of
7831 // the display panel called the physical display. Here, the physical display is bounded by the
7832 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7833 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7834 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7835 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7836
7837 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7838 DISPLAY_ORIENTATION_270}) {
7839 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7840
7841 // Touches outside the physical display should be ignored, and should not generate any
7842 // events. Ensure touches at the following points that lie outside of the physical display
7843 // area do not generate any events.
7844 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7845 processDown(mapper, toRawX(point.x), toRawY(point.y));
7846 processSync(mapper);
7847 processUp(mapper);
7848 processSync(mapper);
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7850 << "Unexpected event generated for touch outside physical display at point: "
7851 << point.x << ", " << point.y;
7852 }
7853 }
7854}
7855
7856TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7857 addConfigurationProperty("touch.deviceType", "touchScreen");
7858 prepareDisplay(DISPLAY_ORIENTATION_0);
7859
7860 prepareButtons();
7861 prepareAxes(POSITION);
7862 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7863
7864 NotifyMotionArgs motionArgs;
7865
7866 // Configure the DisplayViewport such that the logical display maps to a subsection of
7867 // the display panel called the physical display. Here, the physical display is bounded by the
7868 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7869 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7870
7871 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
7872 DISPLAY_ORIENTATION_270}) {
7873 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7874
7875 // Touches that start outside the physical display should be ignored until it enters the
7876 // physical display bounds, at which point it should generate a down event. Start a touch at
7877 // the point (5, 100), which is outside the physical display bounds.
7878 static const Point kOutsidePoint{5, 100};
7879 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7880 processSync(mapper);
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7882
7883 // Move the touch into the physical display area. This should generate a pointer down.
7884 processMove(mapper, toRawX(11), toRawY(21));
7885 processSync(mapper);
7886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7887 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7888 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7889 ASSERT_NO_FATAL_FAILURE(
7890 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7891
7892 // Move the touch inside the physical display area. This should generate a pointer move.
7893 processMove(mapper, toRawX(69), toRawY(159));
7894 processSync(mapper);
7895 assertReceivedMove({69, 159});
7896
7897 // Move outside the physical display area. Since the pointer is already down, this should
7898 // now continue generating events.
7899 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7900 processSync(mapper);
7901 assertReceivedMove(kOutsidePoint);
7902
7903 // Release. This should generate a pointer up.
7904 processUp(mapper);
7905 processSync(mapper);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7907 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7909 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7910
7911 // Ensure no more events were generated.
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7914 }
7915}
7916
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007917// --- ExternalStylusFusionTest ---
7918
7919class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7920public:
7921 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7922 addConfigurationProperty("touch.deviceType", "touchScreen");
7923 prepareDisplay(DISPLAY_ORIENTATION_0);
7924 prepareButtons();
7925 prepareAxes(POSITION);
7926 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7927
7928 mStylusState.when = ARBITRARY_TIME;
7929 mStylusState.pressure = 0.f;
7930 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7931 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7932 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7933 processExternalStylusState(mapper);
7934 return mapper;
7935 }
7936
7937 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7938 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7939 for (const NotifyArgs& args : generatedArgs) {
7940 mFakeListener->notify(args);
7941 }
7942 // Loop the reader to flush the input listener queue.
7943 mReader->loopOnce();
7944 return generatedArgs;
7945 }
7946
7947protected:
7948 StylusState mStylusState{};
7949 static constexpr uint32_t EXPECTED_SOURCE =
7950 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7951
7952 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7953 auto toolTypeSource =
7954 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7955
7956 // The first pointer is withheld.
7957 processDown(mapper, 100, 200);
7958 processSync(mapper);
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7960 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7961 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7962
7963 // The external stylus reports pressure. The withheld finger pointer is released as a
7964 // stylus.
7965 mStylusState.pressure = 1.f;
7966 processExternalStylusState(mapper);
7967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7968 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7969 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7970
7971 // Subsequent pointer events are not withheld.
7972 processMove(mapper, 101, 201);
7973 processSync(mapper);
7974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7975 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7976
7977 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7979 }
7980
7981 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7982 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7983
7984 // Releasing the touch pointer ends the gesture.
7985 processUp(mapper);
7986 processSync(mapper);
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7988 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7989 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7990
7991 mStylusState.pressure = 0.f;
7992 processExternalStylusState(mapper);
7993 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7995 }
7996
7997 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7998 auto toolTypeSource =
7999 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
8000
8001 // The first pointer is withheld when an external stylus is connected,
8002 // and a timeout is requested.
8003 processDown(mapper, 100, 200);
8004 processSync(mapper);
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8006 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8007 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8008
8009 // If the timeout expires early, it is requested again.
8010 handleTimeout(mapper, ARBITRARY_TIME + 1);
8011 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8012 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8013
8014 // When the timeout expires, the withheld touch is released as a finger pointer.
8015 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
8016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8017 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8018
8019 // Subsequent pointer events are not withheld.
8020 processMove(mapper, 101, 201);
8021 processSync(mapper);
8022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8023 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8024 processUp(mapper);
8025 processSync(mapper);
8026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8027 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
8028
8029 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8031 }
8032
8033private:
8034 InputDeviceInfo mExternalStylusDeviceInfo{};
8035};
8036
8037TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
8038 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8039 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
8040}
8041
8042TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
8043 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8044 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8045}
8046
8047TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
8048 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8049 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8050}
8051
8052// Test a successful stylus fusion gesture where the pressure is reported by the external
8053// before the touch is reported by the touchscreen.
8054TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
8055 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8056 auto toolTypeSource =
8057 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
8058
8059 // The external stylus reports pressure first. It is ignored for now.
8060 mStylusState.pressure = 1.f;
8061 processExternalStylusState(mapper);
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8063 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8064
8065 // When the touch goes down afterwards, it is reported as a stylus pointer.
8066 processDown(mapper, 100, 200);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8069 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8070 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8071
8072 processMove(mapper, 101, 201);
8073 processSync(mapper);
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8075 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8076 processUp(mapper);
8077 processSync(mapper);
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8079 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
8080
8081 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8083}
8084
8085TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
8086 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8087
8088 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8089 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8090
8091 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8092 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8093 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8094 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8095}
8096
8097TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
8098 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8099 auto toolTypeSource =
8100 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
8101
8102 mStylusState.pressure = 0.8f;
8103 processExternalStylusState(mapper);
8104 processDown(mapper, 100, 200);
8105 processSync(mapper);
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8107 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8108 WithPressure(0.8f))));
8109 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8110
8111 // The external stylus reports a pressure change. We wait for some time for a touch event.
8112 mStylusState.pressure = 0.6f;
8113 processExternalStylusState(mapper);
8114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8115 ASSERT_NO_FATAL_FAILURE(
8116 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8117
8118 // If a touch is reported within the timeout, it reports the updated pressure.
8119 processMove(mapper, 101, 201);
8120 processSync(mapper);
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8122 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8123 WithPressure(0.6f))));
8124 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8125
8126 // There is another pressure change.
8127 mStylusState.pressure = 0.5f;
8128 processExternalStylusState(mapper);
8129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8130 ASSERT_NO_FATAL_FAILURE(
8131 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8132
8133 // If a touch is not reported within the timeout, a move event is generated to report
8134 // the new pressure.
8135 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8137 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8138 WithPressure(0.5f))));
8139
8140 // If a zero pressure is reported before the touch goes up, the previous pressure value is
8141 // repeated indefinitely.
8142 mStylusState.pressure = 0.0f;
8143 processExternalStylusState(mapper);
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8145 ASSERT_NO_FATAL_FAILURE(
8146 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8147 processMove(mapper, 102, 202);
8148 processSync(mapper);
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8150 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8151 WithPressure(0.5f))));
8152 processMove(mapper, 103, 203);
8153 processSync(mapper);
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8155 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8156 WithPressure(0.5f))));
8157
8158 processUp(mapper);
8159 processSync(mapper);
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8161 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
8162 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
8163
8164 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8166}
8167
8168TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
8169 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8170 auto source = WithSource(EXPECTED_SOURCE);
8171
8172 mStylusState.pressure = 1.f;
8173 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
8174 processExternalStylusState(mapper);
8175 processDown(mapper, 100, 200);
8176 processSync(mapper);
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8178 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8179 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
8180 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8181
8182 // The external stylus reports a tool change. We wait for some time for a touch event.
8183 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
8184 processExternalStylusState(mapper);
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8186 ASSERT_NO_FATAL_FAILURE(
8187 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8188
8189 // If a touch is reported within the timeout, it reports the updated pressure.
8190 processMove(mapper, 101, 201);
8191 processSync(mapper);
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8193 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8194 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
8195 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8196
8197 // There is another tool type change.
8198 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
8199 processExternalStylusState(mapper);
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8201 ASSERT_NO_FATAL_FAILURE(
8202 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8203
8204 // If a touch is not reported within the timeout, a move event is generated to report
8205 // the new tool type.
8206 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8208 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8209 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
8210
8211 processUp(mapper);
8212 processSync(mapper);
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8214 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
8215 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
8216
8217 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8219}
8220
8221TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
8222 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8223 auto toolTypeSource =
8224 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
8225
8226 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
8227
8228 // The external stylus reports a button change. We wait for some time for a touch event.
8229 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
8230 processExternalStylusState(mapper);
8231 ASSERT_NO_FATAL_FAILURE(
8232 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8233
8234 // If a touch is reported within the timeout, it reports the updated button state.
8235 processMove(mapper, 101, 201);
8236 processSync(mapper);
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8238 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8239 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8241 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8242 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8243 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8244
8245 // The button is now released.
8246 mStylusState.buttons = 0;
8247 processExternalStylusState(mapper);
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8249 ASSERT_NO_FATAL_FAILURE(
8250 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8251
8252 // If a touch is not reported within the timeout, a move event is generated to report
8253 // the new button state.
8254 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8256 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
8257 WithButtonState(0))));
8258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00008259 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8260 WithButtonState(0))));
8261
8262 processUp(mapper);
8263 processSync(mapper);
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008265 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8266
8267 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8269}
8270
Michael Wrightd02c5b62014-02-10 15:10:22 -08008271// --- MultiTouchInputMapperTest ---
8272
8273class MultiTouchInputMapperTest : public TouchInputMapperTest {
8274protected:
8275 void prepareAxes(int axes);
8276
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008277 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
8278 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
8279 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
8280 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
8281 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
8282 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
8283 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
8284 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
8285 void processId(MultiTouchInputMapper& mapper, int32_t id);
8286 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
8287 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
8288 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008289 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008290 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008291 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
8292 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008293};
8294
8295void MultiTouchInputMapperTest::prepareAxes(int axes) {
8296 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008297 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
8298 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008299 }
8300 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008301 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
8302 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008303 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008304 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
8305 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306 }
8307 }
8308 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008309 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8310 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008312 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008313 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314 }
8315 }
8316 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
8318 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008319 }
8320 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008321 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
8322 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323 }
8324 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008325 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
8326 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008327 }
8328 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008329 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
8330 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008331 }
8332 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008333 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
8334 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008335 }
8336 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008337 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008338 }
8339}
8340
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008341void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
8342 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
8344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008345}
8346
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008347void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
8348 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008350}
8351
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008352void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
8353 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355}
8356
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008357void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008359}
8360
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008361void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363}
8364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008365void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
8366 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008368}
8369
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008370void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372}
8373
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008374void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008376}
8377
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008378void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008380}
8381
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008382void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008384}
8385
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008386void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008388}
8389
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008390void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8391 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008393}
8394
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008395void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8396 int32_t value) {
8397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8399}
8400
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008401void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008403}
8404
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008405void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
8406 nsecs_t readTime) {
8407 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408}
8409
Michael Wrightd02c5b62014-02-10 15:10:22 -08008410TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008411 addConfigurationProperty("touch.deviceType", "touchScreen");
8412 prepareDisplay(DISPLAY_ORIENTATION_0);
8413 prepareAxes(POSITION);
8414 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008415 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008416
arthurhungdcef2dc2020-08-11 14:47:50 +08008417 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008418
8419 NotifyMotionArgs motionArgs;
8420
8421 // Two fingers down at once.
8422 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8423 processPosition(mapper, x1, y1);
8424 processMTSync(mapper);
8425 processPosition(mapper, x2, y2);
8426 processMTSync(mapper);
8427 processSync(mapper);
8428
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8431 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8432 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8433 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8434 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8435 ASSERT_EQ(0, motionArgs.flags);
8436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8437 ASSERT_EQ(0, motionArgs.buttonState);
8438 ASSERT_EQ(0, motionArgs.edgeFlags);
8439 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8443 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8444 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8445 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8447
8448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8450 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8451 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8452 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008453 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008454 ASSERT_EQ(0, motionArgs.flags);
8455 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8456 ASSERT_EQ(0, motionArgs.buttonState);
8457 ASSERT_EQ(0, motionArgs.edgeFlags);
8458 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8459 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8461 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8464 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8466 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8467 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8468 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8469 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8470
8471 // Move.
8472 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8473 processPosition(mapper, x1, y1);
8474 processMTSync(mapper);
8475 processPosition(mapper, x2, y2);
8476 processMTSync(mapper);
8477 processSync(mapper);
8478
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8480 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8481 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8482 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8483 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8485 ASSERT_EQ(0, motionArgs.flags);
8486 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8487 ASSERT_EQ(0, motionArgs.buttonState);
8488 ASSERT_EQ(0, motionArgs.edgeFlags);
8489 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8490 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8491 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8492 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8495 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8497 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8498 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8499 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8500 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8501
8502 // First finger up.
8503 x2 += 15; y2 -= 20;
8504 processPosition(mapper, x2, y2);
8505 processMTSync(mapper);
8506 processSync(mapper);
8507
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8510 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8511 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8512 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008513 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514 ASSERT_EQ(0, motionArgs.flags);
8515 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8516 ASSERT_EQ(0, motionArgs.buttonState);
8517 ASSERT_EQ(0, motionArgs.edgeFlags);
8518 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8519 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8520 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8521 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8524 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8525 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8526 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8527 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8528 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8529 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8530
8531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8532 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8533 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8534 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8535 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8537 ASSERT_EQ(0, motionArgs.flags);
8538 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8539 ASSERT_EQ(0, motionArgs.buttonState);
8540 ASSERT_EQ(0, motionArgs.edgeFlags);
8541 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8542 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8543 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8545 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8546 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8547 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8548 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8549
8550 // Move.
8551 x2 += 20; y2 -= 25;
8552 processPosition(mapper, x2, y2);
8553 processMTSync(mapper);
8554 processSync(mapper);
8555
8556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8557 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8558 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8559 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8560 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8562 ASSERT_EQ(0, motionArgs.flags);
8563 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8564 ASSERT_EQ(0, motionArgs.buttonState);
8565 ASSERT_EQ(0, motionArgs.edgeFlags);
8566 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8567 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8568 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8570 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8571 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8572 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8573 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8574
8575 // New finger down.
8576 int32_t x3 = 700, y3 = 300;
8577 processPosition(mapper, x2, y2);
8578 processMTSync(mapper);
8579 processPosition(mapper, x3, y3);
8580 processMTSync(mapper);
8581 processSync(mapper);
8582
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8584 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8585 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8586 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8587 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008588 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008589 ASSERT_EQ(0, motionArgs.flags);
8590 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8591 ASSERT_EQ(0, motionArgs.buttonState);
8592 ASSERT_EQ(0, motionArgs.edgeFlags);
8593 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8596 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8597 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8599 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8601 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8602 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8603 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8604 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8605
8606 // Second finger up.
8607 x3 += 30; y3 -= 20;
8608 processPosition(mapper, x3, y3);
8609 processMTSync(mapper);
8610 processSync(mapper);
8611
8612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8613 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8614 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8615 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8616 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008617 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008618 ASSERT_EQ(0, motionArgs.flags);
8619 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8620 ASSERT_EQ(0, motionArgs.buttonState);
8621 ASSERT_EQ(0, motionArgs.edgeFlags);
8622 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8625 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8628 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8630 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8631 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8632 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8633 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8634
8635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8636 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8637 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8638 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8639 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8641 ASSERT_EQ(0, motionArgs.flags);
8642 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8643 ASSERT_EQ(0, motionArgs.buttonState);
8644 ASSERT_EQ(0, motionArgs.edgeFlags);
8645 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8646 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8649 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8650 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8651 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8652 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8653
8654 // Last finger up.
8655 processMTSync(mapper);
8656 processSync(mapper);
8657
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8659 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8660 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8661 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8662 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8663 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8664 ASSERT_EQ(0, motionArgs.flags);
8665 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8666 ASSERT_EQ(0, motionArgs.buttonState);
8667 ASSERT_EQ(0, motionArgs.edgeFlags);
8668 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8669 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8672 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8673 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8674 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8675 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8676
8677 // Should not have sent any more keys or motions.
8678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8680}
8681
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008682TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8683 addConfigurationProperty("touch.deviceType", "touchScreen");
8684 prepareDisplay(DISPLAY_ORIENTATION_0);
8685
8686 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8687 /*fuzz*/ 0, /*resolution*/ 10);
8688 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8689 /*fuzz*/ 0, /*resolution*/ 11);
8690 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8691 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8692 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8693 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8694 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8695 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8696 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8697 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8698
8699 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8700
8701 // X and Y axes
8702 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8703 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8704 // Touch major and minor
8705 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8706 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8707 // Tool major and minor
8708 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8709 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8710}
8711
8712TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8713 addConfigurationProperty("touch.deviceType", "touchScreen");
8714 prepareDisplay(DISPLAY_ORIENTATION_0);
8715
8716 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8717 /*fuzz*/ 0, /*resolution*/ 10);
8718 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8719 /*fuzz*/ 0, /*resolution*/ 11);
8720
8721 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8722
8723 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8724
8725 // Touch major and minor
8726 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8727 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8728 // Tool major and minor
8729 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8730 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8731}
8732
Michael Wrightd02c5b62014-02-10 15:10:22 -08008733TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008734 addConfigurationProperty("touch.deviceType", "touchScreen");
8735 prepareDisplay(DISPLAY_ORIENTATION_0);
8736 prepareAxes(POSITION | ID);
8737 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008738 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008739
arthurhungdcef2dc2020-08-11 14:47:50 +08008740 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008741
8742 NotifyMotionArgs motionArgs;
8743
8744 // Two fingers down at once.
8745 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8746 processPosition(mapper, x1, y1);
8747 processId(mapper, 1);
8748 processMTSync(mapper);
8749 processPosition(mapper, x2, y2);
8750 processId(mapper, 2);
8751 processMTSync(mapper);
8752 processSync(mapper);
8753
8754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8755 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8756 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8757 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8760 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8761
8762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008763 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008764 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8765 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8766 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8767 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8769 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8770 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8772 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8773
8774 // Move.
8775 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8776 processPosition(mapper, x1, y1);
8777 processId(mapper, 1);
8778 processMTSync(mapper);
8779 processPosition(mapper, x2, y2);
8780 processId(mapper, 2);
8781 processMTSync(mapper);
8782 processSync(mapper);
8783
8784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8786 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8787 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8788 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8789 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8790 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8792 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8794 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8795
8796 // First finger up.
8797 x2 += 15; y2 -= 20;
8798 processPosition(mapper, x2, y2);
8799 processId(mapper, 2);
8800 processMTSync(mapper);
8801 processSync(mapper);
8802
8803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008804 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008805 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8806 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8808 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8811 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8813 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8814
8815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8817 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8818 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8821 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8822
8823 // Move.
8824 x2 += 20; y2 -= 25;
8825 processPosition(mapper, x2, y2);
8826 processId(mapper, 2);
8827 processMTSync(mapper);
8828 processSync(mapper);
8829
8830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8832 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8833 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8834 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8836 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8837
8838 // New finger down.
8839 int32_t x3 = 700, y3 = 300;
8840 processPosition(mapper, x2, y2);
8841 processId(mapper, 2);
8842 processMTSync(mapper);
8843 processPosition(mapper, x3, y3);
8844 processId(mapper, 3);
8845 processMTSync(mapper);
8846 processSync(mapper);
8847
8848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008849 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008850 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8851 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8852 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8853 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8856 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8858 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8859
8860 // Second finger up.
8861 x3 += 30; y3 -= 20;
8862 processPosition(mapper, x3, y3);
8863 processId(mapper, 3);
8864 processMTSync(mapper);
8865 processSync(mapper);
8866
8867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008868 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008869 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8870 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8871 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8872 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8875 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8877 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8878
8879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8881 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8882 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8883 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8884 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8885 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8886
8887 // Last finger up.
8888 processMTSync(mapper);
8889 processSync(mapper);
8890
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8892 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8893 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8894 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8897 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8898
8899 // Should not have sent any more keys or motions.
8900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8902}
8903
8904TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008905 addConfigurationProperty("touch.deviceType", "touchScreen");
8906 prepareDisplay(DISPLAY_ORIENTATION_0);
8907 prepareAxes(POSITION | ID | SLOT);
8908 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008909 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008910
arthurhungdcef2dc2020-08-11 14:47:50 +08008911 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008912
8913 NotifyMotionArgs motionArgs;
8914
8915 // Two fingers down at once.
8916 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8917 processPosition(mapper, x1, y1);
8918 processId(mapper, 1);
8919 processSlot(mapper, 1);
8920 processPosition(mapper, x2, y2);
8921 processId(mapper, 2);
8922 processSync(mapper);
8923
8924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8925 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8926 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8927 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8928 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8930 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8931
8932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008933 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008934 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8935 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8937 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8938 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8940 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8942 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8943
8944 // Move.
8945 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8946 processSlot(mapper, 0);
8947 processPosition(mapper, x1, y1);
8948 processSlot(mapper, 1);
8949 processPosition(mapper, x2, y2);
8950 processSync(mapper);
8951
8952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8954 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8955 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8957 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8958 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8960 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8962 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8963
8964 // First finger up.
8965 x2 += 15; y2 -= 20;
8966 processSlot(mapper, 0);
8967 processId(mapper, -1);
8968 processSlot(mapper, 1);
8969 processPosition(mapper, x2, y2);
8970 processSync(mapper);
8971
8972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008973 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008974 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8975 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8977 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8980 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8982 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8983
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8986 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8987 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8988 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8990 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8991
8992 // Move.
8993 x2 += 20; y2 -= 25;
8994 processPosition(mapper, x2, y2);
8995 processSync(mapper);
8996
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8999 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
9000 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
9001 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9003 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9004
9005 // New finger down.
9006 int32_t x3 = 700, y3 = 300;
9007 processPosition(mapper, x2, y2);
9008 processSlot(mapper, 0);
9009 processId(mapper, 3);
9010 processPosition(mapper, x3, y3);
9011 processSync(mapper);
9012
9013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009014 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009015 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
9016 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
9017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9018 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
9019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9021 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9023 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9024
9025 // Second finger up.
9026 x3 += 30; y3 -= 20;
9027 processSlot(mapper, 1);
9028 processId(mapper, -1);
9029 processSlot(mapper, 0);
9030 processPosition(mapper, x3, y3);
9031 processSync(mapper);
9032
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009034 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009035 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
9036 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
9037 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9038 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
9039 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9041 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9043 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9044
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9047 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
9048 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
9049 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9051 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9052
9053 // Last finger up.
9054 processId(mapper, -1);
9055 processSync(mapper);
9056
9057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9058 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9059 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
9060 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
9061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9063 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9064
9065 // Should not have sent any more keys or motions.
9066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9068}
9069
9070TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009071 addConfigurationProperty("touch.deviceType", "touchScreen");
9072 prepareDisplay(DISPLAY_ORIENTATION_0);
9073 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009074 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009075
9076 // These calculations are based on the input device calibration documentation.
9077 int32_t rawX = 100;
9078 int32_t rawY = 200;
9079 int32_t rawTouchMajor = 7;
9080 int32_t rawTouchMinor = 6;
9081 int32_t rawToolMajor = 9;
9082 int32_t rawToolMinor = 8;
9083 int32_t rawPressure = 11;
9084 int32_t rawDistance = 0;
9085 int32_t rawOrientation = 3;
9086 int32_t id = 5;
9087
9088 float x = toDisplayX(rawX);
9089 float y = toDisplayY(rawY);
9090 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
9091 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
9092 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
9093 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
9094 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
9095 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
9096 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
9097 float distance = float(rawDistance);
9098
9099 processPosition(mapper, rawX, rawY);
9100 processTouchMajor(mapper, rawTouchMajor);
9101 processTouchMinor(mapper, rawTouchMinor);
9102 processToolMajor(mapper, rawToolMajor);
9103 processToolMinor(mapper, rawToolMinor);
9104 processPressure(mapper, rawPressure);
9105 processOrientation(mapper, rawOrientation);
9106 processDistance(mapper, rawDistance);
9107 processId(mapper, id);
9108 processMTSync(mapper);
9109 processSync(mapper);
9110
9111 NotifyMotionArgs args;
9112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9113 ASSERT_EQ(0, args.pointerProperties[0].id);
9114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9115 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
9116 orientation, distance));
9117}
9118
9119TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009120 addConfigurationProperty("touch.deviceType", "touchScreen");
9121 prepareDisplay(DISPLAY_ORIENTATION_0);
9122 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
9123 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009124 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009125
9126 // These calculations are based on the input device calibration documentation.
9127 int32_t rawX = 100;
9128 int32_t rawY = 200;
9129 int32_t rawTouchMajor = 140;
9130 int32_t rawTouchMinor = 120;
9131 int32_t rawToolMajor = 180;
9132 int32_t rawToolMinor = 160;
9133
9134 float x = toDisplayX(rawX);
9135 float y = toDisplayY(rawY);
9136 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
9137 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
9138 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
9139 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
9140 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
9141
9142 processPosition(mapper, rawX, rawY);
9143 processTouchMajor(mapper, rawTouchMajor);
9144 processTouchMinor(mapper, rawTouchMinor);
9145 processToolMajor(mapper, rawToolMajor);
9146 processToolMinor(mapper, rawToolMinor);
9147 processMTSync(mapper);
9148 processSync(mapper);
9149
9150 NotifyMotionArgs args;
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9153 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
9154}
9155
9156TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009157 addConfigurationProperty("touch.deviceType", "touchScreen");
9158 prepareDisplay(DISPLAY_ORIENTATION_0);
9159 prepareAxes(POSITION | TOUCH | TOOL);
9160 addConfigurationProperty("touch.size.calibration", "diameter");
9161 addConfigurationProperty("touch.size.scale", "10");
9162 addConfigurationProperty("touch.size.bias", "160");
9163 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009164 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009165
9166 // These calculations are based on the input device calibration documentation.
9167 // Note: We only provide a single common touch/tool value because the device is assumed
9168 // not to emit separate values for each pointer (isSummed = 1).
9169 int32_t rawX = 100;
9170 int32_t rawY = 200;
9171 int32_t rawX2 = 150;
9172 int32_t rawY2 = 250;
9173 int32_t rawTouchMajor = 5;
9174 int32_t rawToolMajor = 8;
9175
9176 float x = toDisplayX(rawX);
9177 float y = toDisplayY(rawY);
9178 float x2 = toDisplayX(rawX2);
9179 float y2 = toDisplayY(rawY2);
9180 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
9181 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
9182 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
9183
9184 processPosition(mapper, rawX, rawY);
9185 processTouchMajor(mapper, rawTouchMajor);
9186 processToolMajor(mapper, rawToolMajor);
9187 processMTSync(mapper);
9188 processPosition(mapper, rawX2, rawY2);
9189 processTouchMajor(mapper, rawTouchMajor);
9190 processToolMajor(mapper, rawToolMajor);
9191 processMTSync(mapper);
9192 processSync(mapper);
9193
9194 NotifyMotionArgs args;
9195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9196 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9197
9198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009199 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009200 ASSERT_EQ(size_t(2), args.pointerCount);
9201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9202 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9203 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
9204 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
9205}
9206
9207TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009208 addConfigurationProperty("touch.deviceType", "touchScreen");
9209 prepareDisplay(DISPLAY_ORIENTATION_0);
9210 prepareAxes(POSITION | TOUCH | TOOL);
9211 addConfigurationProperty("touch.size.calibration", "area");
9212 addConfigurationProperty("touch.size.scale", "43");
9213 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009214 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009215
9216 // These calculations are based on the input device calibration documentation.
9217 int32_t rawX = 100;
9218 int32_t rawY = 200;
9219 int32_t rawTouchMajor = 5;
9220 int32_t rawToolMajor = 8;
9221
9222 float x = toDisplayX(rawX);
9223 float y = toDisplayY(rawY);
9224 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
9225 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
9226 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
9227
9228 processPosition(mapper, rawX, rawY);
9229 processTouchMajor(mapper, rawTouchMajor);
9230 processToolMajor(mapper, rawToolMajor);
9231 processMTSync(mapper);
9232 processSync(mapper);
9233
9234 NotifyMotionArgs args;
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9237 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9238}
9239
9240TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009241 addConfigurationProperty("touch.deviceType", "touchScreen");
9242 prepareDisplay(DISPLAY_ORIENTATION_0);
9243 prepareAxes(POSITION | PRESSURE);
9244 addConfigurationProperty("touch.pressure.calibration", "amplitude");
9245 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009246 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009247
Michael Wrightaa449c92017-12-13 21:21:43 +00009248 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009249 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00009250 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
9251 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
9252 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
9253
Michael Wrightd02c5b62014-02-10 15:10:22 -08009254 // These calculations are based on the input device calibration documentation.
9255 int32_t rawX = 100;
9256 int32_t rawY = 200;
9257 int32_t rawPressure = 60;
9258
9259 float x = toDisplayX(rawX);
9260 float y = toDisplayY(rawY);
9261 float pressure = float(rawPressure) * 0.01f;
9262
9263 processPosition(mapper, rawX, rawY);
9264 processPressure(mapper, rawPressure);
9265 processMTSync(mapper);
9266 processSync(mapper);
9267
9268 NotifyMotionArgs args;
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9271 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
9272}
9273
9274TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009275 addConfigurationProperty("touch.deviceType", "touchScreen");
9276 prepareDisplay(DISPLAY_ORIENTATION_0);
9277 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009278 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009279
9280 NotifyMotionArgs motionArgs;
9281 NotifyKeyArgs keyArgs;
9282
9283 processId(mapper, 1);
9284 processPosition(mapper, 100, 200);
9285 processSync(mapper);
9286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9287 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9288 ASSERT_EQ(0, motionArgs.buttonState);
9289
9290 // press BTN_LEFT, release BTN_LEFT
9291 processKey(mapper, BTN_LEFT, 1);
9292 processSync(mapper);
9293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9295 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9296
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9299 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9300
Michael Wrightd02c5b62014-02-10 15:10:22 -08009301 processKey(mapper, BTN_LEFT, 0);
9302 processSync(mapper);
9303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009304 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009305 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009306
9307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009309 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009310
9311 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
9312 processKey(mapper, BTN_RIGHT, 1);
9313 processKey(mapper, BTN_MIDDLE, 1);
9314 processSync(mapper);
9315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9316 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9317 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9318 motionArgs.buttonState);
9319
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9322 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
9323
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9325 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9326 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9327 motionArgs.buttonState);
9328
Michael Wrightd02c5b62014-02-10 15:10:22 -08009329 processKey(mapper, BTN_RIGHT, 0);
9330 processSync(mapper);
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009333 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009334
9335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009337 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009338
9339 processKey(mapper, BTN_MIDDLE, 0);
9340 processSync(mapper);
9341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009342 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009343 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009344
9345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009347 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009348
9349 // press BTN_BACK, release BTN_BACK
9350 processKey(mapper, BTN_BACK, 1);
9351 processSync(mapper);
9352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9353 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9354 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009355
Michael Wrightd02c5b62014-02-10 15:10:22 -08009356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009358 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9359
9360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9361 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9362 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009363
9364 processKey(mapper, BTN_BACK, 0);
9365 processSync(mapper);
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009367 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009368 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009369
9370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009372 ASSERT_EQ(0, motionArgs.buttonState);
9373
Michael Wrightd02c5b62014-02-10 15:10:22 -08009374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9375 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9376 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9377
9378 // press BTN_SIDE, release BTN_SIDE
9379 processKey(mapper, BTN_SIDE, 1);
9380 processSync(mapper);
9381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9382 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9383 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009384
Michael Wrightd02c5b62014-02-10 15:10:22 -08009385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009387 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9388
9389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9390 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9391 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009392
9393 processKey(mapper, BTN_SIDE, 0);
9394 processSync(mapper);
9395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009397 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009398
9399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009401 ASSERT_EQ(0, motionArgs.buttonState);
9402
Michael Wrightd02c5b62014-02-10 15:10:22 -08009403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9404 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9405 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9406
9407 // press BTN_FORWARD, release BTN_FORWARD
9408 processKey(mapper, BTN_FORWARD, 1);
9409 processSync(mapper);
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9411 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9412 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009413
Michael Wrightd02c5b62014-02-10 15:10:22 -08009414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009416 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9417
9418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9419 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9420 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009421
9422 processKey(mapper, BTN_FORWARD, 0);
9423 processSync(mapper);
9424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009425 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009426 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009427
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009430 ASSERT_EQ(0, motionArgs.buttonState);
9431
Michael Wrightd02c5b62014-02-10 15:10:22 -08009432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9433 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9434 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9435
9436 // press BTN_EXTRA, release BTN_EXTRA
9437 processKey(mapper, BTN_EXTRA, 1);
9438 processSync(mapper);
9439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9440 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9441 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009442
Michael Wrightd02c5b62014-02-10 15:10:22 -08009443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009445 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9446
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9448 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9449 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009450
9451 processKey(mapper, BTN_EXTRA, 0);
9452 processSync(mapper);
9453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009454 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009455 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009456
9457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009459 ASSERT_EQ(0, motionArgs.buttonState);
9460
Michael Wrightd02c5b62014-02-10 15:10:22 -08009461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9462 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9463 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9464
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9466
Michael Wrightd02c5b62014-02-10 15:10:22 -08009467 // press BTN_STYLUS, release BTN_STYLUS
9468 processKey(mapper, BTN_STYLUS, 1);
9469 processSync(mapper);
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009472 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9473
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9476 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009477
9478 processKey(mapper, BTN_STYLUS, 0);
9479 processSync(mapper);
9480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009481 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009482 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009483
9484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009486 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009487
9488 // press BTN_STYLUS2, release BTN_STYLUS2
9489 processKey(mapper, BTN_STYLUS2, 1);
9490 processSync(mapper);
9491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009493 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9494
9495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9496 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9497 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009498
9499 processKey(mapper, BTN_STYLUS2, 0);
9500 processSync(mapper);
9501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009502 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009503 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009504
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009507 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009508
9509 // release touch
9510 processId(mapper, -1);
9511 processSync(mapper);
9512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9513 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9514 ASSERT_EQ(0, motionArgs.buttonState);
9515}
9516
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009517TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9518 addConfigurationProperty("touch.deviceType", "touchScreen");
9519 prepareDisplay(DISPLAY_ORIENTATION_0);
9520 prepareAxes(POSITION | ID | SLOT);
9521 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9522
9523 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9524 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9525
9526 // Touch down.
9527 processId(mapper, 1);
9528 processPosition(mapper, 100, 200);
9529 processSync(mapper);
9530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9531 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9532
9533 // Press and release button mapped to the primary stylus button.
9534 processKey(mapper, BTN_A, 1);
9535 processSync(mapper);
9536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9537 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9538 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9540 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9541 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9542
9543 processKey(mapper, BTN_A, 0);
9544 processSync(mapper);
9545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9546 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9548 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9549
9550 // Press and release the HID usage mapped to the secondary stylus button.
9551 processHidUsage(mapper, 0xabcd, 1);
9552 processSync(mapper);
9553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9554 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9555 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9557 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9558 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9559
9560 processHidUsage(mapper, 0xabcd, 0);
9561 processSync(mapper);
9562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9563 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9565 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9566
9567 // Release touch.
9568 processId(mapper, -1);
9569 processSync(mapper);
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9571 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9572}
9573
Michael Wrightd02c5b62014-02-10 15:10:22 -08009574TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009575 addConfigurationProperty("touch.deviceType", "touchScreen");
9576 prepareDisplay(DISPLAY_ORIENTATION_0);
9577 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009578 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009579
9580 NotifyMotionArgs motionArgs;
9581
9582 // default tool type is finger
9583 processId(mapper, 1);
9584 processPosition(mapper, 100, 200);
9585 processSync(mapper);
9586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9587 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9589
9590 // eraser
9591 processKey(mapper, BTN_TOOL_RUBBER, 1);
9592 processSync(mapper);
9593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9596
9597 // stylus
9598 processKey(mapper, BTN_TOOL_RUBBER, 0);
9599 processKey(mapper, BTN_TOOL_PEN, 1);
9600 processSync(mapper);
9601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9604
9605 // brush
9606 processKey(mapper, BTN_TOOL_PEN, 0);
9607 processKey(mapper, BTN_TOOL_BRUSH, 1);
9608 processSync(mapper);
9609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9612
9613 // pencil
9614 processKey(mapper, BTN_TOOL_BRUSH, 0);
9615 processKey(mapper, BTN_TOOL_PENCIL, 1);
9616 processSync(mapper);
9617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9619 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9620
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009621 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009622 processKey(mapper, BTN_TOOL_PENCIL, 0);
9623 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9624 processSync(mapper);
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9628
9629 // mouse
9630 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9631 processKey(mapper, BTN_TOOL_MOUSE, 1);
9632 processSync(mapper);
9633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9636
9637 // lens
9638 processKey(mapper, BTN_TOOL_MOUSE, 0);
9639 processKey(mapper, BTN_TOOL_LENS, 1);
9640 processSync(mapper);
9641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9644
9645 // double-tap
9646 processKey(mapper, BTN_TOOL_LENS, 0);
9647 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9648 processSync(mapper);
9649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9651 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9652
9653 // triple-tap
9654 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9655 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9656 processSync(mapper);
9657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9660
9661 // quad-tap
9662 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9663 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9664 processSync(mapper);
9665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9668
9669 // finger
9670 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9671 processKey(mapper, BTN_TOOL_FINGER, 1);
9672 processSync(mapper);
9673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9674 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9675 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9676
9677 // stylus trumps finger
9678 processKey(mapper, BTN_TOOL_PEN, 1);
9679 processSync(mapper);
9680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9682 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9683
9684 // eraser trumps stylus
9685 processKey(mapper, BTN_TOOL_RUBBER, 1);
9686 processSync(mapper);
9687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9688 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
9690
9691 // mouse trumps eraser
9692 processKey(mapper, BTN_TOOL_MOUSE, 1);
9693 processSync(mapper);
9694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9696 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
9697
9698 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9699 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9700 processSync(mapper);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9702 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9703 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9704
9705 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9706 processToolType(mapper, MT_TOOL_PEN);
9707 processSync(mapper);
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
9711
9712 // back to default tool type
9713 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9714 processKey(mapper, BTN_TOOL_MOUSE, 0);
9715 processKey(mapper, BTN_TOOL_RUBBER, 0);
9716 processKey(mapper, BTN_TOOL_PEN, 0);
9717 processKey(mapper, BTN_TOOL_FINGER, 0);
9718 processSync(mapper);
9719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9721 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9722}
9723
9724TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009725 addConfigurationProperty("touch.deviceType", "touchScreen");
9726 prepareDisplay(DISPLAY_ORIENTATION_0);
9727 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009728 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009729 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009730
9731 NotifyMotionArgs motionArgs;
9732
9733 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9734 processId(mapper, 1);
9735 processPosition(mapper, 100, 200);
9736 processSync(mapper);
9737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9738 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9740 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9741
9742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9743 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9745 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9746
9747 // move a little
9748 processPosition(mapper, 150, 250);
9749 processSync(mapper);
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9753 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9754
9755 // down when BTN_TOUCH is pressed, pressure defaults to 1
9756 processKey(mapper, BTN_TOUCH, 1);
9757 processSync(mapper);
9758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9759 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9761 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9762
9763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9766 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9767
9768 // up when BTN_TOUCH is released, hover restored
9769 processKey(mapper, BTN_TOUCH, 0);
9770 processSync(mapper);
9771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9772 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9774 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9775
9776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9777 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9779 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9780
9781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9782 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9784 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9785
9786 // exit hover when pointer goes away
9787 processId(mapper, -1);
9788 processSync(mapper);
9789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9790 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9792 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9793}
9794
9795TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009796 addConfigurationProperty("touch.deviceType", "touchScreen");
9797 prepareDisplay(DISPLAY_ORIENTATION_0);
9798 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009799 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009800
9801 NotifyMotionArgs motionArgs;
9802
9803 // initially hovering because pressure is 0
9804 processId(mapper, 1);
9805 processPosition(mapper, 100, 200);
9806 processPressure(mapper, 0);
9807 processSync(mapper);
9808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9809 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9811 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9812
9813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9814 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9816 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9817
9818 // move a little
9819 processPosition(mapper, 150, 250);
9820 processSync(mapper);
9821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9822 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9824 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9825
9826 // down when pressure becomes non-zero
9827 processPressure(mapper, RAW_PRESSURE_MAX);
9828 processSync(mapper);
9829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9830 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9832 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9833
9834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9835 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9837 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9838
9839 // up when pressure becomes 0, hover restored
9840 processPressure(mapper, 0);
9841 processSync(mapper);
9842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9843 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9845 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9846
9847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9848 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9849 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9850 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9851
9852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9853 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9855 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9856
9857 // exit hover when pointer goes away
9858 processId(mapper, -1);
9859 processSync(mapper);
9860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9861 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9863 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9864}
9865
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009866/**
9867 * Set the input device port <--> display port associations, and check that the
9868 * events are routed to the display that matches the display port.
9869 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9870 */
9871TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009872 const std::string usb2 = "USB2";
9873 const uint8_t hdmi1 = 0;
9874 const uint8_t hdmi2 = 1;
9875 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009876 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009877
9878 addConfigurationProperty("touch.deviceType", "touchScreen");
9879 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009880 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009881
9882 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9883 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9884
9885 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9886 // for this input device is specified, and the matching viewport is not present,
9887 // the input device should be disabled (at the mapper level).
9888
9889 // Add viewport for display 2 on hdmi2
9890 prepareSecondaryDisplay(type, hdmi2);
9891 // Send a touch event
9892 processPosition(mapper, 100, 100);
9893 processSync(mapper);
9894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9895
9896 // Add viewport for display 1 on hdmi1
9897 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
9898 // Send a touch event again
9899 processPosition(mapper, 100, 100);
9900 processSync(mapper);
9901
9902 NotifyMotionArgs args;
9903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9904 ASSERT_EQ(DISPLAY_ID, args.displayId);
9905}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009906
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009907TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9908 addConfigurationProperty("touch.deviceType", "touchScreen");
9909 prepareAxes(POSITION);
9910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9911
9912 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9913
9914 prepareDisplay(DISPLAY_ORIENTATION_0);
9915 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
9916
9917 // Send a touch event
9918 processPosition(mapper, 100, 100);
9919 processSync(mapper);
9920
9921 NotifyMotionArgs args;
9922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9923 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9924}
9925
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009926TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009927 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009928 std::shared_ptr<FakePointerController> fakePointerController =
9929 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009930 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009931 fakePointerController->setPosition(100, 200);
9932 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009933 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009934
Garfield Tan888a6a42020-01-09 11:39:16 -08009935 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009936 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009937
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009938 prepareDisplay(DISPLAY_ORIENTATION_0);
9939 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009940 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009941
Harry Cutts16a24cc2022-10-26 15:22:19 +00009942 // Check source is a touchpad that would obtain the PointerController.
9943 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009944
9945 NotifyMotionArgs motionArgs;
9946 processPosition(mapper, 100, 100);
9947 processSync(mapper);
9948
9949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9950 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9951 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9952}
9953
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009954/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009955 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9956 */
9957TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9958 addConfigurationProperty("touch.deviceType", "touchScreen");
9959 prepareAxes(POSITION);
9960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9961
9962 prepareDisplay(DISPLAY_ORIENTATION_0);
9963 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9964 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9965 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9966 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9967
9968 NotifyMotionArgs args;
9969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9970 ASSERT_EQ(26, args.readTime);
9971
9972 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9973 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9974 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9975
9976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9977 ASSERT_EQ(33, args.readTime);
9978}
9979
9980/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009981 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9982 * events should not be delivered to the listener.
9983 */
9984TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9985 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009986 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009987 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9988 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9989 ViewportType::INTERNAL);
9990 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9991 prepareAxes(POSITION);
9992 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9993
9994 NotifyMotionArgs motionArgs;
9995 processPosition(mapper, 100, 100);
9996 processSync(mapper);
9997
9998 mFakeListener->assertNotifyMotionWasNotCalled();
9999}
10000
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010001/**
10002 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
10003 * the touch mapper can process the events and the events can be delivered to the listener.
10004 */
10005TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
10006 addConfigurationProperty("touch.deviceType", "touchScreen");
10007 addConfigurationProperty("touch.enableForInactiveViewport", "1");
10008 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
10009 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
10010 ViewportType::INTERNAL);
10011 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10012 prepareAxes(POSITION);
10013 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10014
10015 NotifyMotionArgs motionArgs;
10016 processPosition(mapper, 100, 100);
10017 processSync(mapper);
10018
10019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10020 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10021}
10022
Garfield Tanc734e4f2021-01-15 20:01:39 -080010023TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
10024 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010025 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Garfield Tanc734e4f2021-01-15 20:01:39 -080010026 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
10027 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
10028 ViewportType::INTERNAL);
10029 std::optional<DisplayViewport> optionalDisplayViewport =
10030 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
10031 ASSERT_TRUE(optionalDisplayViewport.has_value());
10032 DisplayViewport displayViewport = *optionalDisplayViewport;
10033
10034 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10035 prepareAxes(POSITION);
10036 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10037
10038 // Finger down
10039 int32_t x = 100, y = 100;
10040 processPosition(mapper, x, y);
10041 processSync(mapper);
10042
10043 NotifyMotionArgs motionArgs;
10044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10045 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10046
10047 // Deactivate display viewport
10048 displayViewport.isActive = false;
10049 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
10050 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10051
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010052 // The ongoing touch should be canceled immediately
10053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10054 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10055
10056 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -080010057 x += 10, y += 10;
10058 processPosition(mapper, x, y);
10059 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -080010061
10062 // Reactivate display viewport
10063 displayViewport.isActive = true;
10064 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
10065 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10066
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010067 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -080010068 x += 10, y += 10;
10069 processPosition(mapper, x, y);
10070 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10072 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010073}
10074
Arthur Hung7c645402019-01-25 17:45:42 +080010075TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
10076 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +080010077 prepareAxes(POSITION | ID | SLOT);
10078 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010079 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +080010080
10081 // Create the second touch screen device, and enable multi fingers.
10082 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +080010083 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +080010084 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010085 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +080010086 std::shared_ptr<InputDevice> device2 =
10087 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010088 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +080010089
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010090 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10091 0 /*flat*/, 0 /*fuzz*/);
10092 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10093 0 /*flat*/, 0 /*fuzz*/);
10094 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
10095 0 /*flat*/, 0 /*fuzz*/);
10096 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
10097 0 /*flat*/, 0 /*fuzz*/);
10098 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
10099 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
10100 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +080010101
10102 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010103 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010104 std::list<NotifyArgs> unused =
10105 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10106 0 /*changes*/);
10107 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +080010108
10109 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +010010110 std::shared_ptr<FakePointerController> fakePointerController =
10111 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010112 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +080010113
10114 // Setup policy for associated displays and show touches.
10115 const uint8_t hdmi1 = 0;
10116 const uint8_t hdmi2 = 1;
10117 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
10118 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
10119 mFakePolicy->setShowTouches(true);
10120
10121 // Create displays.
10122 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010123 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +080010124
10125 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010126 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10127 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
10128 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +080010129
10130 // Two fingers down at default display.
10131 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
10132 processPosition(mapper, x1, y1);
10133 processId(mapper, 1);
10134 processSlot(mapper, 1);
10135 processPosition(mapper, x2, y2);
10136 processId(mapper, 2);
10137 processSync(mapper);
10138
10139 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
10140 fakePointerController->getSpots().find(DISPLAY_ID);
10141 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10142 ASSERT_EQ(size_t(2), iter->second.size());
10143
10144 // Two fingers down at second display.
10145 processPosition(mapper2, x1, y1);
10146 processId(mapper2, 1);
10147 processSlot(mapper2, 1);
10148 processPosition(mapper2, x2, y2);
10149 processId(mapper2, 2);
10150 processSync(mapper2);
10151
10152 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
10153 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10154 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +000010155
10156 // Disable the show touches configuration and ensure the spots are cleared.
10157 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010158 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10159 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +000010160
10161 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +080010162}
10163
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010164TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010165 prepareAxes(POSITION);
10166 addConfigurationProperty("touch.deviceType", "touchScreen");
10167 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010168 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010169
10170 NotifyMotionArgs motionArgs;
10171 // Unrotated video frame
10172 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10173 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010174 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010175 processPosition(mapper, 100, 200);
10176 processSync(mapper);
10177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10178 ASSERT_EQ(frames, motionArgs.videoFrames);
10179
10180 // Subsequent touch events should not have any videoframes
10181 // This is implemented separately in FakeEventHub,
10182 // but that should match the behaviour of TouchVideoDevice.
10183 processPosition(mapper, 200, 200);
10184 processSync(mapper);
10185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10186 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
10187}
10188
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010189TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010190 prepareAxes(POSITION);
10191 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010192 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010193 // Unrotated video frame
10194 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10195 NotifyMotionArgs motionArgs;
10196
10197 // Test all 4 orientations
10198 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010199 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
10200 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10201 clearViewports();
10202 prepareDisplay(orientation);
10203 std::vector<TouchVideoFrame> frames{frame};
10204 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10205 processPosition(mapper, 100, 200);
10206 processSync(mapper);
10207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10208 ASSERT_EQ(frames, motionArgs.videoFrames);
10209 }
10210}
10211
10212TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
10213 prepareAxes(POSITION);
10214 addConfigurationProperty("touch.deviceType", "touchScreen");
10215 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10216 // orientation-aware are affected by display rotation.
10217 addConfigurationProperty("touch.orientationAware", "0");
10218 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10219 // Unrotated video frame
10220 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10221 NotifyMotionArgs motionArgs;
10222
10223 // Test all 4 orientations
10224 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010225 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
10226 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10227 clearViewports();
10228 prepareDisplay(orientation);
10229 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010230 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010231 processPosition(mapper, 100, 200);
10232 processSync(mapper);
10233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010234 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10235 // compared to the display. This is so that when the window transform (which contains the
10236 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10237 // window's coordinate space.
10238 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010239 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +080010240
10241 // Release finger.
10242 processSync(mapper);
10243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010244 }
10245}
10246
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010247TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010248 prepareAxes(POSITION);
10249 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010250 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010251 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10252 // so mix these.
10253 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10254 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10255 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10256 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10257 NotifyMotionArgs motionArgs;
10258
10259 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010260 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010261 processPosition(mapper, 100, 200);
10262 processSync(mapper);
10263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010264 ASSERT_EQ(frames, motionArgs.videoFrames);
10265}
10266
10267TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
10268 prepareAxes(POSITION);
10269 addConfigurationProperty("touch.deviceType", "touchScreen");
10270 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10271 // orientation-aware are affected by display rotation.
10272 addConfigurationProperty("touch.orientationAware", "0");
10273 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10274 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10275 // so mix these.
10276 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10277 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10278 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10279 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10280 NotifyMotionArgs motionArgs;
10281
10282 prepareDisplay(DISPLAY_ORIENTATION_90);
10283 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10284 processPosition(mapper, 100, 200);
10285 processSync(mapper);
10286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10287 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
10288 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10289 // compared to the display. This is so that when the window transform (which contains the
10290 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10291 // window's coordinate space.
10292 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
10293 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010294 ASSERT_EQ(frames, motionArgs.videoFrames);
10295}
10296
Arthur Hung9da14732019-09-02 16:16:58 +080010297/**
10298 * If we had defined port associations, but the viewport is not ready, the touch device would be
10299 * expected to be disabled, and it should be enabled after the viewport has found.
10300 */
10301TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +080010302 constexpr uint8_t hdmi2 = 1;
10303 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010304 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +080010305
10306 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
10307
10308 addConfigurationProperty("touch.deviceType", "touchScreen");
10309 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010310 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +080010311
10312 ASSERT_EQ(mDevice->isEnabled(), false);
10313
10314 // Add display on hdmi2, the device should be enabled and can receive touch event.
10315 prepareSecondaryDisplay(type, hdmi2);
10316 ASSERT_EQ(mDevice->isEnabled(), true);
10317
10318 // Send a touch event.
10319 processPosition(mapper, 100, 100);
10320 processSync(mapper);
10321
10322 NotifyMotionArgs args;
10323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10324 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10325}
10326
Arthur Hung421eb1c2020-01-16 00:09:42 +080010327TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010328 addConfigurationProperty("touch.deviceType", "touchScreen");
10329 prepareDisplay(DISPLAY_ORIENTATION_0);
10330 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010331 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010332
10333 NotifyMotionArgs motionArgs;
10334
10335 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10336 // finger down
10337 processId(mapper, 1);
10338 processPosition(mapper, x1, y1);
10339 processSync(mapper);
10340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10341 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10343
10344 // finger move
10345 processId(mapper, 1);
10346 processPosition(mapper, x2, y2);
10347 processSync(mapper);
10348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10351
10352 // finger up.
10353 processId(mapper, -1);
10354 processSync(mapper);
10355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10356 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10357 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10358
10359 // new finger down
10360 processId(mapper, 1);
10361 processPosition(mapper, x3, y3);
10362 processSync(mapper);
10363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10364 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10366}
10367
10368/**
arthurhungcc7f9802020-04-30 17:55:40 +080010369 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10370 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010371 */
arthurhungcc7f9802020-04-30 17:55:40 +080010372TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010373 addConfigurationProperty("touch.deviceType", "touchScreen");
10374 prepareDisplay(DISPLAY_ORIENTATION_0);
10375 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -080010376 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010377
10378 NotifyMotionArgs motionArgs;
10379
10380 // default tool type is finger
10381 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010382 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010383 processPosition(mapper, x1, y1);
10384 processSync(mapper);
10385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10386 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10387 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10388
10389 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10390 processToolType(mapper, MT_TOOL_PALM);
10391 processSync(mapper);
10392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10393 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10394
10395 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010396 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010397 processPosition(mapper, x2, y2);
10398 processSync(mapper);
10399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10400
10401 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010402 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010403 processSync(mapper);
10404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10405
10406 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010407 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010408 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010409 processPosition(mapper, x3, y3);
10410 processSync(mapper);
10411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10412 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10414}
10415
arthurhungbf89a482020-04-17 17:37:55 +080010416/**
arthurhungcc7f9802020-04-30 17:55:40 +080010417 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10418 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010419 */
arthurhungcc7f9802020-04-30 17:55:40 +080010420TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010421 addConfigurationProperty("touch.deviceType", "touchScreen");
10422 prepareDisplay(DISPLAY_ORIENTATION_0);
10423 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10424 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10425
10426 NotifyMotionArgs motionArgs;
10427
10428 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010429 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10430 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010431 processPosition(mapper, x1, y1);
10432 processSync(mapper);
10433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10434 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10435 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10436
10437 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010438 processSlot(mapper, SECOND_SLOT);
10439 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010440 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010441 processSync(mapper);
10442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010443 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
10445
10446 // If the tool type of the first finger changes to MT_TOOL_PALM,
10447 // we expect to receive ACTION_POINTER_UP with cancel flag.
10448 processSlot(mapper, FIRST_SLOT);
10449 processId(mapper, FIRST_TRACKING_ID);
10450 processToolType(mapper, MT_TOOL_PALM);
10451 processSync(mapper);
10452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010453 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010454 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10455
10456 // The following MOVE events of second finger should be processed.
10457 processSlot(mapper, SECOND_SLOT);
10458 processId(mapper, SECOND_TRACKING_ID);
10459 processPosition(mapper, x2 + 1, y2 + 1);
10460 processSync(mapper);
10461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10463 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10464
10465 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10466 // it. Second finger receive move.
10467 processSlot(mapper, FIRST_SLOT);
10468 processId(mapper, INVALID_TRACKING_ID);
10469 processSync(mapper);
10470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10472 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10473
10474 // Second finger keeps moving.
10475 processSlot(mapper, SECOND_SLOT);
10476 processId(mapper, SECOND_TRACKING_ID);
10477 processPosition(mapper, x2 + 2, y2 + 2);
10478 processSync(mapper);
10479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10481 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10482
10483 // Second finger up.
10484 processId(mapper, INVALID_TRACKING_ID);
10485 processSync(mapper);
10486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10487 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10488 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10489}
10490
10491/**
10492 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10493 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10494 */
10495TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10496 addConfigurationProperty("touch.deviceType", "touchScreen");
10497 prepareDisplay(DISPLAY_ORIENTATION_0);
10498 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10499 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10500
10501 NotifyMotionArgs motionArgs;
10502
10503 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10504 // First finger down.
10505 processId(mapper, FIRST_TRACKING_ID);
10506 processPosition(mapper, x1, y1);
10507 processSync(mapper);
10508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10509 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10511
10512 // Second finger down.
10513 processSlot(mapper, SECOND_SLOT);
10514 processId(mapper, SECOND_TRACKING_ID);
10515 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010516 processSync(mapper);
10517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010518 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +080010519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10520
arthurhungcc7f9802020-04-30 17:55:40 +080010521 // If the tool type of the first finger changes to MT_TOOL_PALM,
10522 // we expect to receive ACTION_POINTER_UP with cancel flag.
10523 processSlot(mapper, FIRST_SLOT);
10524 processId(mapper, FIRST_TRACKING_ID);
10525 processToolType(mapper, MT_TOOL_PALM);
10526 processSync(mapper);
10527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010528 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010529 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10530
10531 // Second finger keeps moving.
10532 processSlot(mapper, SECOND_SLOT);
10533 processId(mapper, SECOND_TRACKING_ID);
10534 processPosition(mapper, x2 + 1, y2 + 1);
10535 processSync(mapper);
10536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10538
10539 // second finger becomes palm, receive cancel due to only 1 finger is active.
10540 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010541 processToolType(mapper, MT_TOOL_PALM);
10542 processSync(mapper);
10543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10544 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10545
arthurhungcc7f9802020-04-30 17:55:40 +080010546 // third finger down.
10547 processSlot(mapper, THIRD_SLOT);
10548 processId(mapper, THIRD_TRACKING_ID);
10549 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010550 processPosition(mapper, x3, y3);
10551 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10553 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010555 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10556
10557 // third finger move
10558 processId(mapper, THIRD_TRACKING_ID);
10559 processPosition(mapper, x3 + 1, y3 + 1);
10560 processSync(mapper);
10561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10563
10564 // first finger up, third finger receive move.
10565 processSlot(mapper, FIRST_SLOT);
10566 processId(mapper, INVALID_TRACKING_ID);
10567 processSync(mapper);
10568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10570 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10571
10572 // second finger up, third finger receive move.
10573 processSlot(mapper, SECOND_SLOT);
10574 processId(mapper, INVALID_TRACKING_ID);
10575 processSync(mapper);
10576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10578 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10579
10580 // third finger up.
10581 processSlot(mapper, THIRD_SLOT);
10582 processId(mapper, INVALID_TRACKING_ID);
10583 processSync(mapper);
10584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10585 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10586 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10587}
10588
10589/**
10590 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10591 * and the active finger could still be allowed to receive the events
10592 */
10593TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10594 addConfigurationProperty("touch.deviceType", "touchScreen");
10595 prepareDisplay(DISPLAY_ORIENTATION_0);
10596 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
10597 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10598
10599 NotifyMotionArgs motionArgs;
10600
10601 // default tool type is finger
10602 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10603 processId(mapper, FIRST_TRACKING_ID);
10604 processPosition(mapper, x1, y1);
10605 processSync(mapper);
10606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10607 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10609
10610 // Second finger down.
10611 processSlot(mapper, SECOND_SLOT);
10612 processId(mapper, SECOND_TRACKING_ID);
10613 processPosition(mapper, x2, y2);
10614 processSync(mapper);
10615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010616 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010617 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10618
10619 // If the tool type of the second finger changes to MT_TOOL_PALM,
10620 // we expect to receive ACTION_POINTER_UP with cancel flag.
10621 processId(mapper, SECOND_TRACKING_ID);
10622 processToolType(mapper, MT_TOOL_PALM);
10623 processSync(mapper);
10624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010625 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010626 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10627
10628 // The following MOVE event should be processed.
10629 processSlot(mapper, FIRST_SLOT);
10630 processId(mapper, FIRST_TRACKING_ID);
10631 processPosition(mapper, x1 + 1, y1 + 1);
10632 processSync(mapper);
10633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10635 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10636
10637 // second finger up.
10638 processSlot(mapper, SECOND_SLOT);
10639 processId(mapper, INVALID_TRACKING_ID);
10640 processSync(mapper);
10641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10643
10644 // first finger keep moving
10645 processSlot(mapper, FIRST_SLOT);
10646 processId(mapper, FIRST_TRACKING_ID);
10647 processPosition(mapper, x1 + 2, y1 + 2);
10648 processSync(mapper);
10649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10651
10652 // first finger up.
10653 processId(mapper, INVALID_TRACKING_ID);
10654 processSync(mapper);
10655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10656 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10657 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010658}
10659
Arthur Hung9ad18942021-06-19 02:04:46 +000010660/**
10661 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10662 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10663 * cause slot be valid again.
10664 */
10665TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10666 addConfigurationProperty("touch.deviceType", "touchScreen");
10667 prepareDisplay(DISPLAY_ORIENTATION_0);
10668 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10669 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10670
10671 NotifyMotionArgs motionArgs;
10672
10673 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10674 // First finger down.
10675 processId(mapper, FIRST_TRACKING_ID);
10676 processPosition(mapper, x1, y1);
10677 processPressure(mapper, RAW_PRESSURE_MAX);
10678 processSync(mapper);
10679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10680 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10681 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10682
10683 // First finger move.
10684 processId(mapper, FIRST_TRACKING_ID);
10685 processPosition(mapper, x1 + 1, y1 + 1);
10686 processPressure(mapper, RAW_PRESSURE_MAX);
10687 processSync(mapper);
10688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10689 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10690 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10691
10692 // Second finger down.
10693 processSlot(mapper, SECOND_SLOT);
10694 processId(mapper, SECOND_TRACKING_ID);
10695 processPosition(mapper, x2, y2);
10696 processPressure(mapper, RAW_PRESSURE_MAX);
10697 processSync(mapper);
10698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010699 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010700 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10701
10702 // second finger up with some unexpected data.
10703 processSlot(mapper, SECOND_SLOT);
10704 processId(mapper, INVALID_TRACKING_ID);
10705 processPosition(mapper, x2, y2);
10706 processSync(mapper);
10707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010708 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +000010709 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
10710
10711 // first finger up with some unexpected data.
10712 processSlot(mapper, FIRST_SLOT);
10713 processId(mapper, INVALID_TRACKING_ID);
10714 processPosition(mapper, x2, y2);
10715 processPressure(mapper, RAW_PRESSURE_MAX);
10716 processSync(mapper);
10717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10718 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10719 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
10720}
10721
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010722TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10723 addConfigurationProperty("touch.deviceType", "touchScreen");
10724 prepareDisplay(DISPLAY_ORIENTATION_0);
10725 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10726 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10727
10728 // First finger down.
10729 processId(mapper, FIRST_TRACKING_ID);
10730 processPosition(mapper, 100, 200);
10731 processPressure(mapper, RAW_PRESSURE_MAX);
10732 processSync(mapper);
10733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10734 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10735
10736 // Second finger down.
10737 processSlot(mapper, SECOND_SLOT);
10738 processId(mapper, SECOND_TRACKING_ID);
10739 processPosition(mapper, 300, 400);
10740 processPressure(mapper, RAW_PRESSURE_MAX);
10741 processSync(mapper);
10742 ASSERT_NO_FATAL_FAILURE(
10743 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10744
10745 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010746 // preserved. Resetting should cancel the ongoing gesture.
10747 resetMapper(mapper, ARBITRARY_TIME);
10748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10749 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010750
10751 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10752 // the existing touch state to generate a down event.
10753 processPosition(mapper, 301, 302);
10754 processSync(mapper);
10755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10756 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10758 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10759
10760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10761}
10762
10763TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10764 addConfigurationProperty("touch.deviceType", "touchScreen");
10765 prepareDisplay(DISPLAY_ORIENTATION_0);
10766 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10767 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10768
10769 // First finger touches down and releases.
10770 processId(mapper, FIRST_TRACKING_ID);
10771 processPosition(mapper, 100, 200);
10772 processPressure(mapper, RAW_PRESSURE_MAX);
10773 processSync(mapper);
10774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10775 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10776 processId(mapper, INVALID_TRACKING_ID);
10777 processSync(mapper);
10778 ASSERT_NO_FATAL_FAILURE(
10779 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10780
10781 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10782 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010783 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10785
10786 // Send an empty sync frame. Since there are no pointers, no events are generated.
10787 processSync(mapper);
10788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10789}
10790
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010791TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010792 addConfigurationProperty("touch.deviceType", "touchScreen");
10793 prepareDisplay(DISPLAY_ORIENTATION_0);
10794 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
10795 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010797
10798 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10799 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10800 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10801 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10802 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10803
10804 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010805 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010806 processId(mapper, FIRST_TRACKING_ID);
10807 processToolType(mapper, MT_TOOL_PEN);
10808 processPosition(mapper, 100, 200);
10809 processPressure(mapper, RAW_PRESSURE_MAX);
10810 processSync(mapper);
10811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10812 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10813 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
10814 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10815
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010816 // Now that we know the device supports styluses, ensure that the device is re-configured with
10817 // the stylus source.
10818 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10819 {
10820 const auto& devices = mReader->getInputDevices();
10821 auto deviceInfo =
10822 std::find_if(devices.begin(), devices.end(),
10823 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10824 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10825 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10826 }
10827
10828 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10830
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010831 processId(mapper, INVALID_TRACKING_ID);
10832 processSync(mapper);
10833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10834 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10835 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
10836 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010837}
10838
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010839// --- MultiTouchInputMapperTest_ExternalDevice ---
10840
10841class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10842protected:
Chris Yea52ade12020-08-27 16:49:20 -070010843 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010844};
10845
10846/**
10847 * Expect fallback to internal viewport if device is external and external viewport is not present.
10848 */
10849TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10850 prepareAxes(POSITION);
10851 addConfigurationProperty("touch.deviceType", "touchScreen");
10852 prepareDisplay(DISPLAY_ORIENTATION_0);
10853 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10854
10855 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10856
10857 NotifyMotionArgs motionArgs;
10858
10859 // Expect the event to be sent to the internal viewport,
10860 // because an external viewport is not present.
10861 processPosition(mapper, 100, 100);
10862 processSync(mapper);
10863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10864 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10865
10866 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010867 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010868 processPosition(mapper, 100, 100);
10869 processSync(mapper);
10870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10871 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10872}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010873
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010874TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10875 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10876 std::shared_ptr<FakePointerController> fakePointerController =
10877 std::make_shared<FakePointerController>();
10878 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10879 fakePointerController->setPosition(0, 0);
10880 fakePointerController->setButtonState(0);
10881
10882 // prepare device and capture
10883 prepareDisplay(DISPLAY_ORIENTATION_0);
10884 prepareAxes(POSITION | ID | SLOT);
10885 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10886 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10887 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010888 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010889 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10890
10891 // captured touchpad should be a touchpad source
10892 NotifyDeviceResetArgs resetArgs;
10893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10894 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10895
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010896 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010897
10898 const InputDeviceInfo::MotionRange* relRangeX =
10899 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10900 ASSERT_NE(relRangeX, nullptr);
10901 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10902 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10903 const InputDeviceInfo::MotionRange* relRangeY =
10904 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10905 ASSERT_NE(relRangeY, nullptr);
10906 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10907 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10908
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010909 // run captured pointer tests - note that this is unscaled, so input listener events should be
10910 // identical to what the hardware sends (accounting for any
10911 // calibration).
10912 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010913 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010914 processId(mapper, 1);
10915 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10916 processKey(mapper, BTN_TOUCH, 1);
10917 processSync(mapper);
10918
10919 // expect coord[0] to contain initial location of touch 0
10920 NotifyMotionArgs args;
10921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10922 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10923 ASSERT_EQ(1U, args.pointerCount);
10924 ASSERT_EQ(0, args.pointerProperties[0].id);
10925 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10926 ASSERT_NO_FATAL_FAILURE(
10927 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10928
10929 // FINGER 1 DOWN
10930 processSlot(mapper, 1);
10931 processId(mapper, 2);
10932 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10933 processSync(mapper);
10934
10935 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010937 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010938 ASSERT_EQ(2U, args.pointerCount);
10939 ASSERT_EQ(0, args.pointerProperties[0].id);
10940 ASSERT_EQ(1, args.pointerProperties[1].id);
10941 ASSERT_NO_FATAL_FAILURE(
10942 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10943 ASSERT_NO_FATAL_FAILURE(
10944 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10945
10946 // FINGER 1 MOVE
10947 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10948 processSync(mapper);
10949
10950 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10951 // from move
10952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10954 ASSERT_NO_FATAL_FAILURE(
10955 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10956 ASSERT_NO_FATAL_FAILURE(
10957 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10958
10959 // FINGER 0 MOVE
10960 processSlot(mapper, 0);
10961 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10962 processSync(mapper);
10963
10964 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10967 ASSERT_NO_FATAL_FAILURE(
10968 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10969 ASSERT_NO_FATAL_FAILURE(
10970 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10971
10972 // BUTTON DOWN
10973 processKey(mapper, BTN_LEFT, 1);
10974 processSync(mapper);
10975
10976 // touchinputmapper design sends a move before button press
10977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10981
10982 // BUTTON UP
10983 processKey(mapper, BTN_LEFT, 0);
10984 processSync(mapper);
10985
10986 // touchinputmapper design sends a move after button release
10987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10991
10992 // FINGER 0 UP
10993 processId(mapper, -1);
10994 processSync(mapper);
10995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10996 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10997
10998 // FINGER 1 MOVE
10999 processSlot(mapper, 1);
11000 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
11001 processSync(mapper);
11002
11003 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
11004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11006 ASSERT_EQ(1U, args.pointerCount);
11007 ASSERT_EQ(1, args.pointerProperties[0].id);
11008 ASSERT_NO_FATAL_FAILURE(
11009 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
11010
11011 // FINGER 1 UP
11012 processId(mapper, -1);
11013 processKey(mapper, BTN_TOUCH, 0);
11014 processSync(mapper);
11015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11016 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
11017
Harry Cutts16a24cc2022-10-26 15:22:19 +000011018 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011019 mFakePolicy->setPointerCapture(false);
11020 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
11021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +000011022 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011023}
11024
11025TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
11026 std::shared_ptr<FakePointerController> fakePointerController =
11027 std::make_shared<FakePointerController>();
11028 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11029 fakePointerController->setPosition(0, 0);
11030 fakePointerController->setButtonState(0);
11031
11032 // prepare device and capture
11033 prepareDisplay(DISPLAY_ORIENTATION_0);
11034 prepareAxes(POSITION | ID | SLOT);
11035 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
11036 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011037 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011038 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11039 // run uncaptured pointer tests - pushes out generic events
11040 // FINGER 0 DOWN
11041 processId(mapper, 3);
11042 processPosition(mapper, 100, 100);
11043 processKey(mapper, BTN_TOUCH, 1);
11044 processSync(mapper);
11045
11046 // start at (100,100), cursor should be at (0,0) * scale
11047 NotifyMotionArgs args;
11048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11049 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
11050 ASSERT_NO_FATAL_FAILURE(
11051 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
11052
11053 // FINGER 0 MOVE
11054 processPosition(mapper, 200, 200);
11055 processSync(mapper);
11056
11057 // compute scaling to help with touch position checking
11058 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
11059 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
11060 float scale =
11061 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
11062
11063 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
11064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11065 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
11066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
11067 0, 0, 0, 0, 0, 0, 0));
11068}
11069
11070TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
11071 std::shared_ptr<FakePointerController> fakePointerController =
11072 std::make_shared<FakePointerController>();
11073
11074 prepareDisplay(DISPLAY_ORIENTATION_0);
11075 prepareAxes(POSITION | ID | SLOT);
11076 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011077 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011078 mFakePolicy->setPointerCapture(false);
11079 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11080
Harry Cutts16a24cc2022-10-26 15:22:19 +000011081 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
11082 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011083
Harry Cutts16a24cc2022-10-26 15:22:19 +000011084 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011085 mFakePolicy->setPointerCapture(true);
11086 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
11087 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
11088}
11089
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000011090// --- BluetoothMultiTouchInputMapperTest ---
11091
11092class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
11093protected:
11094 void SetUp() override {
11095 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
11096 }
11097};
11098
11099TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
11100 addConfigurationProperty("touch.deviceType", "touchScreen");
11101 prepareDisplay(DISPLAY_ORIENTATION_0);
11102 prepareAxes(POSITION | ID | SLOT | PRESSURE);
11103 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11104
11105 nsecs_t kernelEventTime = ARBITRARY_TIME;
11106 nsecs_t expectedEventTime = ARBITRARY_TIME;
11107 // Touch down.
11108 processId(mapper, FIRST_TRACKING_ID);
11109 processPosition(mapper, 100, 200);
11110 processPressure(mapper, RAW_PRESSURE_MAX);
11111 processSync(mapper, ARBITRARY_TIME);
11112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11113 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
11114
11115 // Process several events that come in quick succession, according to their timestamps.
11116 for (int i = 0; i < 3; i++) {
11117 constexpr static nsecs_t delta = ms2ns(1);
11118 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
11119 kernelEventTime += delta;
11120 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
11121
11122 processPosition(mapper, 101 + i, 201 + i);
11123 processSync(mapper, kernelEventTime);
11124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11125 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11126 WithEventTime(expectedEventTime))));
11127 }
11128
11129 // Release the touch.
11130 processId(mapper, INVALID_TRACKING_ID);
11131 processPressure(mapper, RAW_PRESSURE_MIN);
11132 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
11133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
11135 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
11136}
11137
11138// --- MultiTouchPointerModeTest ---
11139
HQ Liue6983c72022-04-19 22:14:56 +000011140class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
11141protected:
11142 float mPointerMovementScale;
11143 float mPointerXZoomScale;
11144 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
11145 addConfigurationProperty("touch.deviceType", "pointer");
11146 std::shared_ptr<FakePointerController> fakePointerController =
11147 std::make_shared<FakePointerController>();
11148 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11149 fakePointerController->setPosition(0, 0);
11150 fakePointerController->setButtonState(0);
11151 prepareDisplay(DISPLAY_ORIENTATION_0);
11152
11153 prepareAxes(POSITION);
11154 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
11155 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
11156 // needs to be disabled, and the pointer gesture needs to be enabled.
11157 mFakePolicy->setPointerCapture(false);
11158 mFakePolicy->setPointerGestureEnabled(true);
11159 mFakePolicy->setPointerController(fakePointerController);
11160
11161 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
11162 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
11163 mPointerMovementScale =
11164 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
11165 mPointerXZoomScale =
11166 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
11167 }
11168
11169 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
11170 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
11171 /*flat*/ 0,
11172 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
11173 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
11174 /*flat*/ 0,
11175 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
11176 }
11177};
11178
11179/**
11180 * Two fingers down on a pointer mode touch pad. The width
11181 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
11182 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
11183 * be greater than the both value to be freeform gesture, so that after two
11184 * fingers start to move downwards, the gesture should be swipe.
11185 */
11186TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
11187 // The min freeform gesture width is 25units/mm x 30mm = 750
11188 // which is greater than fraction of the diagnal length of the touchpad (349).
11189 // Thus, MaxSwipWidth is 750.
11190 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11191 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11192 NotifyMotionArgs motionArgs;
11193
11194 // Two fingers down at once.
11195 // The two fingers are 450 units apart, expects the current gesture to be PRESS
11196 // Pointer's initial position is used the [0,0] coordinate.
11197 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11198
11199 processId(mapper, FIRST_TRACKING_ID);
11200 processPosition(mapper, x1, y1);
11201 processMTSync(mapper);
11202 processId(mapper, SECOND_TRACKING_ID);
11203 processPosition(mapper, x2, y2);
11204 processMTSync(mapper);
11205 processSync(mapper);
11206
11207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11208 ASSERT_EQ(1U, motionArgs.pointerCount);
11209 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11210 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011211 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011212 ASSERT_NO_FATAL_FAILURE(
11213 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11214
11215 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11216 // that there should be 1 pointer.
11217 int32_t movingDistance = 200;
11218 y1 += movingDistance;
11219 y2 += movingDistance;
11220
11221 processId(mapper, FIRST_TRACKING_ID);
11222 processPosition(mapper, x1, y1);
11223 processMTSync(mapper);
11224 processId(mapper, SECOND_TRACKING_ID);
11225 processPosition(mapper, x2, y2);
11226 processMTSync(mapper);
11227 processSync(mapper);
11228
11229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11230 ASSERT_EQ(1U, motionArgs.pointerCount);
11231 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11232 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011233 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11235 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11236 0, 0, 0, 0));
11237}
11238
11239/**
11240 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
11241 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
11242 * the touch pack diagnal length. Two fingers' distance must be greater than the both
11243 * value to be freeform gesture, so that after two fingers start to move downwards,
11244 * the gesture should be swipe.
11245 */
11246TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
11247 // The min freeform gesture width is 5units/mm x 30mm = 150
11248 // which is greater than fraction of the diagnal length of the touchpad (349).
11249 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
11250 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
11251 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11252 NotifyMotionArgs motionArgs;
11253
11254 // Two fingers down at once.
11255 // The two fingers are 250 units apart, expects the current gesture to be PRESS
11256 // Pointer's initial position is used the [0,0] coordinate.
11257 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
11258
11259 processId(mapper, FIRST_TRACKING_ID);
11260 processPosition(mapper, x1, y1);
11261 processMTSync(mapper);
11262 processId(mapper, SECOND_TRACKING_ID);
11263 processPosition(mapper, x2, y2);
11264 processMTSync(mapper);
11265 processSync(mapper);
11266
11267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11268 ASSERT_EQ(1U, motionArgs.pointerCount);
11269 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11270 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011271 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011272 ASSERT_NO_FATAL_FAILURE(
11273 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11274
11275 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11276 // and there should be 1 pointer.
11277 int32_t movingDistance = 200;
11278 y1 += movingDistance;
11279 y2 += movingDistance;
11280
11281 processId(mapper, FIRST_TRACKING_ID);
11282 processPosition(mapper, x1, y1);
11283 processMTSync(mapper);
11284 processId(mapper, SECOND_TRACKING_ID);
11285 processPosition(mapper, x2, y2);
11286 processMTSync(mapper);
11287 processSync(mapper);
11288
11289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11290 ASSERT_EQ(1U, motionArgs.pointerCount);
11291 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11292 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011293 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011294 // New coordinate is the scaled relative coordinate from the initial coordinate.
11295 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11296 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11297 0, 0, 0, 0));
11298}
11299
11300/**
11301 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11302 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11303 * freeform gestures after two fingers start to move downwards.
11304 */
11305TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
11306 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11307 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11308
11309 NotifyMotionArgs motionArgs;
11310
11311 // Two fingers down at once. Wider than the max swipe width.
11312 // The gesture is expected to be PRESS, then transformed to FREEFORM
11313 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11314
11315 processId(mapper, FIRST_TRACKING_ID);
11316 processPosition(mapper, x1, y1);
11317 processMTSync(mapper);
11318 processId(mapper, SECOND_TRACKING_ID);
11319 processPosition(mapper, x2, y2);
11320 processMTSync(mapper);
11321 processSync(mapper);
11322
11323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11324 ASSERT_EQ(1U, motionArgs.pointerCount);
11325 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11326 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011327 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011328 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11329 ASSERT_NO_FATAL_FAILURE(
11330 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11331
11332 int32_t movingDistance = 200;
11333
11334 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11335 // then two down events for two pointers.
11336 y1 += movingDistance;
11337 y2 += movingDistance;
11338
11339 processId(mapper, FIRST_TRACKING_ID);
11340 processPosition(mapper, x1, y1);
11341 processMTSync(mapper);
11342 processId(mapper, SECOND_TRACKING_ID);
11343 processPosition(mapper, x2, y2);
11344 processMTSync(mapper);
11345 processSync(mapper);
11346
11347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11348 // The previous PRESS gesture is cancelled, because it is transformed to freeform
11349 ASSERT_EQ(1U, motionArgs.pointerCount);
11350 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
11353 ASSERT_EQ(1U, motionArgs.pointerCount);
11354 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011357 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011358 ASSERT_EQ(2U, motionArgs.pointerCount);
11359 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
11360 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011361 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011362 // Two pointers' scaled relative coordinates from their initial centroid.
11363 // Initial y coordinates are 0 as y1 and y2 have the same value.
11364 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11365 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11366 // When pointers move, the new coordinates equal to the initial coordinates plus
11367 // scaled moving distance.
11368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11369 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11370 0, 0, 0, 0));
11371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11372 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11373 0, 0, 0, 0));
11374
11375 // Move two fingers down again, expect one MOVE motion event.
11376 y1 += movingDistance;
11377 y2 += movingDistance;
11378
11379 processId(mapper, FIRST_TRACKING_ID);
11380 processPosition(mapper, x1, y1);
11381 processMTSync(mapper);
11382 processId(mapper, SECOND_TRACKING_ID);
11383 processPosition(mapper, x2, y2);
11384 processMTSync(mapper);
11385 processSync(mapper);
11386
11387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11388 ASSERT_EQ(2U, motionArgs.pointerCount);
11389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011391 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11393 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11394 0, 0, 0, 0, 0));
11395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11396 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11397 0, 0, 0, 0, 0));
11398}
11399
Harry Cutts39b7ca22022-10-05 15:55:48 +000011400TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
11401 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11402 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11403 NotifyMotionArgs motionArgs;
11404
11405 // Place two fingers down.
11406 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11407
11408 processId(mapper, FIRST_TRACKING_ID);
11409 processPosition(mapper, x1, y1);
11410 processMTSync(mapper);
11411 processId(mapper, SECOND_TRACKING_ID);
11412 processPosition(mapper, x2, y2);
11413 processMTSync(mapper);
11414 processSync(mapper);
11415
11416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11417 ASSERT_EQ(1U, motionArgs.pointerCount);
11418 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11419 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11420 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11421 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11422
11423 // Move the two fingers down and to the left.
11424 int32_t movingDistance = 200;
11425 x1 -= movingDistance;
11426 y1 += movingDistance;
11427 x2 -= movingDistance;
11428 y2 += movingDistance;
11429
11430 processId(mapper, FIRST_TRACKING_ID);
11431 processPosition(mapper, x1, y1);
11432 processMTSync(mapper);
11433 processId(mapper, SECOND_TRACKING_ID);
11434 processPosition(mapper, x2, y2);
11435 processMTSync(mapper);
11436 processSync(mapper);
11437
11438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11439 ASSERT_EQ(1U, motionArgs.pointerCount);
11440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11441 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11442 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11443 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11444}
11445
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011446TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
11447 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
11448 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11449 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
11450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11451
11452 // Start a stylus gesture.
11453 processKey(mapper, BTN_TOOL_PEN, 1);
11454 processId(mapper, FIRST_TRACKING_ID);
11455 processPosition(mapper, 100, 200);
11456 processSync(mapper);
11457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11458 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11459 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11460 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11461 // TODO(b/257078296): Pointer mode generates extra event.
11462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11463 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11464 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11465 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11467
11468 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11469 // gesture should be disabled.
11470 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11471 viewport->isActive = false;
11472 mFakePolicy->updateViewport(*viewport);
11473 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
11474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11475 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11476 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11477 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11478 // TODO(b/257078296): Pointer mode generates extra event.
11479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11480 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11481 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
11482 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
11483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11484}
11485
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011486// --- JoystickInputMapperTest ---
11487
11488class JoystickInputMapperTest : public InputMapperTest {
11489protected:
11490 static const int32_t RAW_X_MIN;
11491 static const int32_t RAW_X_MAX;
11492 static const int32_t RAW_Y_MIN;
11493 static const int32_t RAW_Y_MAX;
11494
11495 void SetUp() override {
11496 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11497 }
11498 void prepareAxes() {
11499 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11500 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11501 }
11502
11503 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11505 }
11506
11507 void processSync(JoystickInputMapper& mapper) {
11508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11509 }
11510
11511 void prepareVirtualDisplay(int32_t orientation) {
11512 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11513 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11514 NO_PORT, ViewportType::VIRTUAL);
11515 }
11516};
11517
11518const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11519const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11520const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11521const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11522
11523TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11524 prepareAxes();
11525 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
11526
11527 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11528
11529 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
11530
11531 // Send an axis event
11532 processAxis(mapper, ABS_X, 100);
11533 processSync(mapper);
11534
11535 NotifyMotionArgs args;
11536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11537 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11538
11539 // Send another axis event
11540 processAxis(mapper, ABS_Y, 100);
11541 processSync(mapper);
11542
11543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11544 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11545}
11546
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011547// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011548
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011549class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011550protected:
11551 static const char* DEVICE_NAME;
11552 static const char* DEVICE_LOCATION;
11553 static const int32_t DEVICE_ID;
11554 static const int32_t DEVICE_GENERATION;
11555 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011556 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011557 static const int32_t EVENTHUB_ID;
11558
11559 std::shared_ptr<FakeEventHub> mFakeEventHub;
11560 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011561 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011562 std::unique_ptr<InstrumentedInputReader> mReader;
11563 std::shared_ptr<InputDevice> mDevice;
11564
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011565 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011566 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011567 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011568 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011569 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011570 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011571 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11572 }
11573
11574 void SetUp() override { SetUp(DEVICE_CLASSES); }
11575
11576 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011577 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011578 mFakePolicy.clear();
11579 }
11580
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011581 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011582 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
11583 mReader->requestRefreshConfiguration(changes);
11584 mReader->loopOnce();
11585 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070011586 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011587 }
11588
11589 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11590 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011591 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011592 InputDeviceIdentifier identifier;
11593 identifier.name = name;
11594 identifier.location = location;
11595 std::shared_ptr<InputDevice> device =
11596 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11597 identifier);
11598 mReader->pushNextDevice(device);
11599 mFakeEventHub->addDevice(eventHubId, name, classes);
11600 mReader->loopOnce();
11601 return device;
11602 }
11603
11604 template <class T, typename... Args>
11605 T& addControllerAndConfigure(Args... args) {
11606 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11607
11608 return controller;
11609 }
11610};
11611
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011612const char* PeripheralControllerTest::DEVICE_NAME = "device";
11613const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11614const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11615const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11616const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011617const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11618 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011619const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011620
11621// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011622class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011623protected:
11624 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011625 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011626 }
11627};
11628
11629TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011630 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011631
11632 ASSERT_TRUE(controller.getBatteryCapacity(DEFAULT_BATTERY));
11633 ASSERT_EQ(controller.getBatteryCapacity(DEFAULT_BATTERY).value_or(-1), BATTERY_CAPACITY);
11634}
11635
11636TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011637 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011638
11639 ASSERT_TRUE(controller.getBatteryStatus(DEFAULT_BATTERY));
11640 ASSERT_EQ(controller.getBatteryStatus(DEFAULT_BATTERY).value_or(-1), BATTERY_STATUS);
11641}
11642
11643// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011644class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011645protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011646 void SetUp() override {
11647 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11648 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011649};
11650
Chris Ye85758332021-05-16 23:05:17 -070011651TEST_F(LightControllerTest, MonoLight) {
11652 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011653 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011654 .maxBrightness = 255,
11655 .flags = InputLightClass::BRIGHTNESS,
11656 .path = ""};
11657 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011658
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011659 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011660 InputDeviceInfo info;
11661 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011662 std::vector<InputDeviceLightInfo> lights = info.getLights();
11663 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011664 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11665 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11666
11667 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11668 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11669}
11670
11671TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11672 RawLightInfo infoMono = {.id = 1,
11673 .name = "mono_keyboard_backlight",
11674 .maxBrightness = 255,
11675 .flags = InputLightClass::BRIGHTNESS |
11676 InputLightClass::KEYBOARD_BACKLIGHT,
11677 .path = ""};
11678 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11679
11680 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11681 InputDeviceInfo info;
11682 controller.populateDeviceInfo(&info);
11683 std::vector<InputDeviceLightInfo> lights = info.getLights();
11684 ASSERT_EQ(1U, lights.size());
11685 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11686 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011687
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011688 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11689 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011690}
11691
11692TEST_F(LightControllerTest, RGBLight) {
11693 RawLightInfo infoRed = {.id = 1,
11694 .name = "red",
11695 .maxBrightness = 255,
11696 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11697 .path = ""};
11698 RawLightInfo infoGreen = {.id = 2,
11699 .name = "green",
11700 .maxBrightness = 255,
11701 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11702 .path = ""};
11703 RawLightInfo infoBlue = {.id = 3,
11704 .name = "blue",
11705 .maxBrightness = 255,
11706 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11707 .path = ""};
11708 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11709 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11710 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11711
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011712 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011713 InputDeviceInfo info;
11714 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011715 std::vector<InputDeviceLightInfo> lights = info.getLights();
11716 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011717 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11718 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11719 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11720
11721 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11722 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11723}
11724
11725TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
11726 RawLightInfo infoRed = {.id = 1,
11727 .name = "red_keyboard_backlight",
11728 .maxBrightness = 255,
11729 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
11730 InputLightClass::KEYBOARD_BACKLIGHT,
11731 .path = ""};
11732 RawLightInfo infoGreen = {.id = 2,
11733 .name = "green_keyboard_backlight",
11734 .maxBrightness = 255,
11735 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
11736 InputLightClass::KEYBOARD_BACKLIGHT,
11737 .path = ""};
11738 RawLightInfo infoBlue = {.id = 3,
11739 .name = "blue_keyboard_backlight",
11740 .maxBrightness = 255,
11741 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
11742 InputLightClass::KEYBOARD_BACKLIGHT,
11743 .path = ""};
11744 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11745 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11746 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11747
11748 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11749 InputDeviceInfo info;
11750 controller.populateDeviceInfo(&info);
11751 std::vector<InputDeviceLightInfo> lights = info.getLights();
11752 ASSERT_EQ(1U, lights.size());
11753 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11754 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11755 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11756
11757 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11758 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11759}
11760
11761TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11762 RawLightInfo infoRed = {.id = 1,
11763 .name = "red",
11764 .maxBrightness = 255,
11765 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11766 .path = ""};
11767 RawLightInfo infoGreen = {.id = 2,
11768 .name = "green",
11769 .maxBrightness = 255,
11770 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11771 .path = ""};
11772 RawLightInfo infoBlue = {.id = 3,
11773 .name = "blue",
11774 .maxBrightness = 255,
11775 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11776 .path = ""};
11777 RawLightInfo infoGlobal = {.id = 3,
11778 .name = "global_keyboard_backlight",
11779 .maxBrightness = 255,
11780 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11781 InputLightClass::KEYBOARD_BACKLIGHT,
11782 .path = ""};
11783 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11784 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11785 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11786 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11787
11788 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11789 InputDeviceInfo info;
11790 controller.populateDeviceInfo(&info);
11791 std::vector<InputDeviceLightInfo> lights = info.getLights();
11792 ASSERT_EQ(1U, lights.size());
11793 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11794 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11795 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011796
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011797 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11798 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011799}
11800
11801TEST_F(LightControllerTest, MultiColorRGBLight) {
11802 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011803 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011804 .maxBrightness = 255,
11805 .flags = InputLightClass::BRIGHTNESS |
11806 InputLightClass::MULTI_INTENSITY |
11807 InputLightClass::MULTI_INDEX,
11808 .path = ""};
11809
11810 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11811
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011812 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011813 InputDeviceInfo info;
11814 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011815 std::vector<InputDeviceLightInfo> lights = info.getLights();
11816 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011817 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11818 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11819 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11820
11821 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11822 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11823}
11824
11825TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11826 RawLightInfo infoColor = {.id = 1,
11827 .name = "multi_color_keyboard_backlight",
11828 .maxBrightness = 255,
11829 .flags = InputLightClass::BRIGHTNESS |
11830 InputLightClass::MULTI_INTENSITY |
11831 InputLightClass::MULTI_INDEX |
11832 InputLightClass::KEYBOARD_BACKLIGHT,
11833 .path = ""};
11834
11835 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11836
11837 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11838 InputDeviceInfo info;
11839 controller.populateDeviceInfo(&info);
11840 std::vector<InputDeviceLightInfo> lights = info.getLights();
11841 ASSERT_EQ(1U, lights.size());
11842 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11843 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11844 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011845
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011846 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11847 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011848}
11849
11850TEST_F(LightControllerTest, PlayerIdLight) {
11851 RawLightInfo info1 = {.id = 1,
11852 .name = "player1",
11853 .maxBrightness = 255,
11854 .flags = InputLightClass::BRIGHTNESS,
11855 .path = ""};
11856 RawLightInfo info2 = {.id = 2,
11857 .name = "player2",
11858 .maxBrightness = 255,
11859 .flags = InputLightClass::BRIGHTNESS,
11860 .path = ""};
11861 RawLightInfo info3 = {.id = 3,
11862 .name = "player3",
11863 .maxBrightness = 255,
11864 .flags = InputLightClass::BRIGHTNESS,
11865 .path = ""};
11866 RawLightInfo info4 = {.id = 4,
11867 .name = "player4",
11868 .maxBrightness = 255,
11869 .flags = InputLightClass::BRIGHTNESS,
11870 .path = ""};
11871 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11872 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11873 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11874 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11875
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011876 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011877 InputDeviceInfo info;
11878 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011879 std::vector<InputDeviceLightInfo> lights = info.getLights();
11880 ASSERT_EQ(1U, lights.size());
11881 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011882 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11883 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011884
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011885 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11886 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11887 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011888}
11889
Michael Wrightd02c5b62014-02-10 15:10:22 -080011890} // namespace android