blob: 2beb631db3efb682045452cf297ea54be6174f9e [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>
Harry Cuttsa5b71292022-11-28 12:56:17 +000043#include "FakeEventHub.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000044#include "FakePointerController.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000045#include "TestConstants.h"
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000046#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000047#include "input/DisplayViewport.h"
48#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010049
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000050using android::hardware::input::InputDeviceCountryCode;
51
Michael Wrightd02c5b62014-02-10 15:10:22 -080052namespace android {
53
Dominik Laskowski2f01d772022-03-23 16:01:29 -070054using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000055using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070056using std::chrono_literals::operator""ms;
57
Michael Wrightd02c5b62014-02-10 15:10:22 -080058// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080059static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000060static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080061static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000062static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t DISPLAY_WIDTH = 480;
64static constexpr int32_t DISPLAY_HEIGHT = 800;
65static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
66static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
67static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070068static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070069static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080070
arthurhungcc7f9802020-04-30 17:55:40 +080071static constexpr int32_t FIRST_SLOT = 0;
72static constexpr int32_t SECOND_SLOT = 1;
73static constexpr int32_t THIRD_SLOT = 2;
74static constexpr int32_t INVALID_TRACKING_ID = -1;
75static constexpr int32_t FIRST_TRACKING_ID = 0;
76static constexpr int32_t SECOND_TRACKING_ID = 1;
77static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080078static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
79static constexpr int32_t LIGHT_COLOR = 0x7F448866;
80static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080081
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080082static constexpr int32_t ACTION_POINTER_0_DOWN =
83 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
84static constexpr int32_t ACTION_POINTER_0_UP =
85 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_1_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90
Michael Wrightd02c5b62014-02-10 15:10:22 -080091// Error tolerance for floating point assertions.
92static const float EPSILON = 0.001f;
93
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000094// Minimum timestamp separation between subsequent input events from a Bluetooth device.
95static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
96// Maximum smoothing time delta so that we don't generate events too far into the future.
97constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
98
Michael Wrightd02c5b62014-02-10 15:10:22 -080099template<typename T>
100static inline T min(T a, T b) {
101 return a < b ? a : b;
102}
103
104static inline float avg(float x, float y) {
105 return (x + y) / 2;
106}
107
Chris Ye3fdbfef2021-01-06 18:45:18 -0800108// Mapping for light color name and the light color
109const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
110 {"green", LightColor::GREEN},
111 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800112
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700113static int32_t getInverseRotation(int32_t orientation) {
114 switch (orientation) {
115 case DISPLAY_ORIENTATION_90:
116 return DISPLAY_ORIENTATION_270;
117 case DISPLAY_ORIENTATION_270:
118 return DISPLAY_ORIENTATION_90;
119 default:
120 return orientation;
121 }
122}
123
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800124static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
125 InputDeviceInfo info;
126 mapper.populateDeviceInfo(&info);
127
128 const InputDeviceInfo::MotionRange* motionRange =
129 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
130 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
131}
132
133static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
134 InputDeviceInfo info;
135 mapper.populateDeviceInfo(&info);
136
137 const InputDeviceInfo::MotionRange* motionRange =
138 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
139 ASSERT_EQ(nullptr, motionRange);
140}
141
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700142[[maybe_unused]] static void dumpReader(InputReader& reader) {
143 std::string dump;
144 reader.dump(dump);
145 std::istringstream iss(dump);
146 for (std::string line; std::getline(iss, line);) {
147 ALOGE("%s", line.c_str());
148 std::this_thread::sleep_for(std::chrono::milliseconds(1));
149 }
150}
151
Michael Wrightd02c5b62014-02-10 15:10:22 -0800152// --- FakeInputReaderPolicy ---
153
154class FakeInputReaderPolicy : public InputReaderPolicyInterface {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700155 std::mutex mLock;
156 std::condition_variable mDevicesChangedCondition;
157
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158 InputReaderConfiguration mConfig;
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000159 std::shared_ptr<FakePointerController> mPointerController;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700160 std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
161 bool mInputDevicesChanged GUARDED_BY(mLock){false};
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100162 std::vector<DisplayViewport> mViewports;
Jason Gerecke489fda82012-09-07 17:19:40 -0700163 TouchAffineTransformation transform;
Prabir Pradhanda20b172022-09-26 17:01:18 +0000164 std::optional<int32_t /*deviceId*/> mStylusGestureNotified GUARDED_BY(mLock){};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
166protected:
Chris Yea52ade12020-08-27 16:49:20 -0700167 virtual ~FakeInputReaderPolicy() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800168
169public:
170 FakeInputReaderPolicy() {
171 }
172
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700173 void assertInputDevicesChanged() {
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800174 waitForInputDevices([](bool devicesChanged) {
175 if (!devicesChanged) {
176 FAIL() << "Timed out waiting for notifyInputDevicesChanged() to be called.";
177 }
178 });
179 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700180
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800181 void assertInputDevicesNotChanged() {
182 waitForInputDevices([](bool devicesChanged) {
183 if (devicesChanged) {
184 FAIL() << "Expected notifyInputDevicesChanged() to not be called.";
185 }
186 });
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700187 }
188
Prabir Pradhanda20b172022-09-26 17:01:18 +0000189 void assertStylusGestureNotified(int32_t deviceId) {
190 std::scoped_lock lock(mLock);
191 ASSERT_TRUE(mStylusGestureNotified);
192 ASSERT_EQ(deviceId, *mStylusGestureNotified);
193 mStylusGestureNotified.reset();
194 }
195
196 void assertStylusGestureNotNotified() {
197 std::scoped_lock lock(mLock);
198 ASSERT_FALSE(mStylusGestureNotified);
199 }
200
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700201 virtual void clearViewports() {
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100202 mViewports.clear();
Siarhei Vishniakoud6343922018-07-06 23:33:37 +0100203 mConfig.setDisplayViewports(mViewports);
Santos Cordonfa5cf462017-04-05 10:37:00 -0700204 }
205
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700206 std::optional<DisplayViewport> getDisplayViewportByUniqueId(const std::string& uniqueId) const {
207 return mConfig.getDisplayViewportByUniqueId(uniqueId);
208 }
209 std::optional<DisplayViewport> getDisplayViewportByType(ViewportType type) const {
210 return mConfig.getDisplayViewportByType(type);
211 }
212
213 std::optional<DisplayViewport> getDisplayViewportByPort(uint8_t displayPort) const {
214 return mConfig.getDisplayViewportByPort(displayPort);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700215 }
216
Prabir Pradhan5632d622021-09-06 07:57:20 -0700217 void addDisplayViewport(DisplayViewport viewport) {
218 mViewports.push_back(std::move(viewport));
219 mConfig.setDisplayViewports(mViewports);
220 }
221
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700222 void addDisplayViewport(int32_t displayId, int32_t width, int32_t height, int32_t orientation,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000223 bool isActive, const std::string& uniqueId,
Prabir Pradhan5632d622021-09-06 07:57:20 -0700224 std::optional<uint8_t> physicalPort, ViewportType type) {
225 const bool isRotated =
226 (orientation == DISPLAY_ORIENTATION_90 || orientation == DISPLAY_ORIENTATION_270);
227 DisplayViewport v;
228 v.displayId = displayId;
229 v.orientation = orientation;
230 v.logicalLeft = 0;
231 v.logicalTop = 0;
232 v.logicalRight = isRotated ? height : width;
233 v.logicalBottom = isRotated ? width : height;
234 v.physicalLeft = 0;
235 v.physicalTop = 0;
236 v.physicalRight = isRotated ? height : width;
237 v.physicalBottom = isRotated ? width : height;
238 v.deviceWidth = isRotated ? height : width;
239 v.deviceHeight = isRotated ? width : height;
240 v.isActive = isActive;
241 v.uniqueId = uniqueId;
242 v.physicalPort = physicalPort;
243 v.type = type;
244
245 addDisplayViewport(v);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246 }
247
Arthur Hung6cd19a42019-08-30 19:04:12 +0800248 bool updateViewport(const DisplayViewport& viewport) {
249 size_t count = mViewports.size();
250 for (size_t i = 0; i < count; i++) {
251 const DisplayViewport& currentViewport = mViewports[i];
252 if (currentViewport.displayId == viewport.displayId) {
253 mViewports[i] = viewport;
254 mConfig.setDisplayViewports(mViewports);
255 return true;
256 }
257 }
258 // no viewport found.
259 return false;
260 }
261
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100262 void addExcludedDeviceName(const std::string& deviceName) {
263 mConfig.excludedDeviceNames.push_back(deviceName);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800264 }
265
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700266 void addInputPortAssociation(const std::string& inputPort, uint8_t displayPort) {
267 mConfig.portAssociations.insert({inputPort, displayPort});
268 }
269
Christine Franks1ba71cc2021-04-07 14:37:42 -0700270 void addInputUniqueIdAssociation(const std::string& inputUniqueId,
271 const std::string& displayUniqueId) {
272 mConfig.uniqueIdAssociations.insert({inputUniqueId, displayUniqueId});
273 }
274
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000275 void addDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.insert(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700276
Siarhei Vishniakouc6f61192019-07-23 18:12:31 +0000277 void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700278
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000279 void setPointerController(std::shared_ptr<FakePointerController> controller) {
280 mPointerController = std::move(controller);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 }
282
283 const InputReaderConfiguration* getReaderConfiguration() const {
284 return &mConfig;
285 }
286
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800287 const std::vector<InputDeviceInfo>& getInputDevices() const {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288 return mInputDevices;
289 }
290
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100291 TouchAffineTransformation getTouchAffineTransformation(const std::string& inputDeviceDescriptor,
Jason Gerecke71b16e82014-03-10 09:47:59 -0700292 int32_t surfaceRotation) {
Jason Gerecke489fda82012-09-07 17:19:40 -0700293 return transform;
294 }
295
296 void setTouchAffineTransformation(const TouchAffineTransformation t) {
297 transform = t;
Jason Gerecke12d6baa2014-01-27 18:34:20 -0800298 }
299
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000300 PointerCaptureRequest setPointerCapture(bool enabled) {
301 mConfig.pointerCaptureRequest = {enabled, mNextPointerCaptureSequenceNumber++};
302 return mConfig.pointerCaptureRequest;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -0800303 }
304
Arthur Hung7c645402019-01-25 17:45:42 +0800305 void setShowTouches(bool enabled) {
306 mConfig.showTouches = enabled;
307 }
308
Garfield Tan888a6a42020-01-09 11:39:16 -0800309 void setDefaultPointerDisplayId(int32_t pointerDisplayId) {
310 mConfig.defaultPointerDisplayId = pointerDisplayId;
311 }
312
HQ Liue6983c72022-04-19 22:14:56 +0000313 void setPointerGestureEnabled(bool enabled) { mConfig.pointerGesturesEnabled = enabled; }
314
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -0800315 float getPointerGestureMovementSpeedRatio() { return mConfig.pointerGestureMovementSpeedRatio; }
316
HQ Liue6983c72022-04-19 22:14:56 +0000317 float getPointerGestureZoomSpeedRatio() { return mConfig.pointerGestureZoomSpeedRatio; }
318
Prabir Pradhanf99d6e72022-04-21 15:28:35 +0000319 void setVelocityControlParams(const VelocityControlParameters& params) {
320 mConfig.pointerVelocityControlParameters = params;
321 mConfig.wheelVelocityControlParameters = params;
322 }
323
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324private:
Prabir Pradhan5cc1a692021-08-06 14:01:18 +0000325 uint32_t mNextPointerCaptureSequenceNumber = 0;
326
Chris Yea52ade12020-08-27 16:49:20 -0700327 void getReaderConfiguration(InputReaderConfiguration* outConfig) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 *outConfig = mConfig;
329 }
330
Prabir Pradhan2853b7a2021-08-23 14:08:51 +0000331 std::shared_ptr<PointerControllerInterface> obtainPointerController(
332 int32_t /*deviceId*/) override {
333 return mPointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 }
335
Chris Yea52ade12020-08-27 16:49:20 -0700336 void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700337 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 mInputDevices = inputDevices;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700339 mInputDevicesChanged = true;
340 mDevicesChangedCondition.notify_all();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800341 }
342
Chris Yea52ade12020-08-27 16:49:20 -0700343 std::shared_ptr<KeyCharacterMap> getKeyboardLayoutOverlay(
344 const InputDeviceIdentifier&) override {
Yi Kong9b14ac62018-07-17 13:48:38 -0700345 return nullptr;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346 }
347
Chris Yea52ade12020-08-27 16:49:20 -0700348 std::string getDeviceAlias(const InputDeviceIdentifier&) override { return ""; }
Prabir Pradhan1aed8582019-12-30 11:46:51 -0800349
350 void waitForInputDevices(std::function<void(bool)> processDevicesChanged) {
351 std::unique_lock<std::mutex> lock(mLock);
352 base::ScopedLockAssertion assumeLocked(mLock);
353
354 const bool devicesChanged =
355 mDevicesChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
356 return mInputDevicesChanged;
357 });
358 ASSERT_NO_FATAL_FAILURE(processDevicesChanged(devicesChanged));
359 mInputDevicesChanged = false;
360 }
Prabir Pradhanda20b172022-09-26 17:01:18 +0000361
362 void notifyStylusGestureStarted(int32_t deviceId, nsecs_t eventTime) override {
363 std::scoped_lock<std::mutex> lock(mLock);
364 mStylusGestureNotified = deviceId;
365 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800366};
367
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368// --- FakeInputMapper ---
369
370class FakeInputMapper : public InputMapper {
371 uint32_t mSources;
372 int32_t mKeyboardType;
373 int32_t mMetaState;
374 KeyedVector<int32_t, int32_t> mKeyCodeStates;
375 KeyedVector<int32_t, int32_t> mScanCodeStates;
376 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100377 // fake mapping which would normally come from keyCharacterMap
378 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800379 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800380
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700381 std::mutex mLock;
382 std::condition_variable mStateChangedCondition;
383 bool mConfigureWasCalled GUARDED_BY(mLock);
384 bool mResetWasCalled GUARDED_BY(mLock);
385 bool mProcessWasCalled GUARDED_BY(mLock);
386 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800387
Arthur Hungc23540e2018-11-29 20:42:11 +0800388 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800389public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800390 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
391 : InputMapper(deviceContext),
392 mSources(sources),
393 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800394 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800395 mConfigureWasCalled(false),
396 mResetWasCalled(false),
397 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800398
Chris Yea52ade12020-08-27 16:49:20 -0700399 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800400
401 void setKeyboardType(int32_t keyboardType) {
402 mKeyboardType = keyboardType;
403 }
404
405 void setMetaState(int32_t metaState) {
406 mMetaState = metaState;
407 }
408
409 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700410 std::unique_lock<std::mutex> lock(mLock);
411 base::ScopedLockAssertion assumeLocked(mLock);
412 const bool configureCalled =
413 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
414 return mConfigureWasCalled;
415 });
416 if (!configureCalled) {
417 FAIL() << "Expected configure() to have been called.";
418 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800419 mConfigureWasCalled = false;
420 }
421
422 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700423 std::unique_lock<std::mutex> lock(mLock);
424 base::ScopedLockAssertion assumeLocked(mLock);
425 const bool resetCalled =
426 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
427 return mResetWasCalled;
428 });
429 if (!resetCalled) {
430 FAIL() << "Expected reset() to have been called.";
431 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800432 mResetWasCalled = false;
433 }
434
Yi Kong9b14ac62018-07-17 13:48:38 -0700435 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700436 std::unique_lock<std::mutex> lock(mLock);
437 base::ScopedLockAssertion assumeLocked(mLock);
438 const bool processCalled =
439 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
440 return mProcessWasCalled;
441 });
442 if (!processCalled) {
443 FAIL() << "Expected process() to have been called.";
444 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800445 if (outLastEvent) {
446 *outLastEvent = mLastEvent;
447 }
448 mProcessWasCalled = false;
449 }
450
451 void setKeyCodeState(int32_t keyCode, int32_t state) {
452 mKeyCodeStates.replaceValueFor(keyCode, state);
453 }
454
455 void setScanCodeState(int32_t scanCode, int32_t state) {
456 mScanCodeStates.replaceValueFor(scanCode, state);
457 }
458
459 void setSwitchState(int32_t switchCode, int32_t state) {
460 mSwitchStates.replaceValueFor(switchCode, state);
461 }
462
463 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800464 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800465 }
466
Philip Junker4af3b3d2021-12-14 10:36:55 +0100467 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
468 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
469 }
470
Michael Wrightd02c5b62014-02-10 15:10:22 -0800471private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100472 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800473
Chris Yea52ade12020-08-27 16:49:20 -0700474 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800475 InputMapper::populateDeviceInfo(deviceInfo);
476
477 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
478 deviceInfo->setKeyboardType(mKeyboardType);
479 }
480 }
481
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700482 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
483 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700484 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800485 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800486
487 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800488 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800489 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
490 mViewport = config->getDisplayViewportByPort(*displayPort);
491 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700492
493 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700494 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800495 }
496
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700497 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700498 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800499 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700500 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700501 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800502 }
503
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700504 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700505 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800506 mLastEvent = *rawEvent;
507 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700508 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700509 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800510 }
511
Chris Yea52ade12020-08-27 16:49:20 -0700512 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
514 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
515 }
516
Philip Junker4af3b3d2021-12-14 10:36:55 +0100517 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
518 auto it = mKeyCodeMapping.find(locationKeyCode);
519 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
520 }
521
Chris Yea52ade12020-08-27 16:49:20 -0700522 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800523 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
524 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
525 }
526
Chris Yea52ade12020-08-27 16:49:20 -0700527 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800528 ssize_t index = mSwitchStates.indexOfKey(switchCode);
529 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
530 }
531
Chris Yea52ade12020-08-27 16:49:20 -0700532 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700533 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700534 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700535 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800536 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
537 if (keyCodes[i] == mSupportedKeyCodes[j]) {
538 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800539 }
540 }
541 }
Chris Yea52ade12020-08-27 16:49:20 -0700542 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800543 return result;
544 }
545
546 virtual int32_t getMetaState() {
547 return mMetaState;
548 }
549
550 virtual void fadePointer() {
551 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800552
553 virtual std::optional<int32_t> getAssociatedDisplay() {
554 if (mViewport) {
555 return std::make_optional(mViewport->displayId);
556 }
557 return std::nullopt;
558 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800559};
560
561
562// --- InstrumentedInputReader ---
563
564class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800565 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800566
567public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700568 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
569 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700570 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +0800571 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800572
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000573 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800575 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800577 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000578 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 InputDeviceIdentifier identifier;
580 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +0800581 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +0800583 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584 }
585
Prabir Pradhan28efc192019-11-05 01:10:04 +0000586 // Make the protected loopOnce method accessible to tests.
587 using InputReader::loopOnce;
588
Michael Wrightd02c5b62014-02-10 15:10:22 -0800589protected:
Chris Ye1c2e0892020-11-30 21:41:44 -0800590 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
591 const InputDeviceIdentifier& identifier)
592 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800593 if (!mNextDevices.empty()) {
594 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
595 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596 return device;
597 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800598 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599 }
600
arthurhungdcef2dc2020-08-11 14:47:50 +0800601 // --- FakeInputReaderContext ---
602 class FakeInputReaderContext : public ContextImpl {
603 int32_t mGlobalMetaState;
604 bool mUpdateGlobalMetaStateWasCalled;
605 int32_t mGeneration;
606
607 public:
608 FakeInputReaderContext(InputReader* reader)
609 : ContextImpl(reader),
610 mGlobalMetaState(0),
611 mUpdateGlobalMetaStateWasCalled(false),
612 mGeneration(1) {}
613
614 virtual ~FakeInputReaderContext() {}
615
616 void assertUpdateGlobalMetaStateWasCalled() {
617 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
618 << "Expected updateGlobalMetaState() to have been called.";
619 mUpdateGlobalMetaStateWasCalled = false;
620 }
621
622 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
623
624 uint32_t getGeneration() { return mGeneration; }
625
626 void updateGlobalMetaState() override {
627 mUpdateGlobalMetaStateWasCalled = true;
628 ContextImpl::updateGlobalMetaState();
629 }
630
631 int32_t getGlobalMetaState() override {
632 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
633 }
634
635 int32_t bumpGeneration() override {
636 mGeneration = ContextImpl::bumpGeneration();
637 return mGeneration;
638 }
639 } mFakeContext;
640
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +0800642
643public:
644 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800645};
646
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700647// --- InputReaderPolicyTest ---
648class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700649protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700650 sp<FakeInputReaderPolicy> mFakePolicy;
651
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700652 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700653 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700654};
655
656/**
657 * Check that empty set of viewports is an acceptable configuration.
658 * Also try to get internal viewport two different ways - by type and by uniqueId.
659 *
660 * There will be confusion if two viewports with empty uniqueId and identical type are present.
661 * Such configuration is not currently allowed.
662 */
663TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700664 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700665
666 // We didn't add any viewports yet, so there shouldn't be any.
667 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100668 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700669 ASSERT_FALSE(internalViewport);
670
671 // Add an internal viewport, then clear it
672 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000673 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100674 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700675
676 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700677 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700678 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100679 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700680
681 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100682 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700683 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700684 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700685
686 mFakePolicy->clearViewports();
687 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700688 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700689 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100690 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700691 ASSERT_FALSE(internalViewport);
692}
693
694TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
695 const std::string internalUniqueId = "local:0";
696 const std::string externalUniqueId = "local:1";
697 const std::string virtualUniqueId1 = "virtual:2";
698 const std::string virtualUniqueId2 = "virtual:3";
699 constexpr int32_t virtualDisplayId1 = 2;
700 constexpr int32_t virtualDisplayId2 = 3;
701
702 // Add an internal viewport
703 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000704 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
705 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700706 // Add an external viewport
707 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000708 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
709 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700710 // Add an virtual viewport
711 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000712 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
713 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700714 // Add another virtual viewport
715 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000716 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
717 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700718
719 // Check matching by type for internal
720 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100721 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700722 ASSERT_TRUE(internalViewport);
723 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
724
725 // Check matching by type for external
726 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100727 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700728 ASSERT_TRUE(externalViewport);
729 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
730
731 // Check matching by uniqueId for virtual viewport #1
732 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700733 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700734 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100735 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700736 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
737 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
738
739 // Check matching by uniqueId for virtual viewport #2
740 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700741 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700742 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100743 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700744 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
745 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
746}
747
748
749/**
750 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
751 * that lookup works by checking display id.
752 * Check that 2 viewports of each kind is possible, for all existing viewport types.
753 */
754TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
755 const std::string uniqueId1 = "uniqueId1";
756 const std::string uniqueId2 = "uniqueId2";
757 constexpr int32_t displayId1 = 2;
758 constexpr int32_t displayId2 = 3;
759
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100760 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
761 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700762 for (const ViewportType& type : types) {
763 mFakePolicy->clearViewports();
764 // Add a viewport
765 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000766 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
767 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700768 // Add another viewport
769 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000770 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
771 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700772
773 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700774 std::optional<DisplayViewport> viewport1 =
775 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700776 ASSERT_TRUE(viewport1);
777 ASSERT_EQ(displayId1, viewport1->displayId);
778 ASSERT_EQ(type, viewport1->type);
779
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700780 std::optional<DisplayViewport> viewport2 =
781 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700782 ASSERT_TRUE(viewport2);
783 ASSERT_EQ(displayId2, viewport2->displayId);
784 ASSERT_EQ(type, viewport2->type);
785
786 // When there are multiple viewports of the same kind, and uniqueId is not specified
787 // in the call to getDisplayViewport, then that situation is not supported.
788 // The viewports can be stored in any order, so we cannot rely on the order, since that
789 // is just implementation detail.
790 // However, we can check that it still returns *a* viewport, we just cannot assert
791 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700792 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700793 ASSERT_TRUE(someViewport);
794 }
795}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700797/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000798 * When we have multiple internal displays make sure we always return the default display when
799 * querying by type.
800 */
801TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
802 const std::string uniqueId1 = "uniqueId1";
803 const std::string uniqueId2 = "uniqueId2";
804 constexpr int32_t nonDefaultDisplayId = 2;
805 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
806 "Test display ID should not be ADISPLAY_ID_DEFAULT");
807
808 // Add the default display first and ensure it gets returned.
809 mFakePolicy->clearViewports();
810 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000811 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000812 ViewportType::INTERNAL);
813 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000814 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000815 ViewportType::INTERNAL);
816
817 std::optional<DisplayViewport> viewport =
818 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
819 ASSERT_TRUE(viewport);
820 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
821 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
822
823 // Add the default display second to make sure order doesn't matter.
824 mFakePolicy->clearViewports();
825 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000826 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000827 ViewportType::INTERNAL);
828 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000829 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000830 ViewportType::INTERNAL);
831
832 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
833 ASSERT_TRUE(viewport);
834 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
835 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
836}
837
838/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700839 * Check getDisplayViewportByPort
840 */
841TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100842 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700843 const std::string uniqueId1 = "uniqueId1";
844 const std::string uniqueId2 = "uniqueId2";
845 constexpr int32_t displayId1 = 1;
846 constexpr int32_t displayId2 = 2;
847 const uint8_t hdmi1 = 0;
848 const uint8_t hdmi2 = 1;
849 const uint8_t hdmi3 = 2;
850
851 mFakePolicy->clearViewports();
852 // Add a viewport that's associated with some display port that's not of interest.
853 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000854 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
855 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700856 // Add another viewport, connected to HDMI1 port
857 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000858 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
859 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700860
861 // Check that correct display viewport was returned by comparing the display ports.
862 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
863 ASSERT_TRUE(hdmi1Viewport);
864 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
865 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
866
867 // Check that we can still get the same viewport using the uniqueId
868 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
869 ASSERT_TRUE(hdmi1Viewport);
870 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
871 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
872 ASSERT_EQ(type, hdmi1Viewport->type);
873
874 // Check that we cannot find a port with "HDMI2", because we never added one
875 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
876 ASSERT_FALSE(hdmi2Viewport);
877}
878
Michael Wrightd02c5b62014-02-10 15:10:22 -0800879// --- InputReaderTest ---
880
881class InputReaderTest : public testing::Test {
882protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700883 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700885 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000886 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800887
Chris Yea52ade12020-08-27 16:49:20 -0700888 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700889 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700890 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700891 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892
Prabir Pradhan28efc192019-11-05 01:10:04 +0000893 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700894 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 }
896
Chris Yea52ade12020-08-27 16:49:20 -0700897 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700898 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800900 }
901
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700902 void addDevice(int32_t eventHubId, const std::string& name,
903 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800904 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905
906 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800907 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908 }
909 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000910 mReader->loopOnce();
911 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700912 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
913 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800914 }
915
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800916 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700917 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000918 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700919 }
920
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800921 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700922 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000923 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700924 }
925
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800926 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700927 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700928 ftl::Flags<InputDeviceClass> classes,
929 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800930 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800931 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
932 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800933 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800934 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935 return mapper;
936 }
937};
938
Chris Ye98d3f532020-10-01 21:48:59 -0700939TEST_F(InputReaderTest, PolicyGetInputDevices) {
940 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700941 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700942 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943
944 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700945 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800947 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100948 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
950 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000951 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952}
953
Chris Yee7310032020-09-22 15:36:28 -0700954TEST_F(InputReaderTest, GetMergedInputDevices) {
955 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
956 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
957 // Add two subdevices to device
958 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
959 // Must add at least one mapper or the device will be ignored!
960 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
961 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
962
963 // Push same device instance for next device to be added, so they'll have same identifier.
964 mReader->pushNextDevice(device);
965 mReader->pushNextDevice(device);
966 ASSERT_NO_FATAL_FAILURE(
967 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
968 ASSERT_NO_FATAL_FAILURE(
969 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
970
971 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000972 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700973}
974
Chris Yee14523a2020-12-19 13:46:00 -0800975TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
976 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
977 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
978 // Add two subdevices to device
979 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
980 // Must add at least one mapper or the device will be ignored!
981 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
982 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
983
984 // Push same device instance for next device to be added, so they'll have same identifier.
985 mReader->pushNextDevice(device);
986 mReader->pushNextDevice(device);
987 // Sensor device is initially disabled
988 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
989 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
990 nullptr));
991 // Device is disabled because the only sub device is a sensor device and disabled initially.
992 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
993 ASSERT_FALSE(device->isEnabled());
994 ASSERT_NO_FATAL_FAILURE(
995 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
996 // The merged device is enabled if any sub device is enabled
997 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
998 ASSERT_TRUE(device->isEnabled());
999}
1000
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001001TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001002 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001003 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001004 constexpr int32_t eventHubId = 1;
1005 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001006 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001007 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001008 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001009 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001010
Yi Kong9b14ac62018-07-17 13:48:38 -07001011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001012
1013 NotifyDeviceResetArgs resetArgs;
1014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001015 ASSERT_EQ(deviceId, resetArgs.deviceId);
1016
1017 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001018 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001019 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001020
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001022 ASSERT_EQ(deviceId, resetArgs.deviceId);
1023 ASSERT_EQ(device->isEnabled(), false);
1024
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001025 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001026 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
1028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001029 ASSERT_EQ(device->isEnabled(), false);
1030
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001031 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001032 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07001034 ASSERT_EQ(deviceId, resetArgs.deviceId);
1035 ASSERT_EQ(device->isEnabled(), true);
1036}
1037
Michael Wrightd02c5b62014-02-10 15:10:22 -08001038TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001039 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001040 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001041 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001042 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001043 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001044 AINPUT_SOURCE_KEYBOARD, nullptr);
1045 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001046
1047 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
1048 AINPUT_SOURCE_ANY, AKEYCODE_A))
1049 << "Should return unknown when the device id is >= 0 but unknown.";
1050
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001051 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1052 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1053 << "Should return unknown when the device id is valid but the sources are not "
1054 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001055
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001056 ASSERT_EQ(AKEY_STATE_DOWN,
1057 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1058 AKEYCODE_A))
1059 << "Should return value provided by mapper when device id is valid and the device "
1060 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001061
1062 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
1063 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1064 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1065
1066 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
1067 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
1068 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1069}
1070
Philip Junker4af3b3d2021-12-14 10:36:55 +01001071TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
1072 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1073 constexpr int32_t eventHubId = 1;
1074 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
1075 InputDeviceClass::KEYBOARD,
1076 AINPUT_SOURCE_KEYBOARD, nullptr);
1077 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1078
1079 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
1080 << "Should return unknown when the device with the specified id is not found.";
1081
1082 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1083 << "Should return correct mapping when device id is valid and mapping exists.";
1084
1085 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
1086 << "Should return the location key code when device id is valid and there's no "
1087 "mapping.";
1088}
1089
1090TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
1091 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1092 constexpr int32_t eventHubId = 1;
1093 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
1094 InputDeviceClass::JOYSTICK,
1095 AINPUT_SOURCE_GAMEPAD, nullptr);
1096 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
1097
1098 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
1099 << "Should return unknown when the device id is valid but there is no keyboard mapper";
1100}
1101
Michael Wrightd02c5b62014-02-10 15:10:22 -08001102TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001103 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001104 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001105 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001106 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001107 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001108 AINPUT_SOURCE_KEYBOARD, nullptr);
1109 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001110
1111 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
1112 AINPUT_SOURCE_ANY, KEY_A))
1113 << "Should return unknown when the device id is >= 0 but unknown.";
1114
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001115 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1116 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
1117 << "Should return unknown when the device id is valid but the sources are not "
1118 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001119
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001120 ASSERT_EQ(AKEY_STATE_DOWN,
1121 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1122 KEY_A))
1123 << "Should return value provided by mapper when device id is valid and the device "
1124 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001125
1126 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
1127 AINPUT_SOURCE_TRACKBALL, KEY_A))
1128 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1129
1130 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
1131 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
1132 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1133}
1134
1135TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001136 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001137 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001138 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001139 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001140 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001141 AINPUT_SOURCE_KEYBOARD, nullptr);
1142 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001143
1144 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
1145 AINPUT_SOURCE_ANY, SW_LID))
1146 << "Should return unknown when the device id is >= 0 but unknown.";
1147
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001148 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1149 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
1150 << "Should return unknown when the device id is valid but the sources are not "
1151 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001152
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001153 ASSERT_EQ(AKEY_STATE_DOWN,
1154 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
1155 SW_LID))
1156 << "Should return value provided by mapper when device id is valid and the device "
1157 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001158
1159 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
1160 AINPUT_SOURCE_TRACKBALL, SW_LID))
1161 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
1162
1163 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
1164 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
1165 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
1166}
1167
1168TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001169 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001170 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001171 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001172 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001173 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001174 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01001175
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001176 mapper.addSupportedKeyCode(AKEYCODE_A);
1177 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001178
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001179 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08001180 uint8_t flags[4] = { 0, 0, 0, 1 };
1181
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001182 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08001183 << "Should return false when device id is >= 0 but unknown.";
1184 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1185
1186 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001187 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001188 << "Should return false when device id is valid but the sources are not supported by "
1189 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001190 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1191
1192 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001193 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001194 keyCodes, flags))
1195 << "Should return value provided by mapper when device id is valid and the device "
1196 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001197 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1198
1199 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001200 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1201 << "Should return false when the device id is < 0 but the sources are not supported by "
1202 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001203 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1204
1205 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001206 ASSERT_TRUE(
1207 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1208 << "Should return value provided by mapper when device id is < 0 and one of the "
1209 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001210 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1211}
1212
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001213TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001214 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001215 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001216
1217 NotifyConfigurationChangedArgs args;
1218
1219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1220 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1221}
1222
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001223TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001224 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001225 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001226 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001227 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001228 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001229 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001230 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001231 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001232
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001233 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001234 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001235 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1236
1237 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001238 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001239 ASSERT_EQ(when, event.when);
1240 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001241 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001242 ASSERT_EQ(EV_KEY, event.type);
1243 ASSERT_EQ(KEY_A, event.code);
1244 ASSERT_EQ(1, event.value);
1245}
1246
Garfield Tan1c7bc862020-01-28 13:24:04 -08001247TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001248 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001249 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001250 constexpr int32_t eventHubId = 1;
1251 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001252 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001253 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001254 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001255 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001256
1257 NotifyDeviceResetArgs resetArgs;
1258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001259 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001260
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001261 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001262 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001264 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001265 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001267 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001268 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001270 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001271 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001273 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001274 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001276 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001277 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001278}
1279
Garfield Tan1c7bc862020-01-28 13:24:04 -08001280TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1281 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001282 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001283 constexpr int32_t eventHubId = 1;
1284 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1285 // Must add at least one mapper or the device will be ignored!
1286 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001287 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001288 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1289
1290 NotifyDeviceResetArgs resetArgs;
1291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1292 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1293}
1294
Arthur Hungc23540e2018-11-29 20:42:11 +08001295TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001296 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001297 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001298 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001299 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001300 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1301 FakeInputMapper& mapper =
1302 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001303 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001304
1305 const uint8_t hdmi1 = 1;
1306
1307 // Associated touch screen with second display.
1308 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1309
1310 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001311 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001312 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001313 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001314 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001315 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001316 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001317 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001318 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001319 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001320
1321 // Add the device, and make sure all of the callbacks are triggered.
1322 // The device is added after the input port associations are processed since
1323 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001324 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001327 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001328
Arthur Hung2c9a3342019-07-23 14:18:59 +08001329 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001330 ASSERT_EQ(deviceId, device->getId());
1331 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1332 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001333
1334 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001335 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001336 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001337 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001338}
1339
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001340TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1341 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001342 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001343 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1344 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1345 // Must add at least one mapper or the device will be ignored!
1346 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1347 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1348 mReader->pushNextDevice(device);
1349 mReader->pushNextDevice(device);
1350 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1351 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1352
1353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1354
1355 NotifyDeviceResetArgs resetArgs;
1356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1357 ASSERT_EQ(deviceId, resetArgs.deviceId);
1358 ASSERT_TRUE(device->isEnabled());
1359 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1360 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1361
1362 disableDevice(deviceId);
1363 mReader->loopOnce();
1364
1365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1366 ASSERT_EQ(deviceId, resetArgs.deviceId);
1367 ASSERT_FALSE(device->isEnabled());
1368 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1369 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1370
1371 enableDevice(deviceId);
1372 mReader->loopOnce();
1373
1374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1375 ASSERT_EQ(deviceId, resetArgs.deviceId);
1376 ASSERT_TRUE(device->isEnabled());
1377 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1378 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1379}
1380
1381TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1382 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001383 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001384 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1385 // Add two subdevices to device
1386 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1387 FakeInputMapper& mapperDevice1 =
1388 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1389 FakeInputMapper& mapperDevice2 =
1390 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1391 mReader->pushNextDevice(device);
1392 mReader->pushNextDevice(device);
1393 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1394 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1395
1396 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1397 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1398
1399 ASSERT_EQ(AKEY_STATE_DOWN,
1400 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1401 ASSERT_EQ(AKEY_STATE_DOWN,
1402 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1403 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1404 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1405}
1406
Prabir Pradhan7e186182020-11-10 13:56:45 -08001407TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1408 NotifyPointerCaptureChangedArgs args;
1409
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001410 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001411 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1412 mReader->loopOnce();
1413 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001414 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1415 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001416
1417 mFakePolicy->setPointerCapture(false);
1418 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1419 mReader->loopOnce();
1420 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001421 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001422
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001423 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001424 // does not change.
1425 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1426 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001427 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001428}
1429
Chris Ye87143712020-11-10 05:05:58 +00001430class FakeVibratorInputMapper : public FakeInputMapper {
1431public:
1432 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1433 : FakeInputMapper(deviceContext, sources) {}
1434
1435 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1436};
1437
1438TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1439 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001440 ftl::Flags<InputDeviceClass> deviceClass =
1441 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001442 constexpr int32_t eventHubId = 1;
1443 const char* DEVICE_LOCATION = "BLUETOOTH";
1444 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1445 FakeVibratorInputMapper& mapper =
1446 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1447 mReader->pushNextDevice(device);
1448
1449 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1450 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1451
1452 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1453 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1454}
1455
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001456// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001457
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001458class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001459public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001460 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001461
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001462 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001463
Andy Chenf9f1a022022-08-29 20:07:10 -04001464 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1465
Chris Yee2b1e5c2021-03-10 22:45:12 -08001466 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1467
1468 void dump(std::string& dump) override {}
1469
1470 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1471 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001472 }
1473
Chris Yee2b1e5c2021-03-10 22:45:12 -08001474 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1475 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001476 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001477
1478 bool setLightColor(int32_t lightId, int32_t color) override {
1479 getDeviceContext().setLightBrightness(lightId, color >> 24);
1480 return true;
1481 }
1482
1483 std::optional<int32_t> getLightColor(int32_t lightId) override {
1484 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1485 if (!result.has_value()) {
1486 return std::nullopt;
1487 }
1488 return result.value() << 24;
1489 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001490
1491 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1492
1493 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1494
1495private:
1496 InputDeviceContext& mDeviceContext;
1497 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1498 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001499 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001500};
1501
Chris Yee2b1e5c2021-03-10 22:45:12 -08001502TEST_F(InputReaderTest, BatteryGetCapacity) {
1503 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001504 ftl::Flags<InputDeviceClass> deviceClass =
1505 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001506 constexpr int32_t eventHubId = 1;
1507 const char* DEVICE_LOCATION = "BLUETOOTH";
1508 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001509 FakePeripheralController& controller =
1510 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001511 mReader->pushNextDevice(device);
1512
1513 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1514
Harry Cuttsa5b71292022-11-28 12:56:17 +00001515 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1516 FakeEventHub::BATTERY_CAPACITY);
1517 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001518}
1519
1520TEST_F(InputReaderTest, BatteryGetStatus) {
1521 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001522 ftl::Flags<InputDeviceClass> deviceClass =
1523 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001524 constexpr int32_t eventHubId = 1;
1525 const char* DEVICE_LOCATION = "BLUETOOTH";
1526 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001527 FakePeripheralController& controller =
1528 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001529 mReader->pushNextDevice(device);
1530
1531 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1532
Harry Cuttsa5b71292022-11-28 12:56:17 +00001533 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1534 FakeEventHub::BATTERY_STATUS);
1535 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001536}
1537
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001538TEST_F(InputReaderTest, BatteryGetDevicePath) {
1539 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1540 ftl::Flags<InputDeviceClass> deviceClass =
1541 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1542 constexpr int32_t eventHubId = 1;
1543 const char* DEVICE_LOCATION = "BLUETOOTH";
1544 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1545 device->addController<FakePeripheralController>(eventHubId);
1546 mReader->pushNextDevice(device);
1547
1548 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1549
Harry Cuttsa5b71292022-11-28 12:56:17 +00001550 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001551}
1552
Chris Ye3fdbfef2021-01-06 18:45:18 -08001553TEST_F(InputReaderTest, LightGetColor) {
1554 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001555 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001556 constexpr int32_t eventHubId = 1;
1557 const char* DEVICE_LOCATION = "BLUETOOTH";
1558 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001559 FakePeripheralController& controller =
1560 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001561 mReader->pushNextDevice(device);
1562 RawLightInfo info = {.id = 1,
1563 .name = "Mono",
1564 .maxBrightness = 255,
1565 .flags = InputLightClass::BRIGHTNESS,
1566 .path = ""};
1567 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
1568 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
1569
1570 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001571
Chris Yee2b1e5c2021-03-10 22:45:12 -08001572 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
1573 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001574 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
1575 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
1576}
1577
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001578// --- InputReaderIntegrationTest ---
1579
1580// These tests create and interact with the InputReader only through its interface.
1581// The InputReader is started during SetUp(), which starts its processing in its own
1582// thread. The tests use linux uinput to emulate input devices.
1583// NOTE: Interacting with the physical device while these tests are running may cause
1584// the tests to fail.
1585class InputReaderIntegrationTest : public testing::Test {
1586protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001587 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001588 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001589 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001590
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001591 std::shared_ptr<FakePointerController> mFakePointerController;
1592
Chris Yea52ade12020-08-27 16:49:20 -07001593 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001594#if !defined(__ANDROID__)
1595 GTEST_SKIP();
1596#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001597 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001598 mFakePointerController = std::make_shared<FakePointerController>();
1599 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001600 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
1601 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001602
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001603 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1604 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001605 ASSERT_EQ(mReader->start(), OK);
1606
1607 // Since this test is run on a real device, all the input devices connected
1608 // to the test device will show up in mReader. We wait for those input devices to
1609 // show up before beginning the tests.
1610 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1611 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1612 }
1613
Chris Yea52ade12020-08-27 16:49:20 -07001614 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001615#if !defined(__ANDROID__)
1616 return;
1617#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001618 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001619 mReader.reset();
1620 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001621 mFakePolicy.clear();
1622 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001623
1624 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1625 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1626 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1627 [&name](const InputDeviceInfo& info) {
1628 return info.getIdentifier().name == name;
1629 });
1630 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1631 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001632};
1633
1634TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1635 // An invalid input device that is only used for this test.
1636 class InvalidUinputDevice : public UinputDevice {
1637 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00001638 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001639
1640 private:
1641 void configureDevice(int fd, uinput_user_dev* device) override {}
1642 };
1643
1644 const size_t numDevices = mFakePolicy->getInputDevices().size();
1645
1646 // UinputDevice does not set any event or key bits, so InputReader should not
1647 // consider it as a valid device.
1648 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1649 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1650 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1651 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1652
1653 invalidDevice.reset();
1654 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1655 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1656 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1657}
1658
1659TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1660 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1661
1662 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1663 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1664 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1665 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1666
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001667 const auto device = findDeviceByName(keyboard->getName());
1668 ASSERT_TRUE(device.has_value());
1669 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1671 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001672
1673 keyboard.reset();
1674 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1675 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1676 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1677}
1678
1679TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1680 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1681 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1682
1683 NotifyConfigurationChangedArgs configChangedArgs;
1684 ASSERT_NO_FATAL_FAILURE(
1685 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001686 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001687 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1688
1689 NotifyKeyArgs keyArgs;
1690 keyboard->pressAndReleaseHomeKey();
1691 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1692 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001693 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001694 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001695 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001696 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001697 prevTimestamp = keyArgs.eventTime;
1698
1699 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1700 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001701 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001702 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001703 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001704}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001705
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001706TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1707 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1708 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1709
1710 const auto device = findDeviceByName(stylus->getName());
1711 ASSERT_TRUE(device.has_value());
1712
Prabir Pradhana3621852022-10-14 18:57:23 +00001713 // An external stylus with buttons should also be recognized as a keyboard.
1714 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001715 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1716 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1717
1718 const auto DOWN =
1719 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1720 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1721
1722 stylus->pressAndReleaseKey(BTN_STYLUS);
1723 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1724 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1725 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1726 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1727
1728 stylus->pressAndReleaseKey(BTN_STYLUS2);
1729 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1730 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1731 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1732 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1733
1734 stylus->pressAndReleaseKey(BTN_STYLUS3);
1735 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1736 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1737 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1738 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1739}
1740
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001741/**
1742 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1743 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1744 * are passed to the listener.
1745 */
1746static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1747TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1748 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1749 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1750 NotifyKeyArgs keyArgs;
1751
1752 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1753 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1754 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1755 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1756
1757 controller->pressAndReleaseKey(BTN_GEAR_UP);
1758 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1759 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1760 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1761}
1762
Arthur Hungaab25622020-01-16 11:22:11 +08001763// --- TouchProcessTest ---
1764class TouchIntegrationTest : public InputReaderIntegrationTest {
1765protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001766 const std::string UNIQUE_ID = "local:0";
1767
Chris Yea52ade12020-08-27 16:49:20 -07001768 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001769#if !defined(__ANDROID__)
1770 GTEST_SKIP();
1771#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001772 InputReaderIntegrationTest::SetUp();
1773 // At least add an internal display.
1774 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1775 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001776 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001777
1778 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1779 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1780 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001781 const auto info = findDeviceByName(mDevice->getName());
1782 ASSERT_TRUE(info);
1783 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001784 }
1785
1786 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1787 int32_t orientation, const std::string& uniqueId,
1788 std::optional<uint8_t> physicalPort,
1789 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001790 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
1791 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001792 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1793 }
1794
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001795 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1796 NotifyMotionArgs args;
1797 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1798 EXPECT_EQ(action, args.action);
1799 ASSERT_EQ(points.size(), args.pointerCount);
1800 for (size_t i = 0; i < args.pointerCount; i++) {
1801 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1802 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1803 }
1804 }
1805
Arthur Hungaab25622020-01-16 11:22:11 +08001806 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001807 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001808};
1809
1810TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1811 NotifyMotionArgs args;
1812 const Point centerPoint = mDevice->getCenterPoint();
1813
1814 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001815 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001816 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001817 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001818 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1819 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1820
1821 // ACTION_MOVE
1822 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001823 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001824 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1826
1827 // ACTION_UP
1828 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001829 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001830 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1831 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1832}
1833
1834TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1835 NotifyMotionArgs args;
1836 const Point centerPoint = mDevice->getCenterPoint();
1837
1838 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001839 mDevice->sendSlot(FIRST_SLOT);
1840 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001841 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001842 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001843 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1844 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1845
1846 // ACTION_POINTER_DOWN (Second slot)
1847 const Point secondPoint = centerPoint + Point(100, 100);
1848 mDevice->sendSlot(SECOND_SLOT);
1849 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001850 mDevice->sendDown(secondPoint);
1851 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001852 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001853 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001854
1855 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001856 mDevice->sendMove(secondPoint + Point(1, 1));
1857 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001858 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1860
1861 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001862 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001863 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001864 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001865 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001866
1867 // ACTION_UP
1868 mDevice->sendSlot(FIRST_SLOT);
1869 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001870 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001871 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1872 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1873}
1874
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001875/**
1876 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1877 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1878 * data?
1879 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1880 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1881 * for Pointer 0 only is generated after.
1882 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1883 * events, we will not miss any information.
1884 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1885 * event generated afterwards that contains the newest movement of pointer 0.
1886 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1887 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1888 * losing information about non-palm pointers.
1889 */
1890TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1891 NotifyMotionArgs args;
1892 const Point centerPoint = mDevice->getCenterPoint();
1893
1894 // ACTION_DOWN
1895 mDevice->sendSlot(FIRST_SLOT);
1896 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1897 mDevice->sendDown(centerPoint);
1898 mDevice->sendSync();
1899 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1900
1901 // ACTION_POINTER_DOWN (Second slot)
1902 const Point secondPoint = centerPoint + Point(100, 100);
1903 mDevice->sendSlot(SECOND_SLOT);
1904 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1905 mDevice->sendDown(secondPoint);
1906 mDevice->sendSync();
1907 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1908
1909 // ACTION_MOVE (First slot)
1910 mDevice->sendSlot(FIRST_SLOT);
1911 mDevice->sendMove(centerPoint + Point(5, 5));
1912 // ACTION_POINTER_UP (Second slot)
1913 mDevice->sendSlot(SECOND_SLOT);
1914 mDevice->sendPointerUp();
1915 // Send a single sync for the above 2 pointer updates
1916 mDevice->sendSync();
1917
1918 // First, we should get POINTER_UP for the second pointer
1919 assertReceivedMotion(ACTION_POINTER_1_UP,
1920 {/*first pointer */ centerPoint + Point(5, 5),
1921 /*second pointer*/ secondPoint});
1922
1923 // Next, the MOVE event for the first pointer
1924 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1925}
1926
1927/**
1928 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1929 * move, and then it will go up, all in the same frame.
1930 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1931 * gets sent to the listener.
1932 */
1933TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1934 NotifyMotionArgs args;
1935 const Point centerPoint = mDevice->getCenterPoint();
1936
1937 // ACTION_DOWN
1938 mDevice->sendSlot(FIRST_SLOT);
1939 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1940 mDevice->sendDown(centerPoint);
1941 mDevice->sendSync();
1942 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1943
1944 // ACTION_POINTER_DOWN (Second slot)
1945 const Point secondPoint = centerPoint + Point(100, 100);
1946 mDevice->sendSlot(SECOND_SLOT);
1947 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1948 mDevice->sendDown(secondPoint);
1949 mDevice->sendSync();
1950 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1951
1952 // ACTION_MOVE (First slot)
1953 mDevice->sendSlot(FIRST_SLOT);
1954 mDevice->sendMove(centerPoint + Point(5, 5));
1955 // ACTION_POINTER_UP (Second slot)
1956 mDevice->sendSlot(SECOND_SLOT);
1957 mDevice->sendMove(secondPoint + Point(6, 6));
1958 mDevice->sendPointerUp();
1959 // Send a single sync for the above 2 pointer updates
1960 mDevice->sendSync();
1961
1962 // First, we should get POINTER_UP for the second pointer
1963 // The movement of the second pointer during the liftoff frame is ignored.
1964 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1965 assertReceivedMotion(ACTION_POINTER_1_UP,
1966 {/*first pointer */ centerPoint + Point(5, 5),
1967 /*second pointer*/ secondPoint});
1968
1969 // Next, the MOVE event for the first pointer
1970 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1971}
1972
Arthur Hungaab25622020-01-16 11:22:11 +08001973TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1974 NotifyMotionArgs args;
1975 const Point centerPoint = mDevice->getCenterPoint();
1976
1977 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001978 mDevice->sendSlot(FIRST_SLOT);
1979 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001980 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001981 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001982 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1983 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1984
arthurhungcc7f9802020-04-30 17:55:40 +08001985 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001986 const Point secondPoint = centerPoint + Point(100, 100);
1987 mDevice->sendSlot(SECOND_SLOT);
1988 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1989 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001990 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001991 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001992 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001993
arthurhungcc7f9802020-04-30 17:55:40 +08001994 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001995 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001996 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001997 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1999
arthurhungcc7f9802020-04-30 17:55:40 +08002000 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
2001 // a palm event.
2002 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08002003 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002004 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002005 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08002006 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08002007 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08002008
arthurhungcc7f9802020-04-30 17:55:40 +08002009 // Send up to second slot, expect first slot send moving.
2010 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002011 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08002012 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002014
arthurhungcc7f9802020-04-30 17:55:40 +08002015 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08002016 mDevice->sendSlot(FIRST_SLOT);
2017 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08002018 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08002019
arthurhungcc7f9802020-04-30 17:55:40 +08002020 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
2021 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08002022}
2023
Prabir Pradhanda20b172022-09-26 17:01:18 +00002024TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
2025 const Point centerPoint = mDevice->getCenterPoint();
2026
2027 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2028 mDevice->sendSlot(FIRST_SLOT);
2029 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2030 mDevice->sendToolType(MT_TOOL_PEN);
2031 mDevice->sendDown(centerPoint);
2032 mDevice->sendSync();
2033 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2034 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2035 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2036
2037 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2038
2039 // Release the stylus touch.
2040 mDevice->sendUp();
2041 mDevice->sendSync();
2042 ASSERT_NO_FATAL_FAILURE(
2043 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2044
2045 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2046
2047 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2048 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2049 mDevice->sendToolType(MT_TOOL_FINGER);
2050 mDevice->sendDown(centerPoint);
2051 mDevice->sendSync();
2052 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2053 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2054 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2055
2056 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2057
2058 mDevice->sendUp();
2059 mDevice->sendSync();
2060 ASSERT_NO_FATAL_FAILURE(
2061 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2062
2063 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2064 // The policy should be notified of the stylus presence.
2065 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2066 mDevice->sendToolType(MT_TOOL_PEN);
2067 mDevice->sendMove(centerPoint);
2068 mDevice->sendSync();
2069 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2070 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2071 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
2072
2073 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2074}
2075
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002076TEST_F(TouchIntegrationTest, StylusButtonsGenerateKeyEvents) {
2077 mDevice->sendKey(BTN_STYLUS, 1);
2078 mDevice->sendSync();
2079 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2080 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2081 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2082
2083 mDevice->sendKey(BTN_STYLUS, 0);
2084 mDevice->sendSync();
2085 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2086 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2087 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2088}
2089
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002090TEST_F(TouchIntegrationTest, StylusButtonsSurroundingTouchGesture) {
2091 const Point centerPoint = mDevice->getCenterPoint();
2092
2093 // Press the stylus button.
2094 mDevice->sendKey(BTN_STYLUS, 1);
2095 mDevice->sendSync();
2096 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2097 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2098 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2099
2100 // Start and finish a stylus gesture.
2101 mDevice->sendSlot(FIRST_SLOT);
2102 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2103 mDevice->sendToolType(MT_TOOL_PEN);
2104 mDevice->sendDown(centerPoint);
2105 mDevice->sendSync();
2106 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2107 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2108 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2109 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2110 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2111 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2112 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2113 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2114
2115 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2116 mDevice->sendSync();
2117 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2118 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2119 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2120 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2121 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2122 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2123
2124 // Release the stylus button.
2125 mDevice->sendKey(BTN_STYLUS, 0);
2126 mDevice->sendSync();
2127 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2128 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2129 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2130}
2131
2132TEST_F(TouchIntegrationTest, StylusButtonsWithinTouchGesture) {
2133 const Point centerPoint = mDevice->getCenterPoint();
2134
2135 // Start a stylus gesture.
2136 mDevice->sendSlot(FIRST_SLOT);
2137 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2138 mDevice->sendToolType(MT_TOOL_PEN);
2139 mDevice->sendDown(centerPoint);
2140 mDevice->sendSync();
2141 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2142 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2143 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2144
2145 // Press and release a stylus button. Each change in button state also generates a MOVE event.
2146 mDevice->sendKey(BTN_STYLUS, 1);
2147 mDevice->sendSync();
2148 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2149 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2150 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2151 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2152 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2153 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2154 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2155 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2156 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2157 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
2158 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2159
2160 mDevice->sendKey(BTN_STYLUS, 0);
2161 mDevice->sendSync();
2162 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
2163 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2164 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
2165 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2166 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2167 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2168 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2169 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2170 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2171
2172 // Finish the stylus gesture.
2173 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2174 mDevice->sendSync();
2175 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2176 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2177 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0))));
2178}
2179
Michael Wrightd02c5b62014-02-10 15:10:22 -08002180// --- InputDeviceTest ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08002181class InputDeviceTest : public testing::Test {
2182protected:
2183 static const char* DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002184 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002185 static const int32_t DEVICE_ID;
2186 static const int32_t DEVICE_GENERATION;
2187 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002188 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002189 static const int32_t EVENTHUB_ID;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002190 static const std::string DEVICE_BLUETOOTH_ADDRESS;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002191
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002192 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002193 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002194 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002195 std::unique_ptr<InstrumentedInputReader> mReader;
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +00002196 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002197
Chris Yea52ade12020-08-27 16:49:20 -07002198 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002199 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002200 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002201 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002202 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002203 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002204 InputDeviceIdentifier identifier;
2205 identifier.name = DEVICE_NAME;
Arthur Hung2c9a3342019-07-23 14:18:59 +08002206 identifier.location = DEVICE_LOCATION;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002207 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
arthurhungdcef2dc2020-08-11 14:47:50 +08002208 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002209 identifier);
arthurhungdcef2dc2020-08-11 14:47:50 +08002210 mReader->pushNextDevice(mDevice);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002211 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08002212 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002213 }
2214
Chris Yea52ade12020-08-27 16:49:20 -07002215 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002216 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002217 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002218 }
2219};
2220
2221const char* InputDeviceTest::DEVICE_NAME = "device";
Arthur Hung2c9a3342019-07-23 14:18:59 +08002222const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002223const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002224const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
2225const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002226const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
Chris Ye1b0c7342020-07-28 21:57:03 -07002227 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002228const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002229const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002230
2231TEST_F(InputDeviceTest, ImmutableProperties) {
2232 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002233 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002234 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235}
2236
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002237TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
2238 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2239
2240 // Configuration
2241 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2242 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002243 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002244
2245 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2246}
2247
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002248TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2249 ASSERT_EQ(mDevice->isEnabled(), false);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002250}
2251
Michael Wrightd02c5b62014-02-10 15:10:22 -08002252TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2253 // Configuration.
2254 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002255 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256
2257 // Reset.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002258 unused += mDevice->reset(ARBITRARY_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002259
2260 NotifyDeviceResetArgs resetArgs;
2261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2262 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2263 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2264
2265 // Metadata.
2266 ASSERT_TRUE(mDevice->isIgnored());
2267 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2268
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002269 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002270 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002271 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002272 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2273 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2274
2275 // State queries.
2276 ASSERT_EQ(0, mDevice->getMetaState());
2277
2278 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2279 << "Ignored device should return unknown key code state.";
2280 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2281 << "Ignored device should return unknown scan code state.";
2282 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2283 << "Ignored device should return unknown switch state.";
2284
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002285 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286 uint8_t flags[2] = { 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002287 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 << "Ignored device should never mark any key codes.";
2289 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2290 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2291}
2292
2293TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2294 // Configuration.
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002295 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Michael Wrightd02c5b62014-02-10 15:10:22 -08002296
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002297 FakeInputMapper& mapper1 =
2298 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002299 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2300 mapper1.setMetaState(AMETA_ALT_ON);
2301 mapper1.addSupportedKeyCode(AKEYCODE_A);
2302 mapper1.addSupportedKeyCode(AKEYCODE_B);
2303 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2304 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2305 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2306 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2307 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002308
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002309 FakeInputMapper& mapper2 =
2310 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002311 mapper2.setMetaState(AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312
2313 InputReaderConfiguration config;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002314 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002315
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002316 std::string propertyValue;
2317 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002318 << "Device should have read configuration during configuration phase.";
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002319 ASSERT_EQ("value", propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002321 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2322 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002323
2324 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002325 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002326 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2327 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002328
2329 NotifyDeviceResetArgs resetArgs;
2330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2331 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2332 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2333
2334 // Metadata.
2335 ASSERT_FALSE(mDevice->isIgnored());
2336 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2337
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002338 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002339 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002340 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002341 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2342 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2343
2344 // State queries.
2345 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2346 << "Should query mappers and combine meta states.";
2347
2348 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2349 << "Should return unknown key code state when source not supported.";
2350 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2351 << "Should return unknown scan code state when source not supported.";
2352 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2353 << "Should return unknown switch state when source not supported.";
2354
2355 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2356 << "Should query mapper when source is supported.";
2357 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2358 << "Should query mapper when source is supported.";
2359 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2360 << "Should query mapper when source is supported.";
2361
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002362 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002364 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365 << "Should do nothing when source is unsupported.";
2366 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2367 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2368 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2369 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2370
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002371 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002372 << "Should query mapper when source is supported.";
2373 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2374 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2375 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2376 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2377
2378 // Event handling.
2379 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002380 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002381 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002383 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2384 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002385}
2386
Arthur Hung2c9a3342019-07-23 14:18:59 +08002387// A single input device is associated with a specific display. Check that:
2388// 1. Device is disabled if the viewport corresponding to the associated display is not found
2389// 2. Device is disabled when setEnabled API is called
2390TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002391 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002392
2393 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002394 std::list<NotifyArgs> unused =
2395 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002396
2397 // Device should be enabled by default.
2398 ASSERT_TRUE(mDevice->isEnabled());
2399
2400 // Prepare associated info.
2401 constexpr uint8_t hdmi = 1;
2402 const std::string UNIQUE_ID = "local:1";
2403
2404 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002405 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2406 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002407 // Device should be disabled because it is associated with a specific display via
2408 // input port <-> display port association, but the corresponding display is not found
2409 ASSERT_FALSE(mDevice->isEnabled());
2410
2411 // Prepare displays.
2412 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002413 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2414 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002415 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2416 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002417 ASSERT_TRUE(mDevice->isEnabled());
2418
2419 // Device should be disabled after set disable.
2420 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002421 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2422 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002423 ASSERT_FALSE(mDevice->isEnabled());
2424
2425 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002426 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2427 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002428 ASSERT_FALSE(mDevice->isEnabled());
2429}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430
Christine Franks1ba71cc2021-04-07 14:37:42 -07002431TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2432 // Device should be enabled by default.
2433 mFakePolicy->clearViewports();
2434 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002435 std::list<NotifyArgs> unused =
2436 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002437 ASSERT_TRUE(mDevice->isEnabled());
2438
2439 // Device should be disabled because it is associated with a specific display, but the
2440 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002441 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002442 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2443 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002444 ASSERT_FALSE(mDevice->isEnabled());
2445
2446 // Device should be enabled when a display is found.
2447 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2448 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2449 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002450 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2451 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002452 ASSERT_TRUE(mDevice->isEnabled());
2453
2454 // Device should be disabled after set disable.
2455 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002456 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2457 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002458 ASSERT_FALSE(mDevice->isEnabled());
2459
2460 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002461 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2462 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002463 ASSERT_FALSE(mDevice->isEnabled());
2464}
2465
Christine Franks2a2293c2022-01-18 11:51:16 -08002466TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2467 mFakePolicy->clearViewports();
2468 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002469 std::list<NotifyArgs> unused =
2470 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002471
Christine Franks2a2293c2022-01-18 11:51:16 -08002472 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2473 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2474 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
2475 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002476 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2477 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002478 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2479}
2480
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002481/**
2482 * This test reproduces a crash caused by a dangling reference that remains after device is added
2483 * and removed. The reference is accessed in InputDevice::dump(..);
2484 */
2485TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2486 constexpr int32_t TEST_EVENTHUB_ID = 10;
2487 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2488
2489 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
2490 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
2491 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2492 std::string dumpStr, eventHubDevStr;
2493 device.dump(dumpStr, eventHubDevStr);
2494}
2495
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002496TEST_F(InputDeviceTest, GetBluetoothAddress) {
2497 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2498 ASSERT_TRUE(address);
2499 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2500}
2501
Michael Wrightd02c5b62014-02-10 15:10:22 -08002502// --- InputMapperTest ---
2503
2504class InputMapperTest : public testing::Test {
2505protected:
2506 static const char* DEVICE_NAME;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002507 static const char* DEVICE_LOCATION;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508 static const int32_t DEVICE_ID;
2509 static const int32_t DEVICE_GENERATION;
2510 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002511 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002512 static const int32_t EVENTHUB_ID;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002514 std::shared_ptr<FakeEventHub> mFakeEventHub;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002516 std::unique_ptr<TestInputListener> mFakeListener;
arthurhungdcef2dc2020-08-11 14:47:50 +08002517 std::unique_ptr<InstrumentedInputReader> mReader;
2518 std::shared_ptr<InputDevice> mDevice;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002519
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002520 virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002521 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002522 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002523 mFakeListener = std::make_unique<TestInputListener>();
arthurhungdcef2dc2020-08-11 14:47:50 +08002524 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002525 *mFakeListener);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002526 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes, bus);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002527 // Consume the device reset notification generated when adding a new device.
2528 mFakeListener->assertNotifyDeviceResetWasCalled();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529 }
2530
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002531 void SetUp() override {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002532 SetUp(DEVICE_CLASSES);
2533 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002534
Chris Yea52ade12020-08-27 16:49:20 -07002535 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002536 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002537 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002538 }
2539
2540 void addConfigurationProperty(const char* key, const char* value) {
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002541 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002542 }
2543
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002544 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002545 if (!changes ||
2546 (changes &
2547 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2548 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002549 mReader->requestRefreshConfiguration(changes);
2550 mReader->loopOnce();
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002551 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002552 std::list<NotifyArgs> out =
2553 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002554 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002555 for (const NotifyArgs& args : out) {
2556 mFakeListener->notify(args);
2557 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002558 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002559 return out;
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002560 }
2561
arthurhungdcef2dc2020-08-11 14:47:50 +08002562 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
2563 const std::string& location, int32_t eventHubId,
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002564 ftl::Flags<InputDeviceClass> classes, int bus = 0) {
arthurhungdcef2dc2020-08-11 14:47:50 +08002565 InputDeviceIdentifier identifier;
2566 identifier.name = name;
2567 identifier.location = location;
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002568 identifier.bus = bus;
arthurhungdcef2dc2020-08-11 14:47:50 +08002569 std::shared_ptr<InputDevice> device =
2570 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2571 identifier);
2572 mReader->pushNextDevice(device);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002573 mFakeEventHub->addDevice(eventHubId, name, classes, bus);
arthurhungdcef2dc2020-08-11 14:47:50 +08002574 mReader->loopOnce();
2575 return device;
2576 }
2577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002578 template <class T, typename... Args>
2579 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002580 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002581 configureDevice(0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002582 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
2583 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002584 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002585 for (const NotifyArgs& loopArgs : resetArgList) {
2586 mFakeListener->notify(loopArgs);
2587 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002588 mReader->loopOnce();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002589 return mapper;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590 }
2591
2592 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002593 int32_t orientation, const std::string& uniqueId,
2594 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002595 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2596 uniqueId, physicalPort, viewportType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07002597 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2598 }
2599
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002600 void clearViewports() {
2601 mFakePolicy->clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002602 }
2603
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002604 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
2605 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606 RawEvent event;
2607 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002608 event.readTime = readTime;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002609 event.deviceId = mapper.getDeviceContext().getEventHubId();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610 event.type = type;
2611 event.code = code;
2612 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002613 std::list<NotifyArgs> processArgList = mapper.process(&event);
2614 for (const NotifyArgs& args : processArgList) {
2615 mFakeListener->notify(args);
2616 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002617 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08002618 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002619 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620 }
2621
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002622 void resetMapper(InputMapper& mapper, nsecs_t when) {
2623 const auto resetArgs = mapper.reset(when);
2624 for (const auto args : resetArgs) {
2625 mFakeListener->notify(args);
2626 }
2627 // Loop the reader to flush the input listener queue.
2628 mReader->loopOnce();
2629 }
2630
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631 static void assertMotionRange(const InputDeviceInfo& info,
2632 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2633 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002634 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2636 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2637 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2638 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2639 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2640 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2641 }
2642
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002643 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2644 float size, float touchMajor, float touchMinor, float toolMajor,
2645 float toolMinor, float orientation, float distance,
2646 float scaledAxisEpsilon = 1.f) {
2647 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2648 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2650 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002651 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2652 scaledAxisEpsilon);
2653 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2654 scaledAxisEpsilon);
2655 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2656 scaledAxisEpsilon);
2657 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2658 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002659 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2660 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2661 }
2662
Michael Wright17db18e2020-06-26 20:51:44 +01002663 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002665 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002666 ASSERT_NEAR(x, actualX, 1);
2667 ASSERT_NEAR(y, actualY, 1);
2668 }
2669};
2670
2671const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002672const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002673const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2675const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002676const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2677 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002678const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002679
2680// --- SwitchInputMapperTest ---
2681
2682class SwitchInputMapperTest : public InputMapperTest {
2683protected:
2684};
2685
2686TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002687 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002689 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002690}
2691
2692TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002693 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002695 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002696 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002698 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002699 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700}
2701
2702TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002703 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002704 std::list<NotifyArgs> out;
2705 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2706 ASSERT_TRUE(out.empty());
2707 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2708 ASSERT_TRUE(out.empty());
2709 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2710 ASSERT_TRUE(out.empty());
2711 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002713 ASSERT_EQ(1u, out.size());
2714 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002716 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2717 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002718 args.switchMask);
2719 ASSERT_EQ(uint32_t(0), args.policyFlags);
2720}
2721
Chris Ye87143712020-11-10 05:05:58 +00002722// --- VibratorInputMapperTest ---
2723class VibratorInputMapperTest : public InputMapperTest {
2724protected:
2725 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2726};
2727
2728TEST_F(VibratorInputMapperTest, GetSources) {
2729 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2730
2731 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2732}
2733
2734TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2735 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2736
2737 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2738}
2739
2740TEST_F(VibratorInputMapperTest, Vibrate) {
2741 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002742 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002743 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2744
2745 VibrationElement pattern(2);
2746 VibrationSequence sequence(2);
2747 pattern.duration = std::chrono::milliseconds(200);
2748 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2749 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2750 sequence.addElement(pattern);
2751 pattern.duration = std::chrono::milliseconds(500);
2752 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2753 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2754 sequence.addElement(pattern);
2755
2756 std::vector<int64_t> timings = {0, 1};
2757 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2758
2759 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002760 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002761 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002762 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002763 // Verify vibrator state listener was notified.
2764 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002765 ASSERT_EQ(1u, out.size());
2766 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2767 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2768 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002769 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002770 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002771 ASSERT_FALSE(mapper.isVibrating());
2772 // Verify vibrator state listener was notified.
2773 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002774 ASSERT_EQ(1u, out.size());
2775 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2776 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2777 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002778}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002779
Chris Yef59a2f42020-10-16 12:55:26 -07002780// --- SensorInputMapperTest ---
2781
2782class SensorInputMapperTest : public InputMapperTest {
2783protected:
2784 static const int32_t ACCEL_RAW_MIN;
2785 static const int32_t ACCEL_RAW_MAX;
2786 static const int32_t ACCEL_RAW_FUZZ;
2787 static const int32_t ACCEL_RAW_FLAT;
2788 static const int32_t ACCEL_RAW_RESOLUTION;
2789
2790 static const int32_t GYRO_RAW_MIN;
2791 static const int32_t GYRO_RAW_MAX;
2792 static const int32_t GYRO_RAW_FUZZ;
2793 static const int32_t GYRO_RAW_FLAT;
2794 static const int32_t GYRO_RAW_RESOLUTION;
2795
2796 static const float GRAVITY_MS2_UNIT;
2797 static const float DEGREE_RADIAN_UNIT;
2798
2799 void prepareAccelAxes();
2800 void prepareGyroAxes();
2801 void setAccelProperties();
2802 void setGyroProperties();
2803 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2804};
2805
2806const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2807const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2808const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2809const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2810const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2811
2812const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2813const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2814const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2815const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2816const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2817
2818const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2819const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2820
2821void SensorInputMapperTest::prepareAccelAxes() {
2822 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2823 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2824 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2825 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2826 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2827 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2828}
2829
2830void SensorInputMapperTest::prepareGyroAxes() {
2831 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2832 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2833 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2834 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2835 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2836 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2837}
2838
2839void SensorInputMapperTest::setAccelProperties() {
2840 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2841 /* sensorDataIndex */ 0);
2842 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2843 /* sensorDataIndex */ 1);
2844 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2845 /* sensorDataIndex */ 2);
2846 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2847 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2848 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2849 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2850 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2851}
2852
2853void SensorInputMapperTest::setGyroProperties() {
2854 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2855 /* sensorDataIndex */ 0);
2856 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2857 /* sensorDataIndex */ 1);
2858 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2859 /* sensorDataIndex */ 2);
2860 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2861 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2862 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2863 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2864 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2865}
2866
2867TEST_F(SensorInputMapperTest, GetSources) {
2868 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2869
2870 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2871}
2872
2873TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2874 setAccelProperties();
2875 prepareAccelAxes();
2876 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2877
2878 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2879 std::chrono::microseconds(10000),
2880 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002881 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2884 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002887
2888 NotifySensorArgs args;
2889 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2890 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2891 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2892
2893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2894 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2895 ASSERT_EQ(args.deviceId, DEVICE_ID);
2896 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2897 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2898 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2899 ASSERT_EQ(args.values, values);
2900 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2901}
2902
2903TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2904 setGyroProperties();
2905 prepareGyroAxes();
2906 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2907
2908 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2909 std::chrono::microseconds(10000),
2910 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002911 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2913 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2914 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002917
2918 NotifySensorArgs args;
2919 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2920 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2921 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2922
2923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2924 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2925 ASSERT_EQ(args.deviceId, DEVICE_ID);
2926 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2927 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2928 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2929 ASSERT_EQ(args.values, values);
2930 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2931}
2932
Michael Wrightd02c5b62014-02-10 15:10:22 -08002933// --- KeyboardInputMapperTest ---
2934
2935class KeyboardInputMapperTest : public InputMapperTest {
2936protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002937 const std::string UNIQUE_ID = "local:0";
2938
2939 void prepareDisplay(int32_t orientation);
2940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002941 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002942 int32_t originalKeyCode, int32_t rotatedKeyCode,
2943 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002944};
2945
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002946/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2947 * orientation.
2948 */
2949void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002950 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2951 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002952}
2953
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002954void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002955 int32_t originalScanCode, int32_t originalKeyCode,
2956 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002957 NotifyKeyArgs args;
2958
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2961 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2962 ASSERT_EQ(originalScanCode, args.scanCode);
2963 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002964 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002965
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2968 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2969 ASSERT_EQ(originalScanCode, args.scanCode);
2970 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002971 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002972}
2973
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002975 KeyboardInputMapper& mapper =
2976 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2977 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002978
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002979 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980}
2981
2982TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2983 const int32_t USAGE_A = 0x070004;
2984 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002985 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2986 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002987 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2988 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2989 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002991 KeyboardInputMapper& mapper =
2992 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2993 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002994 // Initial metastate is AMETA_NONE.
2995 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002996
2997 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999 NotifyKeyArgs args;
3000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3001 ASSERT_EQ(DEVICE_ID, args.deviceId);
3002 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3003 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3004 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3005 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3006 ASSERT_EQ(KEY_HOME, args.scanCode);
3007 ASSERT_EQ(AMETA_NONE, args.metaState);
3008 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3009 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3010 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3011
3012 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003013 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3015 ASSERT_EQ(DEVICE_ID, args.deviceId);
3016 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3017 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3018 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3019 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3020 ASSERT_EQ(KEY_HOME, args.scanCode);
3021 ASSERT_EQ(AMETA_NONE, args.metaState);
3022 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3023 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3024 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3025
3026 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3030 ASSERT_EQ(DEVICE_ID, args.deviceId);
3031 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3032 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3033 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3034 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3035 ASSERT_EQ(0, args.scanCode);
3036 ASSERT_EQ(AMETA_NONE, args.metaState);
3037 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3038 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3039 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3040
3041 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003042 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3043 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3045 ASSERT_EQ(DEVICE_ID, args.deviceId);
3046 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3047 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3048 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3049 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3050 ASSERT_EQ(0, args.scanCode);
3051 ASSERT_EQ(AMETA_NONE, args.metaState);
3052 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3053 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3054 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3055
3056 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3060 ASSERT_EQ(DEVICE_ID, args.deviceId);
3061 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3062 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3063 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3064 ASSERT_EQ(0, args.keyCode);
3065 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3066 ASSERT_EQ(AMETA_NONE, args.metaState);
3067 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3068 ASSERT_EQ(0U, args.policyFlags);
3069 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3070
3071 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3073 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3075 ASSERT_EQ(DEVICE_ID, args.deviceId);
3076 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3077 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3078 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3079 ASSERT_EQ(0, args.keyCode);
3080 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3081 ASSERT_EQ(AMETA_NONE, args.metaState);
3082 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3083 ASSERT_EQ(0U, args.policyFlags);
3084 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3085}
3086
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003087/**
3088 * Ensure that the readTime is set to the time when the EV_KEY is received.
3089 */
3090TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3091 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3092
3093 KeyboardInputMapper& mapper =
3094 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3095 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3096 NotifyKeyArgs args;
3097
3098 // Key down
3099 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3101 ASSERT_EQ(12, args.readTime);
3102
3103 // Key up
3104 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3106 ASSERT_EQ(15, args.readTime);
3107}
3108
Michael Wrightd02c5b62014-02-10 15:10:22 -08003109TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003110 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3111 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003112 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3113 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3114 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003116 KeyboardInputMapper& mapper =
3117 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3118 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003119
Arthur Hung95f68612022-04-07 14:08:22 +08003120 // Initial metastate is AMETA_NONE.
3121 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122
3123 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 NotifyKeyArgs args;
3126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3127 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003128 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003129 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003130
3131 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003132 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3134 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003135 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003136
3137 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003138 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3140 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003141 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003142
3143 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003144 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3146 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003147 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003148 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003149}
3150
3151TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003152 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3153 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3154 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3155 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003157 KeyboardInputMapper& mapper =
3158 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3159 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003161 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3163 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3164 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3165 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3166 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3167 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3168 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3169 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3170}
3171
3172TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003173 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3174 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3175 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3176 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003179 KeyboardInputMapper& mapper =
3180 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3181 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003183 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003184 ASSERT_NO_FATAL_FAILURE(
3185 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3186 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3187 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3188 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3189 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3190 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3191 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003193 clearViewports();
3194 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003195 ASSERT_NO_FATAL_FAILURE(
3196 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3197 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3198 AKEYCODE_DPAD_UP, DISPLAY_ID));
3199 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3200 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3201 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3202 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003203
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003204 clearViewports();
3205 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003206 ASSERT_NO_FATAL_FAILURE(
3207 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3208 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3209 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3210 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3211 AKEYCODE_DPAD_UP, DISPLAY_ID));
3212 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3213 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003215 clearViewports();
3216 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003217 ASSERT_NO_FATAL_FAILURE(
3218 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3219 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3220 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3221 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3222 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3223 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3224 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003225
3226 // Special case: if orientation changes while key is down, we still emit the same keycode
3227 // in the key up as we did in the key down.
3228 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003229 clearViewports();
3230 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3233 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3234 ASSERT_EQ(KEY_UP, args.scanCode);
3235 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3236
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003237 clearViewports();
3238 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3241 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3242 ASSERT_EQ(KEY_UP, args.scanCode);
3243 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3244}
3245
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003246TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3247 // If the keyboard is not orientation aware,
3248 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003250
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003251 KeyboardInputMapper& mapper =
3252 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3253 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003254 NotifyKeyArgs args;
3255
3256 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3261 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3262
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003263 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3268 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3269}
3270
3271TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3272 // If the keyboard is orientation aware,
3273 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003274 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003275
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003276 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003277 KeyboardInputMapper& mapper =
3278 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3279 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003280 NotifyKeyArgs args;
3281
3282 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3283 // ^--- already checked by the previous test
3284
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003285 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003286 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003287 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3291 ASSERT_EQ(DISPLAY_ID, args.displayId);
3292
3293 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003294 clearViewports();
3295 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003296 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3301 ASSERT_EQ(newDisplayId, args.displayId);
3302}
3303
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003305 KeyboardInputMapper& mapper =
3306 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3307 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003309 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003310 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003312 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003313 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314}
3315
Philip Junker4af3b3d2021-12-14 10:36:55 +01003316TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3317 KeyboardInputMapper& mapper =
3318 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3319 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3320
3321 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3322 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3323 << "If a mapping is available, the result is equal to the mapping";
3324
3325 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3326 << "If no mapping is available, the result is the key location";
3327}
3328
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003330 KeyboardInputMapper& mapper =
3331 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3332 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003334 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003335 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003337 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003339}
3340
3341TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003342 KeyboardInputMapper& mapper =
3343 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3344 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003346 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003349 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350 ASSERT_TRUE(flags[0]);
3351 ASSERT_FALSE(flags[1]);
3352}
3353
3354TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003355 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3356 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3357 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3358 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3359 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3360 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003362 KeyboardInputMapper& mapper =
3363 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3364 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003365 // Initial metastate is AMETA_NONE.
3366 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003367
3368 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003369 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3370 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3371 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372
3373 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003376 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3377 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3378 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003379 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380
3381 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003382 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003384 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3385 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3386 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003387 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003388
3389 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003392 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3393 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3394 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003395 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003396
3397 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003400 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3401 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3402 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003403 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404
3405 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003408 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3409 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3410 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412
3413 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003416 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3417 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3418 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003419 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420}
3421
Chris Yea52ade12020-08-27 16:49:20 -07003422TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3423 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3424 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3425 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3426 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3427
3428 KeyboardInputMapper& mapper =
3429 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3430 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3431
Chris Yea52ade12020-08-27 16:49:20 -07003432 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003433 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003434 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3435 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3436 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3437 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3438
3439 NotifyKeyArgs args;
3440 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3443 ASSERT_EQ(AMETA_NONE, args.metaState);
3444 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3445 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3446 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3447
3448 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003449 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3451 ASSERT_EQ(AMETA_NONE, args.metaState);
3452 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3453 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3454 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3455}
3456
Arthur Hung2c9a3342019-07-23 14:18:59 +08003457TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3458 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3460 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3461 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3462 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003463
3464 // keyboard 2.
3465 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003466 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003467 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003468 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003469 std::shared_ptr<InputDevice> device2 =
3470 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003471 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003472
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003473 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3474 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3475 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3476 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003477
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003478 KeyboardInputMapper& mapper =
3479 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3480 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003481
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003483 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003484 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003485 std::list<NotifyArgs> unused =
3486 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3487 0 /*changes*/);
3488 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003489
3490 // Prepared displays and associated info.
3491 constexpr uint8_t hdmi1 = 0;
3492 constexpr uint8_t hdmi2 = 1;
3493 const std::string SECONDARY_UNIQUE_ID = "local:1";
3494
3495 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3496 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3497
3498 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003499 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3500 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003501 ASSERT_FALSE(device2->isEnabled());
3502
3503 // Prepare second display.
3504 constexpr int32_t newDisplayId = 2;
3505 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003506 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003507 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003508 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003509 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003510 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3511 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003512
3513 // Device should be enabled after the associated display is found.
3514 ASSERT_TRUE(mDevice->isEnabled());
3515 ASSERT_TRUE(device2->isEnabled());
3516
3517 // Test pad key events
3518 ASSERT_NO_FATAL_FAILURE(
3519 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3520 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3521 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3522 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3523 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3524 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3525 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3526
3527 ASSERT_NO_FATAL_FAILURE(
3528 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3529 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3530 AKEYCODE_DPAD_RIGHT, newDisplayId));
3531 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3532 AKEYCODE_DPAD_DOWN, newDisplayId));
3533 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3534 AKEYCODE_DPAD_LEFT, newDisplayId));
3535}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003536
arthurhungc903df12020-08-11 15:08:42 +08003537TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3538 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3539 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3540 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3541 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3542 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3543 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3544
3545 KeyboardInputMapper& mapper =
3546 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3547 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003548 // Initial metastate is AMETA_NONE.
3549 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003550
3551 // Initialization should have turned all of the lights off.
3552 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3553 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3554 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3555
3556 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003559 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3560 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3561
3562 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003565 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3566 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3567
3568 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003571 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3572 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3573
3574 mFakeEventHub->removeDevice(EVENTHUB_ID);
3575 mReader->loopOnce();
3576
3577 // keyboard 2 should default toggle keys.
3578 const std::string USB2 = "USB2";
3579 const std::string DEVICE_NAME2 = "KEYBOARD2";
3580 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3581 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3582 std::shared_ptr<InputDevice> device2 =
3583 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003584 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003585 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3586 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3587 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3588 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3589 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3590 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3591
arthurhung6fe95782020-10-05 22:41:16 +08003592 KeyboardInputMapper& mapper2 =
3593 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3594 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003595 std::list<NotifyArgs> unused =
3596 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3597 0 /*changes*/);
3598 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003599
3600 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3601 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3602 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003603 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3604 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003605}
3606
Arthur Hungcb40a002021-08-03 14:31:01 +00003607TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3608 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3609 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3610 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3611
3612 // Suppose we have two mappers. (DPAD + KEYBOARD)
3613 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3614 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3615 KeyboardInputMapper& mapper =
3616 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3617 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003618 // Initial metastate is AMETA_NONE.
3619 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003620
3621 mReader->toggleCapsLockState(DEVICE_ID);
3622 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3623}
3624
Arthur Hungfb3cc112022-04-13 07:39:50 +00003625TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3626 // keyboard 1.
3627 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3628 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3629 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3630 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3631 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3632 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3633
3634 KeyboardInputMapper& mapper1 =
3635 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3636 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3637
3638 // keyboard 2.
3639 const std::string USB2 = "USB2";
3640 const std::string DEVICE_NAME2 = "KEYBOARD2";
3641 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3642 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3643 std::shared_ptr<InputDevice> device2 =
3644 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3645 ftl::Flags<InputDeviceClass>(0));
3646 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3647 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3648 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3649 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3650 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3651 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3652
3653 KeyboardInputMapper& mapper2 =
3654 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3655 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003656 std::list<NotifyArgs> unused =
3657 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3658 0 /*changes*/);
3659 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003660
Arthur Hung95f68612022-04-07 14:08:22 +08003661 // Initial metastate is AMETA_NONE.
3662 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3663 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3664
3665 // Toggle num lock on and off.
3666 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3667 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003668 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3669 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3670 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3671
3672 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3673 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3674 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3675 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3676 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3677
3678 // Toggle caps lock on and off.
3679 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3680 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3681 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3682 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3683 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3684
3685 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3686 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3687 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3688 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3689 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3690
3691 // Toggle scroll lock on and off.
3692 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3693 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3694 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3695 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3696 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3697
3698 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3699 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3700 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3701 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3702 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3703}
3704
Arthur Hung2141d542022-08-23 07:45:21 +00003705TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3706 const int32_t USAGE_A = 0x070004;
3707 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3708 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3709
3710 KeyboardInputMapper& mapper =
3711 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3712 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3713 // Key down by scan code.
3714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3715 NotifyKeyArgs args;
3716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3717 ASSERT_EQ(DEVICE_ID, args.deviceId);
3718 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3719 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3720 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3721 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3722 ASSERT_EQ(KEY_HOME, args.scanCode);
3723 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3724
3725 // Disable device, it should synthesize cancellation events for down events.
3726 mFakePolicy->addDisabledDevice(DEVICE_ID);
3727 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3728
3729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3730 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3731 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3732 ASSERT_EQ(KEY_HOME, args.scanCode);
3733 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3734}
3735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003736// --- KeyboardInputMapperTest_ExternalDevice ---
3737
3738class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3739protected:
Chris Yea52ade12020-08-27 16:49:20 -07003740 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003741};
3742
3743TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003744 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3745 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003746
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003747 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3749 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3750 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003752 KeyboardInputMapper& mapper =
3753 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3754 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003755
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003756 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003757 NotifyKeyArgs args;
3758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3759 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3760
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003761 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3763 ASSERT_EQ(uint32_t(0), args.policyFlags);
3764
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3767 ASSERT_EQ(uint32_t(0), args.policyFlags);
3768
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3771 ASSERT_EQ(uint32_t(0), args.policyFlags);
3772
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3775 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3776
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3779 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3780}
3781
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003782TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003783 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003784
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3787 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003788
Powei Fengd041c5d2019-05-03 17:11:33 -07003789 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 KeyboardInputMapper& mapper =
3791 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3792 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003795 NotifyKeyArgs args;
3796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3797 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3798
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003799 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3801 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3802
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3805 ASSERT_EQ(uint32_t(0), args.policyFlags);
3806
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003807 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3809 ASSERT_EQ(uint32_t(0), args.policyFlags);
3810
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3813 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3814
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003815 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3817 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3818}
3819
Michael Wrightd02c5b62014-02-10 15:10:22 -08003820// --- CursorInputMapperTest ---
3821
3822class CursorInputMapperTest : public InputMapperTest {
3823protected:
3824 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3825
Michael Wright17db18e2020-06-26 20:51:44 +01003826 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003827
Chris Yea52ade12020-08-27 16:49:20 -07003828 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829 InputMapperTest::SetUp();
3830
Michael Wright17db18e2020-06-26 20:51:44 +01003831 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003832 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003833 }
3834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003835 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3836 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003837
3838 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003839 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3840 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3841 }
3842
3843 void prepareSecondaryDisplay() {
3844 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3845 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
3846 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003847 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003848
3849 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3850 float pressure) {
3851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3852 0.0f, 0.0f, 0.0f, EPSILON));
3853 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854};
3855
3856const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3857
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003858void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3859 int32_t originalY, int32_t rotatedX,
3860 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861 NotifyMotionArgs args;
3862
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3865 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003868 ASSERT_NO_FATAL_FAILURE(
3869 assertCursorPointerCoords(args.pointerCoords[0],
3870 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3871 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872}
3873
3874TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003875 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003876 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003878 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879}
3880
3881TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003883 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886}
3887
3888TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003889 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003890 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891
3892 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003893 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003894
3895 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003896 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3897 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3899 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3900
3901 // When the bounds are set, then there should be a valid motion range.
3902 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3903
3904 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003905 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906
3907 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3908 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3909 1, 800 - 1, 0.0f, 0.0f));
3910 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3911 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3912 2, 480 - 1, 0.0f, 0.0f));
3913 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3914 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3915 0.0f, 1.0f, 0.0f, 0.0f));
3916}
3917
3918TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003919 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003920 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003921
3922 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003923 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924
3925 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3926 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3927 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3928 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3929 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3930 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3931 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3932 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3933 0.0f, 1.0f, 0.0f, 0.0f));
3934}
3935
3936TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003938 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003939
arthurhungdcef2dc2020-08-11 14:47:50 +08003940 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003941
3942 NotifyMotionArgs args;
3943
3944 // Button press.
3945 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3949 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3950 ASSERT_EQ(DEVICE_ID, args.deviceId);
3951 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3952 ASSERT_EQ(uint32_t(0), args.policyFlags);
3953 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3954 ASSERT_EQ(0, args.flags);
3955 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3956 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3957 ASSERT_EQ(0, args.edgeFlags);
3958 ASSERT_EQ(uint32_t(1), args.pointerCount);
3959 ASSERT_EQ(0, args.pointerProperties[0].id);
3960 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003961 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3963 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3964 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3965
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3967 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3968 ASSERT_EQ(DEVICE_ID, args.deviceId);
3969 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3970 ASSERT_EQ(uint32_t(0), args.policyFlags);
3971 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3972 ASSERT_EQ(0, args.flags);
3973 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3974 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3975 ASSERT_EQ(0, args.edgeFlags);
3976 ASSERT_EQ(uint32_t(1), args.pointerCount);
3977 ASSERT_EQ(0, args.pointerProperties[0].id);
3978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003979 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003980 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3981 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3982 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3983
Michael Wrightd02c5b62014-02-10 15:10:22 -08003984 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003985 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3986 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3988 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3989 ASSERT_EQ(DEVICE_ID, args.deviceId);
3990 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3991 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003992 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3993 ASSERT_EQ(0, args.flags);
3994 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3995 ASSERT_EQ(0, args.buttonState);
3996 ASSERT_EQ(0, args.edgeFlags);
3997 ASSERT_EQ(uint32_t(1), args.pointerCount);
3998 ASSERT_EQ(0, args.pointerProperties[0].id);
3999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004000 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004001 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4002 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4003 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4004
4005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4006 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4007 ASSERT_EQ(DEVICE_ID, args.deviceId);
4008 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4009 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4011 ASSERT_EQ(0, args.flags);
4012 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4013 ASSERT_EQ(0, args.buttonState);
4014 ASSERT_EQ(0, args.edgeFlags);
4015 ASSERT_EQ(uint32_t(1), args.pointerCount);
4016 ASSERT_EQ(0, args.pointerProperties[0].id);
4017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004018 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4020 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4021 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4022}
4023
4024TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004025 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004026 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027
4028 NotifyMotionArgs args;
4029
4030 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4032 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004035 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4036 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4037 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004038
4039 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004040 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4041 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004044 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4045 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046}
4047
4048TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004049 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004050 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004051
4052 NotifyMotionArgs args;
4053
4054 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4058 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004059 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4062 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004063 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004064
Michael Wrightd02c5b62014-02-10 15:10:22 -08004065 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004066 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004070 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004071
4072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004073 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004074 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004075}
4076
4077TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004079 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080
4081 NotifyMotionArgs args;
4082
4083 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004084 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4089 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004090 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4091 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4092 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004093
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4095 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004096 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4097 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4098 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004099
Michael Wrightd02c5b62014-02-10 15:10:22 -08004100 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4105 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004106 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4107 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4108 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004109
4110 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004114 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004115 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004116
4117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004118 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004119 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120}
4121
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004122TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004123 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004125 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4126 // need to be rotated.
4127 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004128 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004129
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004130 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004131 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4132 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4133 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4134 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4135 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4136 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4137 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4138 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4139}
4140
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004141TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004142 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004144 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4145 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004146 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004147
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004148 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004149 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004150 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4151 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4152 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4153 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4154 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4155 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4156 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4157 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4158
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004159 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004160 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004161 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4162 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4163 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4164 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4165 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4166 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4167 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4168 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004170 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004171 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4173 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4174 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4175 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4176 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4177 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4178 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4179 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4180
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004181 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004182 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004183 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4184 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4185 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4186 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4187 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4188 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4189 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4190 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191}
4192
4193TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004194 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004195 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004196
4197 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4198 mFakePointerController->setPosition(100, 200);
4199 mFakePointerController->setButtonState(0);
4200
4201 NotifyMotionArgs motionArgs;
4202 NotifyKeyArgs keyArgs;
4203
4204 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4208 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4209 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4210 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004211 ASSERT_NO_FATAL_FAILURE(
4212 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4216 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4217 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004218 ASSERT_NO_FATAL_FAILURE(
4219 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004220
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004221 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4222 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 ASSERT_EQ(0, motionArgs.buttonState);
4226 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004227 ASSERT_NO_FATAL_FAILURE(
4228 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004229
4230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004231 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_EQ(0, motionArgs.buttonState);
4233 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004234 ASSERT_NO_FATAL_FAILURE(
4235 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004236
4237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004239 ASSERT_EQ(0, motionArgs.buttonState);
4240 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(
4242 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243
4244 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4250 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4251 motionArgs.buttonState);
4252 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4253 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004254 ASSERT_NO_FATAL_FAILURE(
4255 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4258 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4259 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4260 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4261 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004262 ASSERT_NO_FATAL_FAILURE(
4263 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264
4265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4267 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4268 motionArgs.buttonState);
4269 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4270 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004271 ASSERT_NO_FATAL_FAILURE(
4272 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004273
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004277 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4279 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(
4281 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282
4283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004285 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4286 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004287 ASSERT_NO_FATAL_FAILURE(
4288 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4294 ASSERT_EQ(0, motionArgs.buttonState);
4295 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004296 ASSERT_NO_FATAL_FAILURE(
4297 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 ASSERT_EQ(0, motionArgs.buttonState);
4303 ASSERT_EQ(0, mFakePointerController->getButtonState());
4304 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(
4306 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004307
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4309 ASSERT_EQ(0, motionArgs.buttonState);
4310 ASSERT_EQ(0, mFakePointerController->getButtonState());
4311 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004312 ASSERT_NO_FATAL_FAILURE(
4313 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314
4315 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4319 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4320 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004321
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4325 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004326 ASSERT_NO_FATAL_FAILURE(
4327 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004328
4329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4330 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4331 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4332 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004333 ASSERT_NO_FATAL_FAILURE(
4334 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 ASSERT_EQ(0, motionArgs.buttonState);
4341 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004342 ASSERT_NO_FATAL_FAILURE(
4343 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004344
4345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347 ASSERT_EQ(0, motionArgs.buttonState);
4348 ASSERT_EQ(0, mFakePointerController->getButtonState());
4349
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004350 ASSERT_NO_FATAL_FAILURE(
4351 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4354 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4355
4356 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4360 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4361 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004362
Michael Wrightd02c5b62014-02-10 15:10:22 -08004363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4366 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004367 ASSERT_NO_FATAL_FAILURE(
4368 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004369
4370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4371 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4372 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4373 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004374 ASSERT_NO_FATAL_FAILURE(
4375 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 ASSERT_EQ(0, motionArgs.buttonState);
4382 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004383 ASSERT_NO_FATAL_FAILURE(
4384 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004385
4386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4388 ASSERT_EQ(0, motionArgs.buttonState);
4389 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004390 ASSERT_NO_FATAL_FAILURE(
4391 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004392
Michael Wrightd02c5b62014-02-10 15:10:22 -08004393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4395 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4396
4397 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004398 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4401 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4402 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004403
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004405 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4407 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004408 ASSERT_NO_FATAL_FAILURE(
4409 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
4411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4413 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4414 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004415 ASSERT_NO_FATAL_FAILURE(
4416 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004421 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 ASSERT_EQ(0, motionArgs.buttonState);
4423 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004424 ASSERT_NO_FATAL_FAILURE(
4425 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004426
4427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4429 ASSERT_EQ(0, motionArgs.buttonState);
4430 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004431 ASSERT_NO_FATAL_FAILURE(
4432 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4435 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4436 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4437
4438 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4442 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4443 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004444
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004446 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4448 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004449 ASSERT_NO_FATAL_FAILURE(
4450 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004451
4452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4453 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4454 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4455 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004456 ASSERT_NO_FATAL_FAILURE(
4457 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004462 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463 ASSERT_EQ(0, motionArgs.buttonState);
4464 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004465 ASSERT_NO_FATAL_FAILURE(
4466 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467
4468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4469 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4470 ASSERT_EQ(0, motionArgs.buttonState);
4471 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004472 ASSERT_NO_FATAL_FAILURE(
4473 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004474
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4476 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4477 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4478}
4479
4480TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004482 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004483
4484 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4485 mFakePointerController->setPosition(100, 200);
4486 mFakePointerController->setButtonState(0);
4487
4488 NotifyMotionArgs args;
4489
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004494 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4495 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4497 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 +01004498 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004499}
4500
4501TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004502 addConfigurationProperty("cursor.mode", "pointer");
4503 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004504 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004505
4506 NotifyDeviceResetArgs resetArgs;
4507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4508 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4509 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4510
4511 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4512 mFakePointerController->setPosition(100, 200);
4513 mFakePointerController->setButtonState(0);
4514
4515 NotifyMotionArgs args;
4516
4517 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4522 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4525 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 +01004526 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004527
4528 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4532 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4533 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4535 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4537 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4538 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4540 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4541
4542 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004543 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4544 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4546 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4549 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4551 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4552 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4554 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4555
4556 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4561 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4564 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 +01004565 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004566
4567 // Disable pointer capture and check that the device generation got bumped
4568 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004569 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004570 mFakePolicy->setPointerCapture(false);
4571 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004572 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004573
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004575 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4576
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4581 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4584 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 +01004585 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586}
4587
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004588/**
4589 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4590 * pointer acceleration or speed processing should not be applied.
4591 */
4592TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4593 addConfigurationProperty("cursor.mode", "pointer");
4594 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4595 100.f /*high threshold*/, 10.f /*acceleration*/);
4596 mFakePolicy->setVelocityControlParams(testParams);
4597 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4598
4599 NotifyDeviceResetArgs resetArgs;
4600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4601 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4602 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4603
4604 NotifyMotionArgs args;
4605
4606 // Move and verify scale is applied.
4607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4611 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4613 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4614 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4615 ASSERT_GT(relX, 10);
4616 ASSERT_GT(relY, 20);
4617
4618 // Enable Pointer Capture
4619 mFakePolicy->setPointerCapture(true);
4620 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4621 NotifyPointerCaptureChangedArgs captureArgs;
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4623 ASSERT_TRUE(captureArgs.request.enable);
4624
4625 // Move and verify scale is not applied.
4626 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4628 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4630 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4632 ASSERT_EQ(10, args.pointerCoords[0].getX());
4633 ASSERT_EQ(20, args.pointerCoords[0].getY());
4634}
4635
Prabir Pradhan208360b2022-06-24 18:37:04 +00004636TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4637 addConfigurationProperty("cursor.mode", "pointer");
4638 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4639
4640 NotifyDeviceResetArgs resetArgs;
4641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4642 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4643 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4644
4645 // Ensure the display is rotated.
4646 prepareDisplay(DISPLAY_ORIENTATION_90);
4647
4648 NotifyMotionArgs args;
4649
4650 // Verify that the coordinates are rotated.
4651 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4653 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4655 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4657 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4658 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4659
4660 // Enable Pointer Capture.
4661 mFakePolicy->setPointerCapture(true);
4662 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4663 NotifyPointerCaptureChangedArgs captureArgs;
4664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4665 ASSERT_TRUE(captureArgs.request.enable);
4666
4667 // Move and verify rotation is not applied.
4668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4672 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4674 ASSERT_EQ(10, args.pointerCoords[0].getX());
4675 ASSERT_EQ(20, args.pointerCoords[0].getY());
4676}
4677
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004678TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004679 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004680
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004681 // Set up the default display.
4682 prepareDisplay(DISPLAY_ORIENTATION_90);
4683
4684 // Set up the secondary display as the display on which the pointer should be shown.
4685 // The InputDevice is not associated with any display.
4686 prepareSecondaryDisplay();
4687 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004688 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4689
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004690 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004691 mFakePointerController->setPosition(100, 200);
4692 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004693
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004694 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004699 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4700 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4701 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01004702 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004703}
4704
4705TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4706 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4707
4708 // Set up the default display.
4709 prepareDisplay(DISPLAY_ORIENTATION_90);
4710
4711 // Set up the secondary display as the display on which the pointer should be shown,
4712 // and associate the InputDevice with the secondary display.
4713 prepareSecondaryDisplay();
4714 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4715 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4716 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4717
4718 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4719 mFakePointerController->setPosition(100, 200);
4720 mFakePointerController->setButtonState(0);
4721
4722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004726 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4727 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4728 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004729 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
4730}
4731
4732TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4733 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4734
4735 // Set up the default display as the display on which the pointer should be shown.
4736 prepareDisplay(DISPLAY_ORIENTATION_90);
4737 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4738
4739 // Associate the InputDevice with the secondary display.
4740 prepareSecondaryDisplay();
4741 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4742 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4743
4744 // The mapper should not generate any events because it is associated with a display that is
4745 // different from the pointer display.
4746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004750}
4751
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004752// --- BluetoothCursorInputMapperTest ---
4753
4754class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4755protected:
4756 void SetUp() override {
4757 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4758
4759 mFakePointerController = std::make_shared<FakePointerController>();
4760 mFakePolicy->setPointerController(mFakePointerController);
4761 }
4762};
4763
4764TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4765 addConfigurationProperty("cursor.mode", "pointer");
4766 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4767
4768 nsecs_t kernelEventTime = ARBITRARY_TIME;
4769 nsecs_t expectedEventTime = ARBITRARY_TIME;
4770 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4771 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4773 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4774 WithEventTime(expectedEventTime))));
4775
4776 // Process several events that come in quick succession, according to their timestamps.
4777 for (int i = 0; i < 3; i++) {
4778 constexpr static nsecs_t delta = ms2ns(1);
4779 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4780 kernelEventTime += delta;
4781 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4782
4783 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4784 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4786 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4787 WithEventTime(expectedEventTime))));
4788 }
4789}
4790
4791TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4792 addConfigurationProperty("cursor.mode", "pointer");
4793 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4794
4795 nsecs_t expectedEventTime = ARBITRARY_TIME;
4796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4797 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4799 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4800 WithEventTime(expectedEventTime))));
4801
4802 // Process several events with the same timestamp from the kernel.
4803 // Ensure that we do not generate events too far into the future.
4804 constexpr static int32_t numEvents =
4805 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4806 for (int i = 0; i < numEvents; i++) {
4807 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4808
4809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4812 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4813 WithEventTime(expectedEventTime))));
4814 }
4815
4816 // By processing more events with the same timestamp, we should not generate events with a
4817 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4818 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4819 for (int i = 0; i < 3; i++) {
4820 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4823 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4824 WithEventTime(cappedEventTime))));
4825 }
4826}
4827
4828TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4829 addConfigurationProperty("cursor.mode", "pointer");
4830 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4831
4832 nsecs_t kernelEventTime = ARBITRARY_TIME;
4833 nsecs_t expectedEventTime = ARBITRARY_TIME;
4834 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4835 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4837 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4838 WithEventTime(expectedEventTime))));
4839
4840 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4841 // smoothening is not needed, its timestamp is not affected.
4842 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4843 expectedEventTime = kernelEventTime;
4844
4845 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4846 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4848 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4849 WithEventTime(expectedEventTime))));
4850}
4851
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852// --- TouchInputMapperTest ---
4853
4854class TouchInputMapperTest : public InputMapperTest {
4855protected:
4856 static const int32_t RAW_X_MIN;
4857 static const int32_t RAW_X_MAX;
4858 static const int32_t RAW_Y_MIN;
4859 static const int32_t RAW_Y_MAX;
4860 static const int32_t RAW_TOUCH_MIN;
4861 static const int32_t RAW_TOUCH_MAX;
4862 static const int32_t RAW_TOOL_MIN;
4863 static const int32_t RAW_TOOL_MAX;
4864 static const int32_t RAW_PRESSURE_MIN;
4865 static const int32_t RAW_PRESSURE_MAX;
4866 static const int32_t RAW_ORIENTATION_MIN;
4867 static const int32_t RAW_ORIENTATION_MAX;
4868 static const int32_t RAW_DISTANCE_MIN;
4869 static const int32_t RAW_DISTANCE_MAX;
4870 static const int32_t RAW_TILT_MIN;
4871 static const int32_t RAW_TILT_MAX;
4872 static const int32_t RAW_ID_MIN;
4873 static const int32_t RAW_ID_MAX;
4874 static const int32_t RAW_SLOT_MIN;
4875 static const int32_t RAW_SLOT_MAX;
4876 static const float X_PRECISION;
4877 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004878 static const float X_PRECISION_VIRTUAL;
4879 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880
4881 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004882 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004883
4884 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4885
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004886 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004887 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004888
Michael Wrightd02c5b62014-02-10 15:10:22 -08004889 enum Axes {
4890 POSITION = 1 << 0,
4891 TOUCH = 1 << 1,
4892 TOOL = 1 << 2,
4893 PRESSURE = 1 << 3,
4894 ORIENTATION = 1 << 4,
4895 MINOR = 1 << 5,
4896 ID = 1 << 6,
4897 DISTANCE = 1 << 7,
4898 TILT = 1 << 8,
4899 SLOT = 1 << 9,
4900 TOOL_TYPE = 1 << 10,
4901 };
4902
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004903 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
4904 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004905 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004906 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004907 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 int32_t toRawX(float displayX);
4909 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004910 int32_t toRotatedRawX(float displayX);
4911 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004912 float toCookedX(float rawX, float rawY);
4913 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004915 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004917 float toDisplayY(int32_t rawY, int32_t displayHeight);
4918
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919};
4920
4921const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4922const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4923const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4924const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4925const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4926const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4927const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4928const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004929const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4930const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004931const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4932const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4933const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4934const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4935const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4936const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4937const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4938const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4939const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4940const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4941const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4942const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004943const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4944 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4945const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4946 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004947const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4948 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949
4950const float TouchInputMapperTest::GEOMETRIC_SCALE =
4951 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4952 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4953
4954const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4955 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4956 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4957};
4958
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004959void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004960 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4961 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004962}
4963
4964void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4965 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
4966 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967}
4968
Santos Cordonfa5cf462017-04-05 10:37:00 -07004969void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004970 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4971 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4972 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004973}
4974
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004976 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4977 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4978 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4979 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980}
4981
Jason Gerecke489fda82012-09-07 17:19:40 -07004982void TouchInputMapperTest::prepareLocationCalibration() {
4983 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4984}
4985
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986int32_t TouchInputMapperTest::toRawX(float displayX) {
4987 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4988}
4989
4990int32_t TouchInputMapperTest::toRawY(float displayY) {
4991 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4992}
4993
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004994int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4995 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4996}
4997
4998int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4999 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5000}
5001
Jason Gerecke489fda82012-09-07 17:19:40 -07005002float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5003 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5004 return rawX;
5005}
5006
5007float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5008 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5009 return rawY;
5010}
5011
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005013 return toDisplayX(rawX, DISPLAY_WIDTH);
5014}
5015
5016float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5017 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018}
5019
5020float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005021 return toDisplayY(rawY, DISPLAY_HEIGHT);
5022}
5023
5024float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5025 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005026}
5027
5028
5029// --- SingleTouchInputMapperTest ---
5030
5031class SingleTouchInputMapperTest : public TouchInputMapperTest {
5032protected:
5033 void prepareButtons();
5034 void prepareAxes(int axes);
5035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005036 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5037 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5038 void processUp(SingleTouchInputMapper& mappery);
5039 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5040 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5041 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5042 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5043 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5044 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045};
5046
5047void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005048 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049}
5050
5051void SingleTouchInputMapperTest::prepareAxes(int axes) {
5052 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005053 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5054 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 }
5056 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005057 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5058 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059 }
5060 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005061 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5062 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063 }
5064 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005065 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5066 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005067 }
5068 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005069 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005071 }
5072}
5073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005074void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078}
5079
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005080void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5082 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005083}
5084
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005085void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087}
5088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005089void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005091}
5092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005093void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5094 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005096}
5097
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005098void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100}
5101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005102void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5103 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005106}
5107
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005108void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5109 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111}
5112
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005113void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115}
5116
Michael Wrightd02c5b62014-02-10 15:10:22 -08005117TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 prepareButtons();
5119 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005120 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005121
Harry Cutts16a24cc2022-10-26 15:22:19 +00005122 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123}
5124
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005126 prepareButtons();
5127 prepareAxes(POSITION);
5128 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005129 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005130
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005131 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132}
5133
5134TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005135 addConfigurationProperty("touch.deviceType", "touchScreen");
5136 prepareDisplay(DISPLAY_ORIENTATION_0);
5137 prepareButtons();
5138 prepareAxes(POSITION);
5139 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005140 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005141
5142 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005143 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144
5145 // Virtual key is down.
5146 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5147 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5148 processDown(mapper, x, y);
5149 processSync(mapper);
5150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005152 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153
5154 // Virtual key is up.
5155 processUp(mapper);
5156 processSync(mapper);
5157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005159 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005160}
5161
5162TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005163 addConfigurationProperty("touch.deviceType", "touchScreen");
5164 prepareDisplay(DISPLAY_ORIENTATION_0);
5165 prepareButtons();
5166 prepareAxes(POSITION);
5167 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005168 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005169
5170 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005171 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172
5173 // Virtual key is down.
5174 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5175 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5176 processDown(mapper, x, y);
5177 processSync(mapper);
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5179
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005180 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181
5182 // Virtual key is up.
5183 processUp(mapper);
5184 processSync(mapper);
5185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5186
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005187 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188}
5189
5190TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005191 addConfigurationProperty("touch.deviceType", "touchScreen");
5192 prepareDisplay(DISPLAY_ORIENTATION_0);
5193 prepareButtons();
5194 prepareAxes(POSITION);
5195 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005196 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005197
Michael Wrightd02c5b62014-02-10 15:10:22 -08005198 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005199 ASSERT_TRUE(
5200 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005201 ASSERT_TRUE(flags[0]);
5202 ASSERT_FALSE(flags[1]);
5203}
5204
5205TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005206 addConfigurationProperty("touch.deviceType", "touchScreen");
5207 prepareDisplay(DISPLAY_ORIENTATION_0);
5208 prepareButtons();
5209 prepareAxes(POSITION);
5210 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005211 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005212
arthurhungdcef2dc2020-08-11 14:47:50 +08005213 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005214
5215 NotifyKeyArgs args;
5216
5217 // Press virtual key.
5218 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5219 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5220 processDown(mapper, x, y);
5221 processSync(mapper);
5222
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5224 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5225 ASSERT_EQ(DEVICE_ID, args.deviceId);
5226 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5227 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5228 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5229 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5230 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5231 ASSERT_EQ(KEY_HOME, args.scanCode);
5232 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5233 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5234
5235 // Release virtual key.
5236 processUp(mapper);
5237 processSync(mapper);
5238
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5240 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5241 ASSERT_EQ(DEVICE_ID, args.deviceId);
5242 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5243 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5244 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5245 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5246 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5247 ASSERT_EQ(KEY_HOME, args.scanCode);
5248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5249 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5250
5251 // Should not have sent any motions.
5252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5253}
5254
5255TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256 addConfigurationProperty("touch.deviceType", "touchScreen");
5257 prepareDisplay(DISPLAY_ORIENTATION_0);
5258 prepareButtons();
5259 prepareAxes(POSITION);
5260 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005261 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005262
arthurhungdcef2dc2020-08-11 14:47:50 +08005263 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005264
5265 NotifyKeyArgs keyArgs;
5266
5267 // Press virtual key.
5268 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5269 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5270 processDown(mapper, x, y);
5271 processSync(mapper);
5272
5273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5274 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5275 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5276 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5277 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5278 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5279 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5280 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5281 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5282 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5283 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5284
5285 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5286 // into the display area.
5287 y -= 100;
5288 processMove(mapper, x, y);
5289 processSync(mapper);
5290
5291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5292 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5293 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5294 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5295 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5296 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5297 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5298 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5299 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5300 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5301 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5302 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5303
5304 NotifyMotionArgs motionArgs;
5305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5306 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5307 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5308 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5309 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5310 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5311 ASSERT_EQ(0, motionArgs.flags);
5312 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5313 ASSERT_EQ(0, motionArgs.buttonState);
5314 ASSERT_EQ(0, motionArgs.edgeFlags);
5315 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5316 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5317 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5319 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5320 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5321 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5322 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5323
5324 // Keep moving out of bounds. Should generate a pointer move.
5325 y -= 50;
5326 processMove(mapper, x, y);
5327 processSync(mapper);
5328
5329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5330 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5331 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5332 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5333 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5335 ASSERT_EQ(0, motionArgs.flags);
5336 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5337 ASSERT_EQ(0, motionArgs.buttonState);
5338 ASSERT_EQ(0, motionArgs.edgeFlags);
5339 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5340 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5342 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5343 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5344 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5345 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5347
5348 // Release out of bounds. Should generate a pointer up.
5349 processUp(mapper);
5350 processSync(mapper);
5351
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5353 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5354 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5355 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5356 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5357 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5358 ASSERT_EQ(0, motionArgs.flags);
5359 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5360 ASSERT_EQ(0, motionArgs.buttonState);
5361 ASSERT_EQ(0, motionArgs.edgeFlags);
5362 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5363 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5364 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5366 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5367 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5368 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5370
5371 // Should not have sent any more keys or motions.
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5374}
5375
5376TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377 addConfigurationProperty("touch.deviceType", "touchScreen");
5378 prepareDisplay(DISPLAY_ORIENTATION_0);
5379 prepareButtons();
5380 prepareAxes(POSITION);
5381 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005382 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383
arthurhungdcef2dc2020-08-11 14:47:50 +08005384 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005385
5386 NotifyMotionArgs motionArgs;
5387
5388 // Initially go down out of bounds.
5389 int32_t x = -10;
5390 int32_t y = -10;
5391 processDown(mapper, x, y);
5392 processSync(mapper);
5393
5394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5395
5396 // Move into the display area. Should generate a pointer down.
5397 x = 50;
5398 y = 75;
5399 processMove(mapper, x, y);
5400 processSync(mapper);
5401
5402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5403 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5404 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5405 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5406 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5407 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5408 ASSERT_EQ(0, motionArgs.flags);
5409 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5410 ASSERT_EQ(0, motionArgs.buttonState);
5411 ASSERT_EQ(0, motionArgs.edgeFlags);
5412 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5413 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5414 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5416 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5417 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5418 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5419 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5420
5421 // Release. Should generate a pointer up.
5422 processUp(mapper);
5423 processSync(mapper);
5424
5425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5426 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5427 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5428 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5429 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5430 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5431 ASSERT_EQ(0, motionArgs.flags);
5432 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5433 ASSERT_EQ(0, motionArgs.buttonState);
5434 ASSERT_EQ(0, motionArgs.edgeFlags);
5435 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5436 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5439 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5440 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5441 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5442 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5443
5444 // Should not have sent any more keys or motions.
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5447}
5448
Santos Cordonfa5cf462017-04-05 10:37:00 -07005449TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005450 addConfigurationProperty("touch.deviceType", "touchScreen");
5451 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5452
5453 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5454 prepareButtons();
5455 prepareAxes(POSITION);
5456 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005457 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005458
arthurhungdcef2dc2020-08-11 14:47:50 +08005459 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005460
5461 NotifyMotionArgs motionArgs;
5462
5463 // Down.
5464 int32_t x = 100;
5465 int32_t y = 125;
5466 processDown(mapper, x, y);
5467 processSync(mapper);
5468
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5470 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5471 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5472 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5473 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5474 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5476 ASSERT_EQ(0, motionArgs.flags);
5477 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5478 ASSERT_EQ(0, motionArgs.buttonState);
5479 ASSERT_EQ(0, motionArgs.edgeFlags);
5480 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5484 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5485 1, 0, 0, 0, 0, 0, 0, 0));
5486 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5487 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5488 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5489
5490 // Move.
5491 x += 50;
5492 y += 75;
5493 processMove(mapper, x, y);
5494 processSync(mapper);
5495
5496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5497 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5498 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5499 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5500 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5501 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5503 ASSERT_EQ(0, motionArgs.flags);
5504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5505 ASSERT_EQ(0, motionArgs.buttonState);
5506 ASSERT_EQ(0, motionArgs.edgeFlags);
5507 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5509 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5511 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5512 1, 0, 0, 0, 0, 0, 0, 0));
5513 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5514 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5515 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5516
5517 // Up.
5518 processUp(mapper);
5519 processSync(mapper);
5520
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5522 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5523 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5524 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5525 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5526 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5527 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5528 ASSERT_EQ(0, motionArgs.flags);
5529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5530 ASSERT_EQ(0, motionArgs.buttonState);
5531 ASSERT_EQ(0, motionArgs.edgeFlags);
5532 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5533 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5536 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5537 1, 0, 0, 0, 0, 0, 0, 0));
5538 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5539 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5540 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5541
5542 // Should not have sent any more keys or motions.
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5545}
5546
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548 addConfigurationProperty("touch.deviceType", "touchScreen");
5549 prepareDisplay(DISPLAY_ORIENTATION_0);
5550 prepareButtons();
5551 prepareAxes(POSITION);
5552 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005553 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554
arthurhungdcef2dc2020-08-11 14:47:50 +08005555 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005556
5557 NotifyMotionArgs motionArgs;
5558
5559 // Down.
5560 int32_t x = 100;
5561 int32_t y = 125;
5562 processDown(mapper, x, y);
5563 processSync(mapper);
5564
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5566 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5567 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5568 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5569 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5570 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5571 ASSERT_EQ(0, motionArgs.flags);
5572 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5573 ASSERT_EQ(0, motionArgs.buttonState);
5574 ASSERT_EQ(0, motionArgs.edgeFlags);
5575 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5576 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5579 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5580 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5581 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5582 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5583
5584 // Move.
5585 x += 50;
5586 y += 75;
5587 processMove(mapper, x, y);
5588 processSync(mapper);
5589
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5592 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5593 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5594 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5596 ASSERT_EQ(0, motionArgs.flags);
5597 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5598 ASSERT_EQ(0, motionArgs.buttonState);
5599 ASSERT_EQ(0, motionArgs.edgeFlags);
5600 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5604 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5605 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5606 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5607 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5608
5609 // Up.
5610 processUp(mapper);
5611 processSync(mapper);
5612
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5615 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5616 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5617 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5618 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5619 ASSERT_EQ(0, motionArgs.flags);
5620 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5621 ASSERT_EQ(0, motionArgs.buttonState);
5622 ASSERT_EQ(0, motionArgs.edgeFlags);
5623 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5624 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5627 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5628 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5629 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5630 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5631
5632 // Should not have sent any more keys or motions.
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5635}
5636
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005637TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638 addConfigurationProperty("touch.deviceType", "touchScreen");
5639 prepareButtons();
5640 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005641 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5642 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005643 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005644
5645 NotifyMotionArgs args;
5646
5647 // Rotation 90.
5648 prepareDisplay(DISPLAY_ORIENTATION_90);
5649 processDown(mapper, toRawX(50), toRawY(75));
5650 processSync(mapper);
5651
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5653 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5654 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5655
5656 processUp(mapper);
5657 processSync(mapper);
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5659}
5660
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005661TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005662 addConfigurationProperty("touch.deviceType", "touchScreen");
5663 prepareButtons();
5664 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005665 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5666 // orientation-aware are affected by display rotation.
5667 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005668 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669
5670 NotifyMotionArgs args;
5671
5672 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005673 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674 prepareDisplay(DISPLAY_ORIENTATION_0);
5675 processDown(mapper, toRawX(50), toRawY(75));
5676 processSync(mapper);
5677
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5679 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5680 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5681
5682 processUp(mapper);
5683 processSync(mapper);
5684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5685
5686 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005687 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005689 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005690 processSync(mapper);
5691
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5693 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5694 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5695
5696 processUp(mapper);
5697 processSync(mapper);
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5699
5700 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005701 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702 prepareDisplay(DISPLAY_ORIENTATION_180);
5703 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5704 processSync(mapper);
5705
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5707 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5708 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5709
5710 processUp(mapper);
5711 processSync(mapper);
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5713
5714 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005715 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005716 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005717 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718 processSync(mapper);
5719
5720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5721 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5722 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5723
5724 processUp(mapper);
5725 processSync(mapper);
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5727}
5728
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005729TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5730 addConfigurationProperty("touch.deviceType", "touchScreen");
5731 prepareButtons();
5732 prepareAxes(POSITION);
5733 addConfigurationProperty("touch.orientationAware", "1");
5734 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5735 clearViewports();
5736 prepareDisplay(DISPLAY_ORIENTATION_0);
5737 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5738 NotifyMotionArgs args;
5739
5740 // Orientation 0.
5741 processDown(mapper, toRawX(50), toRawY(75));
5742 processSync(mapper);
5743
5744 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5745 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5746 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5747
5748 processUp(mapper);
5749 processSync(mapper);
5750 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5751}
5752
5753TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5754 addConfigurationProperty("touch.deviceType", "touchScreen");
5755 prepareButtons();
5756 prepareAxes(POSITION);
5757 addConfigurationProperty("touch.orientationAware", "1");
5758 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5759 clearViewports();
5760 prepareDisplay(DISPLAY_ORIENTATION_0);
5761 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5762 NotifyMotionArgs args;
5763
5764 // Orientation 90.
5765 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5766 processSync(mapper);
5767
5768 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5769 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5770 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5771
5772 processUp(mapper);
5773 processSync(mapper);
5774 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5775}
5776
5777TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5778 addConfigurationProperty("touch.deviceType", "touchScreen");
5779 prepareButtons();
5780 prepareAxes(POSITION);
5781 addConfigurationProperty("touch.orientationAware", "1");
5782 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5783 clearViewports();
5784 prepareDisplay(DISPLAY_ORIENTATION_0);
5785 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5786 NotifyMotionArgs args;
5787
5788 // Orientation 180.
5789 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5790 processSync(mapper);
5791
5792 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5793 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5794 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5795
5796 processUp(mapper);
5797 processSync(mapper);
5798 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5799}
5800
5801TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5802 addConfigurationProperty("touch.deviceType", "touchScreen");
5803 prepareButtons();
5804 prepareAxes(POSITION);
5805 addConfigurationProperty("touch.orientationAware", "1");
5806 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5807 clearViewports();
5808 prepareDisplay(DISPLAY_ORIENTATION_0);
5809 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5810 NotifyMotionArgs args;
5811
5812 // Orientation 270.
5813 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5814 processSync(mapper);
5815
5816 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5817 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5818 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5819
5820 processUp(mapper);
5821 processSync(mapper);
5822 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5823}
5824
5825TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5826 addConfigurationProperty("touch.deviceType", "touchScreen");
5827 prepareButtons();
5828 prepareAxes(POSITION);
5829 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5830 // orientation-aware are affected by display rotation.
5831 addConfigurationProperty("touch.orientationAware", "0");
5832 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5833 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5834
5835 NotifyMotionArgs args;
5836
5837 // Orientation 90, Rotation 0.
5838 clearViewports();
5839 prepareDisplay(DISPLAY_ORIENTATION_0);
5840 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5841 processSync(mapper);
5842
5843 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5844 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5845 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5846
5847 processUp(mapper);
5848 processSync(mapper);
5849 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5850
5851 // Orientation 90, Rotation 90.
5852 clearViewports();
5853 prepareDisplay(DISPLAY_ORIENTATION_90);
5854 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5855 processSync(mapper);
5856
5857 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5858 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5859 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5860
5861 processUp(mapper);
5862 processSync(mapper);
5863 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5864
5865 // Orientation 90, Rotation 180.
5866 clearViewports();
5867 prepareDisplay(DISPLAY_ORIENTATION_180);
5868 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5869 processSync(mapper);
5870
5871 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5872 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5873 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5874
5875 processUp(mapper);
5876 processSync(mapper);
5877 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5878
5879 // Orientation 90, Rotation 270.
5880 clearViewports();
5881 prepareDisplay(DISPLAY_ORIENTATION_270);
5882 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5883 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5884 processSync(mapper);
5885
5886 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5887 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5888 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5889
5890 processUp(mapper);
5891 processSync(mapper);
5892 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5893}
5894
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896 addConfigurationProperty("touch.deviceType", "touchScreen");
5897 prepareDisplay(DISPLAY_ORIENTATION_0);
5898 prepareButtons();
5899 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005900 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901
5902 // These calculations are based on the input device calibration documentation.
5903 int32_t rawX = 100;
5904 int32_t rawY = 200;
5905 int32_t rawPressure = 10;
5906 int32_t rawToolMajor = 12;
5907 int32_t rawDistance = 2;
5908 int32_t rawTiltX = 30;
5909 int32_t rawTiltY = 110;
5910
5911 float x = toDisplayX(rawX);
5912 float y = toDisplayY(rawY);
5913 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5914 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5915 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5916 float distance = float(rawDistance);
5917
5918 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5919 float tiltScale = M_PI / 180;
5920 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5921 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5922 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5923 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5924
5925 processDown(mapper, rawX, rawY);
5926 processPressure(mapper, rawPressure);
5927 processToolMajor(mapper, rawToolMajor);
5928 processDistance(mapper, rawDistance);
5929 processTilt(mapper, rawTiltX, rawTiltY);
5930 processSync(mapper);
5931
5932 NotifyMotionArgs args;
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5935 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5936 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5937}
5938
Jason Gerecke489fda82012-09-07 17:19:40 -07005939TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005940 addConfigurationProperty("touch.deviceType", "touchScreen");
5941 prepareDisplay(DISPLAY_ORIENTATION_0);
5942 prepareLocationCalibration();
5943 prepareButtons();
5944 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005945 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005946
5947 int32_t rawX = 100;
5948 int32_t rawY = 200;
5949
5950 float x = toDisplayX(toCookedX(rawX, rawY));
5951 float y = toDisplayY(toCookedY(rawX, rawY));
5952
5953 processDown(mapper, rawX, rawY);
5954 processSync(mapper);
5955
5956 NotifyMotionArgs args;
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5959 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5960}
5961
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963 addConfigurationProperty("touch.deviceType", "touchScreen");
5964 prepareDisplay(DISPLAY_ORIENTATION_0);
5965 prepareButtons();
5966 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005967 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968
5969 NotifyMotionArgs motionArgs;
5970 NotifyKeyArgs keyArgs;
5971
5972 processDown(mapper, 100, 200);
5973 processSync(mapper);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5975 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5976 ASSERT_EQ(0, motionArgs.buttonState);
5977
5978 // press BTN_LEFT, release BTN_LEFT
5979 processKey(mapper, BTN_LEFT, 1);
5980 processSync(mapper);
5981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5983 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5984
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5987 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5988
Michael Wrightd02c5b62014-02-10 15:10:22 -08005989 processKey(mapper, BTN_LEFT, 0);
5990 processSync(mapper);
5991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005994
5995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005997 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998
5999 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6000 processKey(mapper, BTN_RIGHT, 1);
6001 processKey(mapper, BTN_MIDDLE, 1);
6002 processSync(mapper);
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6005 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6006 motionArgs.buttonState);
6007
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6010 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6011
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6014 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6015 motionArgs.buttonState);
6016
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017 processKey(mapper, BTN_RIGHT, 0);
6018 processSync(mapper);
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006021 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006022
6023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006025 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006026
6027 processKey(mapper, BTN_MIDDLE, 0);
6028 processSync(mapper);
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006031 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006032
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006035 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006036
6037 // press BTN_BACK, release BTN_BACK
6038 processKey(mapper, BTN_BACK, 1);
6039 processSync(mapper);
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6041 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6042 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006043
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006046 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6047
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6050 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006051
6052 processKey(mapper, BTN_BACK, 0);
6053 processSync(mapper);
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006057
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060 ASSERT_EQ(0, motionArgs.buttonState);
6061
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6063 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6064 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6065
6066 // press BTN_SIDE, release BTN_SIDE
6067 processKey(mapper, BTN_SIDE, 1);
6068 processSync(mapper);
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6070 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6071 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006075 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6076
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6078 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6079 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080
6081 processKey(mapper, BTN_SIDE, 0);
6082 processSync(mapper);
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006086
6087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006089 ASSERT_EQ(0, motionArgs.buttonState);
6090
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6092 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6093 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6094
6095 // press BTN_FORWARD, release BTN_FORWARD
6096 processKey(mapper, BTN_FORWARD, 1);
6097 processSync(mapper);
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6099 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6100 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006104 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6105
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6108 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109
6110 processKey(mapper, BTN_FORWARD, 0);
6111 processSync(mapper);
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006115
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006118 ASSERT_EQ(0, motionArgs.buttonState);
6119
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6121 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6122 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6123
6124 // press BTN_EXTRA, release BTN_EXTRA
6125 processKey(mapper, BTN_EXTRA, 1);
6126 processSync(mapper);
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6128 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6129 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006130
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006133 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6134
6135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6136 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6137 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006138
6139 processKey(mapper, BTN_EXTRA, 0);
6140 processSync(mapper);
6141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006143 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006144
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006147 ASSERT_EQ(0, motionArgs.buttonState);
6148
Michael Wrightd02c5b62014-02-10 15:10:22 -08006149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6150 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6151 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6152
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6154
Michael Wrightd02c5b62014-02-10 15:10:22 -08006155 // press BTN_STYLUS, release BTN_STYLUS
6156 processKey(mapper, BTN_STYLUS, 1);
6157 processSync(mapper);
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006160 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6161
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6163 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6164 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165
6166 processKey(mapper, BTN_STYLUS, 0);
6167 processSync(mapper);
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006169 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006170 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006171
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006174 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175
6176 // press BTN_STYLUS2, release BTN_STYLUS2
6177 processKey(mapper, BTN_STYLUS2, 1);
6178 processSync(mapper);
6179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006181 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6182
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6185 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006186
6187 processKey(mapper, BTN_STYLUS2, 0);
6188 processSync(mapper);
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006191 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006192
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006195 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196
6197 // release touch
6198 processUp(mapper);
6199 processSync(mapper);
6200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6201 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6202 ASSERT_EQ(0, motionArgs.buttonState);
6203}
6204
6205TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206 addConfigurationProperty("touch.deviceType", "touchScreen");
6207 prepareDisplay(DISPLAY_ORIENTATION_0);
6208 prepareButtons();
6209 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006210 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006211
6212 NotifyMotionArgs motionArgs;
6213
6214 // default tool type is finger
6215 processDown(mapper, 100, 200);
6216 processSync(mapper);
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6218 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6220
6221 // eraser
6222 processKey(mapper, BTN_TOOL_RUBBER, 1);
6223 processSync(mapper);
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6227
6228 // stylus
6229 processKey(mapper, BTN_TOOL_RUBBER, 0);
6230 processKey(mapper, BTN_TOOL_PEN, 1);
6231 processSync(mapper);
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6235
6236 // brush
6237 processKey(mapper, BTN_TOOL_PEN, 0);
6238 processKey(mapper, BTN_TOOL_BRUSH, 1);
6239 processSync(mapper);
6240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6241 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6243
6244 // pencil
6245 processKey(mapper, BTN_TOOL_BRUSH, 0);
6246 processKey(mapper, BTN_TOOL_PENCIL, 1);
6247 processSync(mapper);
6248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6251
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006252 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253 processKey(mapper, BTN_TOOL_PENCIL, 0);
6254 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6259
6260 // mouse
6261 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6262 processKey(mapper, BTN_TOOL_MOUSE, 1);
6263 processSync(mapper);
6264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6267
6268 // lens
6269 processKey(mapper, BTN_TOOL_MOUSE, 0);
6270 processKey(mapper, BTN_TOOL_LENS, 1);
6271 processSync(mapper);
6272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6274 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6275
6276 // double-tap
6277 processKey(mapper, BTN_TOOL_LENS, 0);
6278 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6279 processSync(mapper);
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6282 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6283
6284 // triple-tap
6285 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6286 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6290 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6291
6292 // quad-tap
6293 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6294 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6299
6300 // finger
6301 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6302 processKey(mapper, BTN_TOOL_FINGER, 1);
6303 processSync(mapper);
6304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6306 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6307
6308 // stylus trumps finger
6309 processKey(mapper, BTN_TOOL_PEN, 1);
6310 processSync(mapper);
6311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6314
6315 // eraser trumps stylus
6316 processKey(mapper, BTN_TOOL_RUBBER, 1);
6317 processSync(mapper);
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6321
6322 // mouse trumps eraser
6323 processKey(mapper, BTN_TOOL_MOUSE, 1);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6328
6329 // back to default tool type
6330 processKey(mapper, BTN_TOOL_MOUSE, 0);
6331 processKey(mapper, BTN_TOOL_RUBBER, 0);
6332 processKey(mapper, BTN_TOOL_PEN, 0);
6333 processKey(mapper, BTN_TOOL_FINGER, 0);
6334 processSync(mapper);
6335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6337 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6338}
6339
6340TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006341 addConfigurationProperty("touch.deviceType", "touchScreen");
6342 prepareDisplay(DISPLAY_ORIENTATION_0);
6343 prepareButtons();
6344 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006345 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006346 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006347
6348 NotifyMotionArgs motionArgs;
6349
6350 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6351 processKey(mapper, BTN_TOOL_FINGER, 1);
6352 processMove(mapper, 100, 200);
6353 processSync(mapper);
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6355 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6357 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6358
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6362 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6363
6364 // move a little
6365 processMove(mapper, 150, 250);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6370 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6371
6372 // down when BTN_TOUCH is pressed, pressure defaults to 1
6373 processKey(mapper, BTN_TOUCH, 1);
6374 processSync(mapper);
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6378 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6379
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6383 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6384
6385 // up when BTN_TOUCH is released, hover restored
6386 processKey(mapper, BTN_TOUCH, 0);
6387 processSync(mapper);
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6389 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6391 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6392
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6394 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6396 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6397
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6401 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6402
6403 // exit hover when pointer goes away
6404 processKey(mapper, BTN_TOOL_FINGER, 0);
6405 processSync(mapper);
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6407 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6409 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6410}
6411
6412TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006413 addConfigurationProperty("touch.deviceType", "touchScreen");
6414 prepareDisplay(DISPLAY_ORIENTATION_0);
6415 prepareButtons();
6416 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006417 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006418
6419 NotifyMotionArgs motionArgs;
6420
6421 // initially hovering because pressure is 0
6422 processDown(mapper, 100, 200);
6423 processPressure(mapper, 0);
6424 processSync(mapper);
6425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6426 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6428 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6429
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6433 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6434
6435 // move a little
6436 processMove(mapper, 150, 250);
6437 processSync(mapper);
6438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6439 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6440 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6441 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6442
6443 // down when pressure is non-zero
6444 processPressure(mapper, RAW_PRESSURE_MAX);
6445 processSync(mapper);
6446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6447 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6449 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6450
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6452 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6454 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6455
6456 // up when pressure becomes 0, hover restored
6457 processPressure(mapper, 0);
6458 processSync(mapper);
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6460 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6462 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6463
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6467 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6468
6469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6470 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6472 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6473
6474 // exit hover when pointer goes away
6475 processUp(mapper);
6476 processSync(mapper);
6477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6478 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6479 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6480 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6481}
6482
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006483TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6484 addConfigurationProperty("touch.deviceType", "touchScreen");
6485 prepareDisplay(DISPLAY_ORIENTATION_0);
6486 prepareButtons();
6487 prepareAxes(POSITION | PRESSURE);
6488 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6489
6490 // Touch down.
6491 processDown(mapper, 100, 200);
6492 processPressure(mapper, 1);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6495 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6496
6497 // Reset the mapper. This should cancel the ongoing gesture.
6498 resetMapper(mapper, ARBITRARY_TIME);
6499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6500 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6501
6502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6503}
6504
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006505TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6506 addConfigurationProperty("touch.deviceType", "touchScreen");
6507 prepareDisplay(DISPLAY_ORIENTATION_0);
6508 prepareButtons();
6509 prepareAxes(POSITION | PRESSURE);
6510 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6511
6512 // Set the initial state for the touch pointer.
6513 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6514 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6515 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6516 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6517
6518 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006519 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6520 // does not generate any events.
6521 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006522
6523 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6524 // the recreated touch state to generate a down event.
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6527 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6528
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6530}
6531
lilinnan687e58f2022-07-19 16:00:50 +08006532TEST_F(SingleTouchInputMapperTest,
6533 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6534 addConfigurationProperty("touch.deviceType", "touchScreen");
6535 prepareDisplay(DISPLAY_ORIENTATION_0);
6536 prepareButtons();
6537 prepareAxes(POSITION);
6538 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6539 NotifyMotionArgs motionArgs;
6540
6541 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006542 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006543 processSync(mapper);
6544
6545 // We should receive a down event
6546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6547 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6548
6549 // Change display id
6550 clearViewports();
6551 prepareSecondaryDisplay(ViewportType::INTERNAL);
6552
6553 // We should receive a cancel event
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6556 // Then receive reset called
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6558}
6559
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006560TEST_F(SingleTouchInputMapperTest,
6561 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6562 addConfigurationProperty("touch.deviceType", "touchScreen");
6563 prepareDisplay(DISPLAY_ORIENTATION_0);
6564 prepareButtons();
6565 prepareAxes(POSITION);
6566 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6568 NotifyMotionArgs motionArgs;
6569
6570 // Start a new gesture.
6571 processDown(mapper, 100, 200);
6572 processSync(mapper);
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6574 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6575
6576 // Make the viewport inactive. This will put the device in disabled mode.
6577 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6578 viewport->isActive = false;
6579 mFakePolicy->updateViewport(*viewport);
6580 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6581
6582 // We should receive a cancel event for the ongoing gesture.
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6584 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6585 // Then we should be notified that the device was reset.
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6587
6588 // No events are generated while the viewport is inactive.
6589 processMove(mapper, 101, 201);
6590 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006591 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006592 processSync(mapper);
6593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6594
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006595 // Start a new gesture while the viewport is still inactive.
6596 processDown(mapper, 300, 400);
6597 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6598 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6599 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6600 processSync(mapper);
6601
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006602 // Make the viewport active again. The device should resume processing events.
6603 viewport->isActive = true;
6604 mFakePolicy->updateViewport(*viewport);
6605 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6606
6607 // The device is reset because it changes back to direct mode, without generating any events.
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6610
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006611 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006612 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6614 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006615
6616 // No more events.
6617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6619}
6620
Prabir Pradhan211ba622022-10-31 21:09:21 +00006621TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6622 addConfigurationProperty("touch.deviceType", "touchScreen");
6623 prepareDisplay(DISPLAY_ORIENTATION_0);
6624 prepareButtons();
6625 prepareAxes(POSITION);
6626 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6628
6629 // Press a stylus button.
6630 processKey(mapper, BTN_STYLUS, 1);
6631 processSync(mapper);
6632
6633 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6634 processDown(mapper, 100, 200);
6635 processSync(mapper);
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6637 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6638 WithCoords(toDisplayX(100), toDisplayY(200)),
6639 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6641 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6642 WithCoords(toDisplayX(100), toDisplayY(200)),
6643 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6644
6645 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6646 // the button has not actually been released, since there will be no pointers through which the
6647 // button state can be reported. The event is generated at the location of the pointer before
6648 // it went up.
6649 processUp(mapper);
6650 processSync(mapper);
6651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6652 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6653 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6655 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6656 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6657}
6658
Prabir Pradhan5632d622021-09-06 07:57:20 -07006659// --- TouchDisplayProjectionTest ---
6660
6661class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6662public:
6663 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6664 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6665 // rotated equivalent of the given un-rotated physical display bounds.
6666 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6667 uint32_t inverseRotationFlags;
6668 auto width = DISPLAY_WIDTH;
6669 auto height = DISPLAY_HEIGHT;
6670 switch (orientation) {
6671 case DISPLAY_ORIENTATION_90:
6672 inverseRotationFlags = ui::Transform::ROT_270;
6673 std::swap(width, height);
6674 break;
6675 case DISPLAY_ORIENTATION_180:
6676 inverseRotationFlags = ui::Transform::ROT_180;
6677 break;
6678 case DISPLAY_ORIENTATION_270:
6679 inverseRotationFlags = ui::Transform::ROT_90;
6680 std::swap(width, height);
6681 break;
6682 case DISPLAY_ORIENTATION_0:
6683 inverseRotationFlags = ui::Transform::ROT_0;
6684 break;
6685 default:
6686 FAIL() << "Invalid orientation: " << orientation;
6687 }
6688
6689 const ui::Transform rotation(inverseRotationFlags, width, height);
6690 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6691
6692 std::optional<DisplayViewport> internalViewport =
6693 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6694 DisplayViewport& v = *internalViewport;
6695 v.displayId = DISPLAY_ID;
6696 v.orientation = orientation;
6697
6698 v.logicalLeft = 0;
6699 v.logicalTop = 0;
6700 v.logicalRight = 100;
6701 v.logicalBottom = 100;
6702
6703 v.physicalLeft = rotatedPhysicalDisplay.left;
6704 v.physicalTop = rotatedPhysicalDisplay.top;
6705 v.physicalRight = rotatedPhysicalDisplay.right;
6706 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6707
6708 v.deviceWidth = width;
6709 v.deviceHeight = height;
6710
6711 v.isActive = true;
6712 v.uniqueId = UNIQUE_ID;
6713 v.type = ViewportType::INTERNAL;
6714 mFakePolicy->updateViewport(v);
6715 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6716 }
6717
6718 void assertReceivedMove(const Point& point) {
6719 NotifyMotionArgs motionArgs;
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6722 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6724 1, 0, 0, 0, 0, 0, 0, 0));
6725 }
6726};
6727
6728TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6729 addConfigurationProperty("touch.deviceType", "touchScreen");
6730 prepareDisplay(DISPLAY_ORIENTATION_0);
6731
6732 prepareButtons();
6733 prepareAxes(POSITION);
6734 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6735
6736 NotifyMotionArgs motionArgs;
6737
6738 // Configure the DisplayViewport such that the logical display maps to a subsection of
6739 // the display panel called the physical display. Here, the physical display is bounded by the
6740 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6741 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6742 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6743 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6744
6745 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6746 DISPLAY_ORIENTATION_270}) {
6747 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6748
6749 // Touches outside the physical display should be ignored, and should not generate any
6750 // events. Ensure touches at the following points that lie outside of the physical display
6751 // area do not generate any events.
6752 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6753 processDown(mapper, toRawX(point.x), toRawY(point.y));
6754 processSync(mapper);
6755 processUp(mapper);
6756 processSync(mapper);
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6758 << "Unexpected event generated for touch outside physical display at point: "
6759 << point.x << ", " << point.y;
6760 }
6761 }
6762}
6763
6764TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6765 addConfigurationProperty("touch.deviceType", "touchScreen");
6766 prepareDisplay(DISPLAY_ORIENTATION_0);
6767
6768 prepareButtons();
6769 prepareAxes(POSITION);
6770 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6771
6772 NotifyMotionArgs motionArgs;
6773
6774 // Configure the DisplayViewport such that the logical display maps to a subsection of
6775 // the display panel called the physical display. Here, the physical display is bounded by the
6776 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6777 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6778
6779 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6780 DISPLAY_ORIENTATION_270}) {
6781 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6782
6783 // Touches that start outside the physical display should be ignored until it enters the
6784 // physical display bounds, at which point it should generate a down event. Start a touch at
6785 // the point (5, 100), which is outside the physical display bounds.
6786 static const Point kOutsidePoint{5, 100};
6787 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6788 processSync(mapper);
6789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6790
6791 // Move the touch into the physical display area. This should generate a pointer down.
6792 processMove(mapper, toRawX(11), toRawY(21));
6793 processSync(mapper);
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6795 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6796 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6797 ASSERT_NO_FATAL_FAILURE(
6798 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6799
6800 // Move the touch inside the physical display area. This should generate a pointer move.
6801 processMove(mapper, toRawX(69), toRawY(159));
6802 processSync(mapper);
6803 assertReceivedMove({69, 159});
6804
6805 // Move outside the physical display area. Since the pointer is already down, this should
6806 // now continue generating events.
6807 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6808 processSync(mapper);
6809 assertReceivedMove(kOutsidePoint);
6810
6811 // Release. This should generate a pointer up.
6812 processUp(mapper);
6813 processSync(mapper);
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6815 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6816 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6817 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6818
6819 // Ensure no more events were generated.
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6822 }
6823}
6824
Michael Wrightd02c5b62014-02-10 15:10:22 -08006825// --- MultiTouchInputMapperTest ---
6826
6827class MultiTouchInputMapperTest : public TouchInputMapperTest {
6828protected:
6829 void prepareAxes(int axes);
6830
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006831 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6832 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6833 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6834 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6835 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6836 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6837 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6838 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6839 void processId(MultiTouchInputMapper& mapper, int32_t id);
6840 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6841 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6842 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00006843 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006844 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00006845 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
6846 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006847};
6848
6849void MultiTouchInputMapperTest::prepareAxes(int axes) {
6850 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006851 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6852 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006853 }
6854 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006855 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6856 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006857 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006858 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6859 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006860 }
6861 }
6862 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006863 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6864 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006865 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006866 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006867 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006868 }
6869 }
6870 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006871 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6872 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006873 }
6874 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006875 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6876 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006877 }
6878 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006879 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6880 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006881 }
6882 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6884 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006885 }
6886 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006887 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6888 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006889 }
6890 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006891 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006892 }
6893}
6894
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006895void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6896 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006899}
6900
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006901void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6902 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006904}
6905
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006906void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6907 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006909}
6910
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006911void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006913}
6914
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006915void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006917}
6918
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006919void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
6920 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006922}
6923
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006924void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006926}
6927
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006928void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006930}
6931
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006932void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006934}
6935
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006936void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006937 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006938}
6939
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006940void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006941 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006942}
6943
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006944void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
6945 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006947}
6948
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00006949void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
6950 int32_t value) {
6951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
6952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
6953}
6954
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006955void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006957}
6958
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00006959void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
6960 nsecs_t readTime) {
6961 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006962}
6963
Michael Wrightd02c5b62014-02-10 15:10:22 -08006964TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006965 addConfigurationProperty("touch.deviceType", "touchScreen");
6966 prepareDisplay(DISPLAY_ORIENTATION_0);
6967 prepareAxes(POSITION);
6968 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006969 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006970
arthurhungdcef2dc2020-08-11 14:47:50 +08006971 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972
6973 NotifyMotionArgs motionArgs;
6974
6975 // Two fingers down at once.
6976 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
6977 processPosition(mapper, x1, y1);
6978 processMTSync(mapper);
6979 processPosition(mapper, x2, y2);
6980 processMTSync(mapper);
6981 processSync(mapper);
6982
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6984 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6985 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6986 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6987 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6989 ASSERT_EQ(0, motionArgs.flags);
6990 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6991 ASSERT_EQ(0, motionArgs.buttonState);
6992 ASSERT_EQ(0, motionArgs.edgeFlags);
6993 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6994 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
6995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6997 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
6998 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6999 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7000 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7001
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7003 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7004 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7005 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7006 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007007 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007008 ASSERT_EQ(0, motionArgs.flags);
7009 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7010 ASSERT_EQ(0, motionArgs.buttonState);
7011 ASSERT_EQ(0, motionArgs.edgeFlags);
7012 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7013 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7014 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7015 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7016 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7018 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7020 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7021 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7022 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7023 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7024
7025 // Move.
7026 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7027 processPosition(mapper, x1, y1);
7028 processMTSync(mapper);
7029 processPosition(mapper, x2, y2);
7030 processMTSync(mapper);
7031 processSync(mapper);
7032
7033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7034 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7035 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7036 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7037 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7039 ASSERT_EQ(0, motionArgs.flags);
7040 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7041 ASSERT_EQ(0, motionArgs.buttonState);
7042 ASSERT_EQ(0, motionArgs.edgeFlags);
7043 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7044 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7046 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7049 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7051 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7052 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7053 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7054 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7055
7056 // First finger up.
7057 x2 += 15; y2 -= 20;
7058 processPosition(mapper, x2, y2);
7059 processMTSync(mapper);
7060 processSync(mapper);
7061
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7064 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7065 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7066 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007067 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 ASSERT_EQ(0, motionArgs.flags);
7069 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7070 ASSERT_EQ(0, motionArgs.buttonState);
7071 ASSERT_EQ(0, motionArgs.edgeFlags);
7072 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7073 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7074 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7075 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7076 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7078 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7080 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7081 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7082 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7083 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7084
7085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7086 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7087 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7088 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7089 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7091 ASSERT_EQ(0, motionArgs.flags);
7092 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7093 ASSERT_EQ(0, motionArgs.buttonState);
7094 ASSERT_EQ(0, motionArgs.edgeFlags);
7095 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7096 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7097 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7098 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7099 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7100 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7101 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7102 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7103
7104 // Move.
7105 x2 += 20; y2 -= 25;
7106 processPosition(mapper, x2, y2);
7107 processMTSync(mapper);
7108 processSync(mapper);
7109
7110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7111 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7112 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7113 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7114 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7116 ASSERT_EQ(0, motionArgs.flags);
7117 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7118 ASSERT_EQ(0, motionArgs.buttonState);
7119 ASSERT_EQ(0, motionArgs.edgeFlags);
7120 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7121 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7123 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7124 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7125 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7126 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7127 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7128
7129 // New finger down.
7130 int32_t x3 = 700, y3 = 300;
7131 processPosition(mapper, x2, y2);
7132 processMTSync(mapper);
7133 processPosition(mapper, x3, y3);
7134 processMTSync(mapper);
7135 processSync(mapper);
7136
7137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7138 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7139 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7140 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7141 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007142 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143 ASSERT_EQ(0, motionArgs.flags);
7144 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7145 ASSERT_EQ(0, motionArgs.buttonState);
7146 ASSERT_EQ(0, motionArgs.edgeFlags);
7147 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7148 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7150 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7151 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7153 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7155 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7156 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7157 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7158 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7159
7160 // Second finger up.
7161 x3 += 30; y3 -= 20;
7162 processPosition(mapper, x3, y3);
7163 processMTSync(mapper);
7164 processSync(mapper);
7165
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7167 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7168 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7169 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7170 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007171 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172 ASSERT_EQ(0, motionArgs.flags);
7173 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7174 ASSERT_EQ(0, motionArgs.buttonState);
7175 ASSERT_EQ(0, motionArgs.edgeFlags);
7176 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7177 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7179 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7182 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7184 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7185 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7186 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7187 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7188
7189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7190 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7191 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7192 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7193 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7195 ASSERT_EQ(0, motionArgs.flags);
7196 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7197 ASSERT_EQ(0, motionArgs.buttonState);
7198 ASSERT_EQ(0, motionArgs.edgeFlags);
7199 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7200 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7203 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7204 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7205 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7207
7208 // Last finger up.
7209 processMTSync(mapper);
7210 processSync(mapper);
7211
7212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7214 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7215 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7216 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7217 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7218 ASSERT_EQ(0, motionArgs.flags);
7219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7220 ASSERT_EQ(0, motionArgs.buttonState);
7221 ASSERT_EQ(0, motionArgs.edgeFlags);
7222 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7223 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7226 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7227 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7228 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7229 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7230
7231 // Should not have sent any more keys or motions.
7232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7234}
7235
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007236TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7237 addConfigurationProperty("touch.deviceType", "touchScreen");
7238 prepareDisplay(DISPLAY_ORIENTATION_0);
7239
7240 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7241 /*fuzz*/ 0, /*resolution*/ 10);
7242 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7243 /*fuzz*/ 0, /*resolution*/ 11);
7244 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7245 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7246 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7247 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7248 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7249 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7250 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7251 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7252
7253 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7254
7255 // X and Y axes
7256 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7257 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7258 // Touch major and minor
7259 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7260 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7261 // Tool major and minor
7262 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7263 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7264}
7265
7266TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7267 addConfigurationProperty("touch.deviceType", "touchScreen");
7268 prepareDisplay(DISPLAY_ORIENTATION_0);
7269
7270 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7271 /*fuzz*/ 0, /*resolution*/ 10);
7272 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7273 /*fuzz*/ 0, /*resolution*/ 11);
7274
7275 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7276
7277 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7278
7279 // Touch major and minor
7280 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7281 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7282 // Tool major and minor
7283 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7284 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7285}
7286
Michael Wrightd02c5b62014-02-10 15:10:22 -08007287TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007288 addConfigurationProperty("touch.deviceType", "touchScreen");
7289 prepareDisplay(DISPLAY_ORIENTATION_0);
7290 prepareAxes(POSITION | ID);
7291 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007292 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007293
arthurhungdcef2dc2020-08-11 14:47:50 +08007294 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007295
7296 NotifyMotionArgs motionArgs;
7297
7298 // Two fingers down at once.
7299 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7300 processPosition(mapper, x1, y1);
7301 processId(mapper, 1);
7302 processMTSync(mapper);
7303 processPosition(mapper, x2, y2);
7304 processId(mapper, 2);
7305 processMTSync(mapper);
7306 processSync(mapper);
7307
7308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7309 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7310 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7311 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7312 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7314 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7315
7316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007317 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007318 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7319 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7320 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7321 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7322 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7324 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7326 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7327
7328 // Move.
7329 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7330 processPosition(mapper, x1, y1);
7331 processId(mapper, 1);
7332 processMTSync(mapper);
7333 processPosition(mapper, x2, y2);
7334 processId(mapper, 2);
7335 processMTSync(mapper);
7336 processSync(mapper);
7337
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7340 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7341 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7342 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7343 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7344 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7346 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7347 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7348 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7349
7350 // First finger up.
7351 x2 += 15; y2 -= 20;
7352 processPosition(mapper, x2, y2);
7353 processId(mapper, 2);
7354 processMTSync(mapper);
7355 processSync(mapper);
7356
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007358 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007359 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7362 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7363 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7365 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7367 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7368
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7371 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7372 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7373 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7375 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7376
7377 // Move.
7378 x2 += 20; y2 -= 25;
7379 processPosition(mapper, x2, y2);
7380 processId(mapper, 2);
7381 processMTSync(mapper);
7382 processSync(mapper);
7383
7384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7386 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7387 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7390 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7391
7392 // New finger down.
7393 int32_t x3 = 700, y3 = 300;
7394 processPosition(mapper, x2, y2);
7395 processId(mapper, 2);
7396 processMTSync(mapper);
7397 processPosition(mapper, x3, y3);
7398 processId(mapper, 3);
7399 processMTSync(mapper);
7400 processSync(mapper);
7401
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007403 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007404 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7405 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7407 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7410 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7412 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7413
7414 // Second finger up.
7415 x3 += 30; y3 -= 20;
7416 processPosition(mapper, x3, y3);
7417 processId(mapper, 3);
7418 processMTSync(mapper);
7419 processSync(mapper);
7420
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007422 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007423 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7424 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7426 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7427 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7429 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7431 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7432
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7435 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7436 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7438 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7439 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7440
7441 // Last finger up.
7442 processMTSync(mapper);
7443 processSync(mapper);
7444
7445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7446 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7447 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7448 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7451 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7452
7453 // Should not have sent any more keys or motions.
7454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7456}
7457
7458TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007459 addConfigurationProperty("touch.deviceType", "touchScreen");
7460 prepareDisplay(DISPLAY_ORIENTATION_0);
7461 prepareAxes(POSITION | ID | SLOT);
7462 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007463 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007464
arthurhungdcef2dc2020-08-11 14:47:50 +08007465 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007466
7467 NotifyMotionArgs motionArgs;
7468
7469 // Two fingers down at once.
7470 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7471 processPosition(mapper, x1, y1);
7472 processId(mapper, 1);
7473 processSlot(mapper, 1);
7474 processPosition(mapper, x2, y2);
7475 processId(mapper, 2);
7476 processSync(mapper);
7477
7478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7479 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7480 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7484 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7485
7486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007487 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007488 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7489 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7490 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7491 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7494 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7496 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7497
7498 // Move.
7499 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7500 processSlot(mapper, 0);
7501 processPosition(mapper, x1, y1);
7502 processSlot(mapper, 1);
7503 processPosition(mapper, x2, y2);
7504 processSync(mapper);
7505
7506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7507 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7508 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7509 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7511 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7512 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7514 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7516 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7517
7518 // First finger up.
7519 x2 += 15; y2 -= 20;
7520 processSlot(mapper, 0);
7521 processId(mapper, -1);
7522 processSlot(mapper, 1);
7523 processPosition(mapper, x2, y2);
7524 processSync(mapper);
7525
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007527 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007528 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7529 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7531 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7534 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7536 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7537
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7539 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7540 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7541 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7544 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7545
7546 // Move.
7547 x2 += 20; y2 -= 25;
7548 processPosition(mapper, x2, y2);
7549 processSync(mapper);
7550
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7553 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7554 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7555 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7557 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7558
7559 // New finger down.
7560 int32_t x3 = 700, y3 = 300;
7561 processPosition(mapper, x2, y2);
7562 processSlot(mapper, 0);
7563 processId(mapper, 3);
7564 processPosition(mapper, x3, y3);
7565 processSync(mapper);
7566
7567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007568 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7572 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7575 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7577 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7578
7579 // Second finger up.
7580 x3 += 30; y3 -= 20;
7581 processSlot(mapper, 1);
7582 processId(mapper, -1);
7583 processSlot(mapper, 0);
7584 processPosition(mapper, x3, y3);
7585 processSync(mapper);
7586
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007588 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7590 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7592 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7595 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7597 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7598
7599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7601 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7602 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7603 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7605 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7606
7607 // Last finger up.
7608 processId(mapper, -1);
7609 processSync(mapper);
7610
7611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7612 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7613 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7614 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7617 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7618
7619 // Should not have sent any more keys or motions.
7620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7622}
7623
7624TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007625 addConfigurationProperty("touch.deviceType", "touchScreen");
7626 prepareDisplay(DISPLAY_ORIENTATION_0);
7627 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007628 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007629
7630 // These calculations are based on the input device calibration documentation.
7631 int32_t rawX = 100;
7632 int32_t rawY = 200;
7633 int32_t rawTouchMajor = 7;
7634 int32_t rawTouchMinor = 6;
7635 int32_t rawToolMajor = 9;
7636 int32_t rawToolMinor = 8;
7637 int32_t rawPressure = 11;
7638 int32_t rawDistance = 0;
7639 int32_t rawOrientation = 3;
7640 int32_t id = 5;
7641
7642 float x = toDisplayX(rawX);
7643 float y = toDisplayY(rawY);
7644 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7645 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7646 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7647 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7648 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7649 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7650 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7651 float distance = float(rawDistance);
7652
7653 processPosition(mapper, rawX, rawY);
7654 processTouchMajor(mapper, rawTouchMajor);
7655 processTouchMinor(mapper, rawTouchMinor);
7656 processToolMajor(mapper, rawToolMajor);
7657 processToolMinor(mapper, rawToolMinor);
7658 processPressure(mapper, rawPressure);
7659 processOrientation(mapper, rawOrientation);
7660 processDistance(mapper, rawDistance);
7661 processId(mapper, id);
7662 processMTSync(mapper);
7663 processSync(mapper);
7664
7665 NotifyMotionArgs args;
7666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7667 ASSERT_EQ(0, args.pointerProperties[0].id);
7668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7669 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7670 orientation, distance));
7671}
7672
7673TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007674 addConfigurationProperty("touch.deviceType", "touchScreen");
7675 prepareDisplay(DISPLAY_ORIENTATION_0);
7676 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7677 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007678 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007679
7680 // These calculations are based on the input device calibration documentation.
7681 int32_t rawX = 100;
7682 int32_t rawY = 200;
7683 int32_t rawTouchMajor = 140;
7684 int32_t rawTouchMinor = 120;
7685 int32_t rawToolMajor = 180;
7686 int32_t rawToolMinor = 160;
7687
7688 float x = toDisplayX(rawX);
7689 float y = toDisplayY(rawY);
7690 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7691 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7692 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7693 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7694 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7695
7696 processPosition(mapper, rawX, rawY);
7697 processTouchMajor(mapper, rawTouchMajor);
7698 processTouchMinor(mapper, rawTouchMinor);
7699 processToolMajor(mapper, rawToolMajor);
7700 processToolMinor(mapper, rawToolMinor);
7701 processMTSync(mapper);
7702 processSync(mapper);
7703
7704 NotifyMotionArgs args;
7705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7707 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7708}
7709
7710TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711 addConfigurationProperty("touch.deviceType", "touchScreen");
7712 prepareDisplay(DISPLAY_ORIENTATION_0);
7713 prepareAxes(POSITION | TOUCH | TOOL);
7714 addConfigurationProperty("touch.size.calibration", "diameter");
7715 addConfigurationProperty("touch.size.scale", "10");
7716 addConfigurationProperty("touch.size.bias", "160");
7717 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007718 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007719
7720 // These calculations are based on the input device calibration documentation.
7721 // Note: We only provide a single common touch/tool value because the device is assumed
7722 // not to emit separate values for each pointer (isSummed = 1).
7723 int32_t rawX = 100;
7724 int32_t rawY = 200;
7725 int32_t rawX2 = 150;
7726 int32_t rawY2 = 250;
7727 int32_t rawTouchMajor = 5;
7728 int32_t rawToolMajor = 8;
7729
7730 float x = toDisplayX(rawX);
7731 float y = toDisplayY(rawY);
7732 float x2 = toDisplayX(rawX2);
7733 float y2 = toDisplayY(rawY2);
7734 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7735 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7736 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7737
7738 processPosition(mapper, rawX, rawY);
7739 processTouchMajor(mapper, rawTouchMajor);
7740 processToolMajor(mapper, rawToolMajor);
7741 processMTSync(mapper);
7742 processPosition(mapper, rawX2, rawY2);
7743 processTouchMajor(mapper, rawTouchMajor);
7744 processToolMajor(mapper, rawToolMajor);
7745 processMTSync(mapper);
7746 processSync(mapper);
7747
7748 NotifyMotionArgs args;
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7750 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7751
7752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007753 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007754 ASSERT_EQ(size_t(2), args.pointerCount);
7755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7756 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7758 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7759}
7760
7761TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007762 addConfigurationProperty("touch.deviceType", "touchScreen");
7763 prepareDisplay(DISPLAY_ORIENTATION_0);
7764 prepareAxes(POSITION | TOUCH | TOOL);
7765 addConfigurationProperty("touch.size.calibration", "area");
7766 addConfigurationProperty("touch.size.scale", "43");
7767 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007768 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007769
7770 // These calculations are based on the input device calibration documentation.
7771 int32_t rawX = 100;
7772 int32_t rawY = 200;
7773 int32_t rawTouchMajor = 5;
7774 int32_t rawToolMajor = 8;
7775
7776 float x = toDisplayX(rawX);
7777 float y = toDisplayY(rawY);
7778 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7779 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7780 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7781
7782 processPosition(mapper, rawX, rawY);
7783 processTouchMajor(mapper, rawTouchMajor);
7784 processToolMajor(mapper, rawToolMajor);
7785 processMTSync(mapper);
7786 processSync(mapper);
7787
7788 NotifyMotionArgs args;
7789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7791 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7792}
7793
7794TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 addConfigurationProperty("touch.deviceType", "touchScreen");
7796 prepareDisplay(DISPLAY_ORIENTATION_0);
7797 prepareAxes(POSITION | PRESSURE);
7798 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7799 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007800 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007801
Michael Wrightaa449c92017-12-13 21:21:43 +00007802 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007803 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007804 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7805 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7806 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7807
Michael Wrightd02c5b62014-02-10 15:10:22 -08007808 // These calculations are based on the input device calibration documentation.
7809 int32_t rawX = 100;
7810 int32_t rawY = 200;
7811 int32_t rawPressure = 60;
7812
7813 float x = toDisplayX(rawX);
7814 float y = toDisplayY(rawY);
7815 float pressure = float(rawPressure) * 0.01f;
7816
7817 processPosition(mapper, rawX, rawY);
7818 processPressure(mapper, rawPressure);
7819 processMTSync(mapper);
7820 processSync(mapper);
7821
7822 NotifyMotionArgs args;
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7825 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7826}
7827
7828TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829 addConfigurationProperty("touch.deviceType", "touchScreen");
7830 prepareDisplay(DISPLAY_ORIENTATION_0);
7831 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007832 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007833
7834 NotifyMotionArgs motionArgs;
7835 NotifyKeyArgs keyArgs;
7836
7837 processId(mapper, 1);
7838 processPosition(mapper, 100, 200);
7839 processSync(mapper);
7840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7841 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7842 ASSERT_EQ(0, motionArgs.buttonState);
7843
7844 // press BTN_LEFT, release BTN_LEFT
7845 processKey(mapper, BTN_LEFT, 1);
7846 processSync(mapper);
7847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7848 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7849 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7850
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7852 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7853 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7854
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855 processKey(mapper, BTN_LEFT, 0);
7856 processSync(mapper);
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007859 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007860
7861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007863 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864
7865 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7866 processKey(mapper, BTN_RIGHT, 1);
7867 processKey(mapper, BTN_MIDDLE, 1);
7868 processSync(mapper);
7869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7871 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7872 motionArgs.buttonState);
7873
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7875 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7876 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7877
7878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7879 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7880 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7881 motionArgs.buttonState);
7882
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 processKey(mapper, BTN_RIGHT, 0);
7884 processSync(mapper);
7885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007886 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007888
7889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007891 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007892
7893 processKey(mapper, BTN_MIDDLE, 0);
7894 processSync(mapper);
7895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007896 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007897 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007898
7899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007901 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902
7903 // press BTN_BACK, release BTN_BACK
7904 processKey(mapper, BTN_BACK, 1);
7905 processSync(mapper);
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7907 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7908 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007909
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007912 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7913
7914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7915 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7916 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007917
7918 processKey(mapper, BTN_BACK, 0);
7919 processSync(mapper);
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007921 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007922 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007923
7924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007926 ASSERT_EQ(0, motionArgs.buttonState);
7927
Michael Wrightd02c5b62014-02-10 15:10:22 -08007928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7929 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7930 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7931
7932 // press BTN_SIDE, release BTN_SIDE
7933 processKey(mapper, BTN_SIDE, 1);
7934 processSync(mapper);
7935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7936 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7937 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007938
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007941 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7942
7943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7945 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946
7947 processKey(mapper, BTN_SIDE, 0);
7948 processSync(mapper);
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007950 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007952
7953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007955 ASSERT_EQ(0, motionArgs.buttonState);
7956
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7958 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7959 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
7960
7961 // press BTN_FORWARD, release BTN_FORWARD
7962 processKey(mapper, BTN_FORWARD, 1);
7963 processSync(mapper);
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7965 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7966 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007967
Michael Wrightd02c5b62014-02-10 15:10:22 -08007968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007970 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7971
7972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7973 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7974 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975
7976 processKey(mapper, BTN_FORWARD, 0);
7977 processSync(mapper);
7978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007980 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007981
7982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007984 ASSERT_EQ(0, motionArgs.buttonState);
7985
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7987 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7988 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7989
7990 // press BTN_EXTRA, release BTN_EXTRA
7991 processKey(mapper, BTN_EXTRA, 1);
7992 processSync(mapper);
7993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7994 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7995 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007996
Michael Wrightd02c5b62014-02-10 15:10:22 -08007997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007999 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8000
8001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8002 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8003 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008004
8005 processKey(mapper, BTN_EXTRA, 0);
8006 processSync(mapper);
8007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008008 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008009 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008010
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008013 ASSERT_EQ(0, motionArgs.buttonState);
8014
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8016 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8017 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8018
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8020
Michael Wrightd02c5b62014-02-10 15:10:22 -08008021 // press BTN_STYLUS, release BTN_STYLUS
8022 processKey(mapper, BTN_STYLUS, 1);
8023 processSync(mapper);
8024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008026 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8027
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8030 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008031
8032 processKey(mapper, BTN_STYLUS, 0);
8033 processSync(mapper);
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008037
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008040 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041
8042 // press BTN_STYLUS2, release BTN_STYLUS2
8043 processKey(mapper, BTN_STYLUS2, 1);
8044 processSync(mapper);
8045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008047 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8048
8049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8050 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8051 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008052
8053 processKey(mapper, BTN_STYLUS2, 0);
8054 processSync(mapper);
8055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008056 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008058
8059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008060 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008061 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008062
8063 // release touch
8064 processId(mapper, -1);
8065 processSync(mapper);
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8067 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8068 ASSERT_EQ(0, motionArgs.buttonState);
8069}
8070
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008071TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8072 addConfigurationProperty("touch.deviceType", "touchScreen");
8073 prepareDisplay(DISPLAY_ORIENTATION_0);
8074 prepareAxes(POSITION | ID | SLOT);
8075 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8076
8077 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8078 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8079
8080 // Touch down.
8081 processId(mapper, 1);
8082 processPosition(mapper, 100, 200);
8083 processSync(mapper);
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8085 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8086
8087 // Press and release button mapped to the primary stylus button.
8088 processKey(mapper, BTN_A, 1);
8089 processSync(mapper);
8090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8091 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8092 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8094 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8095 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8096
8097 processKey(mapper, BTN_A, 0);
8098 processSync(mapper);
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8100 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8102 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8103
8104 // Press and release the HID usage mapped to the secondary stylus button.
8105 processHidUsage(mapper, 0xabcd, 1);
8106 processSync(mapper);
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8108 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8109 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8111 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8112 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8113
8114 processHidUsage(mapper, 0xabcd, 0);
8115 processSync(mapper);
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8117 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8119 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8120
8121 // Release touch.
8122 processId(mapper, -1);
8123 processSync(mapper);
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8125 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8126}
8127
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129 addConfigurationProperty("touch.deviceType", "touchScreen");
8130 prepareDisplay(DISPLAY_ORIENTATION_0);
8131 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008132 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008133
8134 NotifyMotionArgs motionArgs;
8135
8136 // default tool type is finger
8137 processId(mapper, 1);
8138 processPosition(mapper, 100, 200);
8139 processSync(mapper);
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8141 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8143
8144 // eraser
8145 processKey(mapper, BTN_TOOL_RUBBER, 1);
8146 processSync(mapper);
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8150
8151 // stylus
8152 processKey(mapper, BTN_TOOL_RUBBER, 0);
8153 processKey(mapper, BTN_TOOL_PEN, 1);
8154 processSync(mapper);
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8158
8159 // brush
8160 processKey(mapper, BTN_TOOL_PEN, 0);
8161 processKey(mapper, BTN_TOOL_BRUSH, 1);
8162 processSync(mapper);
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8166
8167 // pencil
8168 processKey(mapper, BTN_TOOL_BRUSH, 0);
8169 processKey(mapper, BTN_TOOL_PENCIL, 1);
8170 processSync(mapper);
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8174
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008175 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176 processKey(mapper, BTN_TOOL_PENCIL, 0);
8177 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8178 processSync(mapper);
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8182
8183 // mouse
8184 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8185 processKey(mapper, BTN_TOOL_MOUSE, 1);
8186 processSync(mapper);
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8190
8191 // lens
8192 processKey(mapper, BTN_TOOL_MOUSE, 0);
8193 processKey(mapper, BTN_TOOL_LENS, 1);
8194 processSync(mapper);
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8198
8199 // double-tap
8200 processKey(mapper, BTN_TOOL_LENS, 0);
8201 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8202 processSync(mapper);
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8206
8207 // triple-tap
8208 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8209 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8210 processSync(mapper);
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8214
8215 // quad-tap
8216 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8217 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8218 processSync(mapper);
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8222
8223 // finger
8224 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8225 processKey(mapper, BTN_TOOL_FINGER, 1);
8226 processSync(mapper);
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8230
8231 // stylus trumps finger
8232 processKey(mapper, BTN_TOOL_PEN, 1);
8233 processSync(mapper);
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8236 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8237
8238 // eraser trumps stylus
8239 processKey(mapper, BTN_TOOL_RUBBER, 1);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8243 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8244
8245 // mouse trumps eraser
8246 processKey(mapper, BTN_TOOL_MOUSE, 1);
8247 processSync(mapper);
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8250 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8251
8252 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8253 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8254 processSync(mapper);
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8258
8259 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8260 processToolType(mapper, MT_TOOL_PEN);
8261 processSync(mapper);
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8263 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8264 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8265
8266 // back to default tool type
8267 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8268 processKey(mapper, BTN_TOOL_MOUSE, 0);
8269 processKey(mapper, BTN_TOOL_RUBBER, 0);
8270 processKey(mapper, BTN_TOOL_PEN, 0);
8271 processKey(mapper, BTN_TOOL_FINGER, 0);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8276}
8277
8278TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 addConfigurationProperty("touch.deviceType", "touchScreen");
8280 prepareDisplay(DISPLAY_ORIENTATION_0);
8281 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008282 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008283 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284
8285 NotifyMotionArgs motionArgs;
8286
8287 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8288 processId(mapper, 1);
8289 processPosition(mapper, 100, 200);
8290 processSync(mapper);
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8292 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8294 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8295
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8297 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8299 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8300
8301 // move a little
8302 processPosition(mapper, 150, 250);
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8305 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8307 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8308
8309 // down when BTN_TOUCH is pressed, pressure defaults to 1
8310 processKey(mapper, BTN_TOUCH, 1);
8311 processSync(mapper);
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8313 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8315 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8316
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8318 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8320 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8321
8322 // up when BTN_TOUCH is released, hover restored
8323 processKey(mapper, BTN_TOUCH, 0);
8324 processSync(mapper);
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8328 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8329
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8333 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8334
8335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8336 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8338 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8339
8340 // exit hover when pointer goes away
8341 processId(mapper, -1);
8342 processSync(mapper);
8343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8344 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8346 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8347}
8348
8349TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008350 addConfigurationProperty("touch.deviceType", "touchScreen");
8351 prepareDisplay(DISPLAY_ORIENTATION_0);
8352 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008353 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008354
8355 NotifyMotionArgs motionArgs;
8356
8357 // initially hovering because pressure is 0
8358 processId(mapper, 1);
8359 processPosition(mapper, 100, 200);
8360 processPressure(mapper, 0);
8361 processSync(mapper);
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8365 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8366
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8368 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8370 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8371
8372 // move a little
8373 processPosition(mapper, 150, 250);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8378 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8379
8380 // down when pressure becomes non-zero
8381 processPressure(mapper, RAW_PRESSURE_MAX);
8382 processSync(mapper);
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8386 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8387
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8389 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8391 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8392
8393 // up when pressure becomes 0, hover restored
8394 processPressure(mapper, 0);
8395 processSync(mapper);
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8397 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8399 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8400
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8404 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8405
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8407 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8409 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8410
8411 // exit hover when pointer goes away
8412 processId(mapper, -1);
8413 processSync(mapper);
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8415 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8417 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8418}
8419
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008420/**
8421 * Set the input device port <--> display port associations, and check that the
8422 * events are routed to the display that matches the display port.
8423 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8424 */
8425TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008426 const std::string usb2 = "USB2";
8427 const uint8_t hdmi1 = 0;
8428 const uint8_t hdmi2 = 1;
8429 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008430 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008431
8432 addConfigurationProperty("touch.deviceType", "touchScreen");
8433 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008434 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008435
8436 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8437 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8438
8439 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8440 // for this input device is specified, and the matching viewport is not present,
8441 // the input device should be disabled (at the mapper level).
8442
8443 // Add viewport for display 2 on hdmi2
8444 prepareSecondaryDisplay(type, hdmi2);
8445 // Send a touch event
8446 processPosition(mapper, 100, 100);
8447 processSync(mapper);
8448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8449
8450 // Add viewport for display 1 on hdmi1
8451 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8452 // Send a touch event again
8453 processPosition(mapper, 100, 100);
8454 processSync(mapper);
8455
8456 NotifyMotionArgs args;
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8458 ASSERT_EQ(DISPLAY_ID, args.displayId);
8459}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008460
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008461TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8462 addConfigurationProperty("touch.deviceType", "touchScreen");
8463 prepareAxes(POSITION);
8464 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8465
8466 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8467
8468 prepareDisplay(DISPLAY_ORIENTATION_0);
8469 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8470
8471 // Send a touch event
8472 processPosition(mapper, 100, 100);
8473 processSync(mapper);
8474
8475 NotifyMotionArgs args;
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8477 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8478}
8479
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008480TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008481 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008482 std::shared_ptr<FakePointerController> fakePointerController =
8483 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008484 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008485 fakePointerController->setPosition(100, 200);
8486 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008487 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008488
Garfield Tan888a6a42020-01-09 11:39:16 -08008489 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008490 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008491
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008492 prepareDisplay(DISPLAY_ORIENTATION_0);
8493 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008494 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008495
Harry Cutts16a24cc2022-10-26 15:22:19 +00008496 // Check source is a touchpad that would obtain the PointerController.
8497 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008498
8499 NotifyMotionArgs motionArgs;
8500 processPosition(mapper, 100, 100);
8501 processSync(mapper);
8502
8503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8504 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8505 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8506}
8507
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008508/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008509 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8510 */
8511TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8512 addConfigurationProperty("touch.deviceType", "touchScreen");
8513 prepareAxes(POSITION);
8514 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8515
8516 prepareDisplay(DISPLAY_ORIENTATION_0);
8517 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8518 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8519 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8520 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8521
8522 NotifyMotionArgs args;
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8524 ASSERT_EQ(26, args.readTime);
8525
8526 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8527 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8528 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8529
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8531 ASSERT_EQ(33, args.readTime);
8532}
8533
8534/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008535 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8536 * events should not be delivered to the listener.
8537 */
8538TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8539 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008540 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008541 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8542 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8543 ViewportType::INTERNAL);
8544 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8545 prepareAxes(POSITION);
8546 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8547
8548 NotifyMotionArgs motionArgs;
8549 processPosition(mapper, 100, 100);
8550 processSync(mapper);
8551
8552 mFakeListener->assertNotifyMotionWasNotCalled();
8553}
8554
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008555/**
8556 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8557 * the touch mapper can process the events and the events can be delivered to the listener.
8558 */
8559TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8560 addConfigurationProperty("touch.deviceType", "touchScreen");
8561 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8562 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8563 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
8564 ViewportType::INTERNAL);
8565 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8566 prepareAxes(POSITION);
8567 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8568
8569 NotifyMotionArgs motionArgs;
8570 processPosition(mapper, 100, 100);
8571 processSync(mapper);
8572
8573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8574 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8575}
8576
Garfield Tanc734e4f2021-01-15 20:01:39 -08008577TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8578 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008579 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Garfield Tanc734e4f2021-01-15 20:01:39 -08008580 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
8581 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
8582 ViewportType::INTERNAL);
8583 std::optional<DisplayViewport> optionalDisplayViewport =
8584 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8585 ASSERT_TRUE(optionalDisplayViewport.has_value());
8586 DisplayViewport displayViewport = *optionalDisplayViewport;
8587
8588 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8589 prepareAxes(POSITION);
8590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8591
8592 // Finger down
8593 int32_t x = 100, y = 100;
8594 processPosition(mapper, x, y);
8595 processSync(mapper);
8596
8597 NotifyMotionArgs motionArgs;
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8599 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8600
8601 // Deactivate display viewport
8602 displayViewport.isActive = false;
8603 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8604 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8605
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008606 // The ongoing touch should be canceled immediately
8607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8608 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8609
8610 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008611 x += 10, y += 10;
8612 processPosition(mapper, x, y);
8613 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008615
8616 // Reactivate display viewport
8617 displayViewport.isActive = true;
8618 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8619 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8620
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008621 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008622 x += 10, y += 10;
8623 processPosition(mapper, x, y);
8624 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8626 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008627}
8628
Arthur Hung7c645402019-01-25 17:45:42 +08008629TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8630 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008631 prepareAxes(POSITION | ID | SLOT);
8632 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008633 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008634
8635 // Create the second touch screen device, and enable multi fingers.
8636 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008637 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008638 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008639 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008640 std::shared_ptr<InputDevice> device2 =
8641 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008642 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008644 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8645 0 /*flat*/, 0 /*fuzz*/);
8646 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8647 0 /*flat*/, 0 /*fuzz*/);
8648 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8649 0 /*flat*/, 0 /*fuzz*/);
8650 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8651 0 /*flat*/, 0 /*fuzz*/);
8652 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8653 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8654 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008655
8656 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008657 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008658 std::list<NotifyArgs> unused =
8659 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8660 0 /*changes*/);
8661 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008662
8663 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008664 std::shared_ptr<FakePointerController> fakePointerController =
8665 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008666 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008667
8668 // Setup policy for associated displays and show touches.
8669 const uint8_t hdmi1 = 0;
8670 const uint8_t hdmi2 = 1;
8671 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8672 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8673 mFakePolicy->setShowTouches(true);
8674
8675 // Create displays.
8676 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008677 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008678
8679 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008680 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8681 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8682 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008683
8684 // Two fingers down at default display.
8685 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8686 processPosition(mapper, x1, y1);
8687 processId(mapper, 1);
8688 processSlot(mapper, 1);
8689 processPosition(mapper, x2, y2);
8690 processId(mapper, 2);
8691 processSync(mapper);
8692
8693 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8694 fakePointerController->getSpots().find(DISPLAY_ID);
8695 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8696 ASSERT_EQ(size_t(2), iter->second.size());
8697
8698 // Two fingers down at second display.
8699 processPosition(mapper2, x1, y1);
8700 processId(mapper2, 1);
8701 processSlot(mapper2, 1);
8702 processPosition(mapper2, x2, y2);
8703 processId(mapper2, 2);
8704 processSync(mapper2);
8705
8706 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8707 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8708 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008709
8710 // Disable the show touches configuration and ensure the spots are cleared.
8711 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008712 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8713 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008714
8715 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008716}
8717
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008718TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008719 prepareAxes(POSITION);
8720 addConfigurationProperty("touch.deviceType", "touchScreen");
8721 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008722 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008723
8724 NotifyMotionArgs motionArgs;
8725 // Unrotated video frame
8726 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8727 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008728 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008729 processPosition(mapper, 100, 200);
8730 processSync(mapper);
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8732 ASSERT_EQ(frames, motionArgs.videoFrames);
8733
8734 // Subsequent touch events should not have any videoframes
8735 // This is implemented separately in FakeEventHub,
8736 // but that should match the behaviour of TouchVideoDevice.
8737 processPosition(mapper, 200, 200);
8738 processSync(mapper);
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8740 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8741}
8742
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008743TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008744 prepareAxes(POSITION);
8745 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008746 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008747 // Unrotated video frame
8748 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8749 NotifyMotionArgs motionArgs;
8750
8751 // Test all 4 orientations
8752 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008753 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8754 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8755 clearViewports();
8756 prepareDisplay(orientation);
8757 std::vector<TouchVideoFrame> frames{frame};
8758 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8759 processPosition(mapper, 100, 200);
8760 processSync(mapper);
8761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8762 ASSERT_EQ(frames, motionArgs.videoFrames);
8763 }
8764}
8765
8766TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8767 prepareAxes(POSITION);
8768 addConfigurationProperty("touch.deviceType", "touchScreen");
8769 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8770 // orientation-aware are affected by display rotation.
8771 addConfigurationProperty("touch.orientationAware", "0");
8772 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8773 // Unrotated video frame
8774 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8775 NotifyMotionArgs motionArgs;
8776
8777 // Test all 4 orientations
8778 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008779 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
8780 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8781 clearViewports();
8782 prepareDisplay(orientation);
8783 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008784 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008785 processPosition(mapper, 100, 200);
8786 processSync(mapper);
8787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008788 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8789 // compared to the display. This is so that when the window transform (which contains the
8790 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8791 // window's coordinate space.
8792 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008793 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008794
8795 // Release finger.
8796 processSync(mapper);
8797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008798 }
8799}
8800
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008801TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008802 prepareAxes(POSITION);
8803 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008804 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008805 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8806 // so mix these.
8807 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8808 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8809 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8810 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8811 NotifyMotionArgs motionArgs;
8812
8813 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008814 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008815 processPosition(mapper, 100, 200);
8816 processSync(mapper);
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008818 ASSERT_EQ(frames, motionArgs.videoFrames);
8819}
8820
8821TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8822 prepareAxes(POSITION);
8823 addConfigurationProperty("touch.deviceType", "touchScreen");
8824 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8825 // orientation-aware are affected by display rotation.
8826 addConfigurationProperty("touch.orientationAware", "0");
8827 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8828 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8829 // so mix these.
8830 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8831 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8832 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8833 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8834 NotifyMotionArgs motionArgs;
8835
8836 prepareDisplay(DISPLAY_ORIENTATION_90);
8837 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8838 processPosition(mapper, 100, 200);
8839 processSync(mapper);
8840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8841 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8842 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8843 // compared to the display. This is so that when the window transform (which contains the
8844 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8845 // window's coordinate space.
8846 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
8847 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008848 ASSERT_EQ(frames, motionArgs.videoFrames);
8849}
8850
Arthur Hung9da14732019-09-02 16:16:58 +08008851/**
8852 * If we had defined port associations, but the viewport is not ready, the touch device would be
8853 * expected to be disabled, and it should be enabled after the viewport has found.
8854 */
8855TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008856 constexpr uint8_t hdmi2 = 1;
8857 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008858 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008859
8860 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8861
8862 addConfigurationProperty("touch.deviceType", "touchScreen");
8863 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008864 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008865
8866 ASSERT_EQ(mDevice->isEnabled(), false);
8867
8868 // Add display on hdmi2, the device should be enabled and can receive touch event.
8869 prepareSecondaryDisplay(type, hdmi2);
8870 ASSERT_EQ(mDevice->isEnabled(), true);
8871
8872 // Send a touch event.
8873 processPosition(mapper, 100, 100);
8874 processSync(mapper);
8875
8876 NotifyMotionArgs args;
8877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8878 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8879}
8880
Arthur Hung421eb1c2020-01-16 00:09:42 +08008881TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008882 addConfigurationProperty("touch.deviceType", "touchScreen");
8883 prepareDisplay(DISPLAY_ORIENTATION_0);
8884 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008885 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008886
8887 NotifyMotionArgs motionArgs;
8888
8889 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8890 // finger down
8891 processId(mapper, 1);
8892 processPosition(mapper, x1, y1);
8893 processSync(mapper);
8894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8897
8898 // finger move
8899 processId(mapper, 1);
8900 processPosition(mapper, x2, y2);
8901 processSync(mapper);
8902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8905
8906 // finger up.
8907 processId(mapper, -1);
8908 processSync(mapper);
8909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8910 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8911 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8912
8913 // new finger down
8914 processId(mapper, 1);
8915 processPosition(mapper, x3, y3);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8918 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8919 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8920}
8921
8922/**
arthurhungcc7f9802020-04-30 17:55:40 +08008923 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8924 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008925 */
arthurhungcc7f9802020-04-30 17:55:40 +08008926TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008927 addConfigurationProperty("touch.deviceType", "touchScreen");
8928 prepareDisplay(DISPLAY_ORIENTATION_0);
8929 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008930 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008931
8932 NotifyMotionArgs motionArgs;
8933
8934 // default tool type is finger
8935 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008936 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008937 processPosition(mapper, x1, y1);
8938 processSync(mapper);
8939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8940 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8942
8943 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8944 processToolType(mapper, MT_TOOL_PALM);
8945 processSync(mapper);
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8947 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8948
8949 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008950 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008951 processPosition(mapper, x2, y2);
8952 processSync(mapper);
8953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8954
8955 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008956 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008957 processSync(mapper);
8958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8959
8960 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08008961 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008962 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008963 processPosition(mapper, x3, y3);
8964 processSync(mapper);
8965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8966 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8967 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8968}
8969
arthurhungbf89a482020-04-17 17:37:55 +08008970/**
arthurhungcc7f9802020-04-30 17:55:40 +08008971 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
8972 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08008973 */
arthurhungcc7f9802020-04-30 17:55:40 +08008974TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08008975 addConfigurationProperty("touch.deviceType", "touchScreen");
8976 prepareDisplay(DISPLAY_ORIENTATION_0);
8977 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
8978 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8979
8980 NotifyMotionArgs motionArgs;
8981
8982 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08008983 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
8984 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008985 processPosition(mapper, x1, y1);
8986 processSync(mapper);
8987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8988 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8989 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8990
8991 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08008992 processSlot(mapper, SECOND_SLOT);
8993 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08008994 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08008995 processSync(mapper);
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008997 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08008998 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8999
9000 // If the tool type of the first finger changes to MT_TOOL_PALM,
9001 // we expect to receive ACTION_POINTER_UP with cancel flag.
9002 processSlot(mapper, FIRST_SLOT);
9003 processId(mapper, FIRST_TRACKING_ID);
9004 processToolType(mapper, MT_TOOL_PALM);
9005 processSync(mapper);
9006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009007 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009008 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9009
9010 // The following MOVE events of second finger should be processed.
9011 processSlot(mapper, SECOND_SLOT);
9012 processId(mapper, SECOND_TRACKING_ID);
9013 processPosition(mapper, x2 + 1, y2 + 1);
9014 processSync(mapper);
9015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9017 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9018
9019 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9020 // it. Second finger receive move.
9021 processSlot(mapper, FIRST_SLOT);
9022 processId(mapper, INVALID_TRACKING_ID);
9023 processSync(mapper);
9024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9025 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9026 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9027
9028 // Second finger keeps moving.
9029 processSlot(mapper, SECOND_SLOT);
9030 processId(mapper, SECOND_TRACKING_ID);
9031 processPosition(mapper, x2 + 2, y2 + 2);
9032 processSync(mapper);
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9034 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9035 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9036
9037 // Second finger up.
9038 processId(mapper, INVALID_TRACKING_ID);
9039 processSync(mapper);
9040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9041 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9042 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9043}
9044
9045/**
9046 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9047 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9048 */
9049TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9050 addConfigurationProperty("touch.deviceType", "touchScreen");
9051 prepareDisplay(DISPLAY_ORIENTATION_0);
9052 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9053 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9054
9055 NotifyMotionArgs motionArgs;
9056
9057 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9058 // First finger down.
9059 processId(mapper, FIRST_TRACKING_ID);
9060 processPosition(mapper, x1, y1);
9061 processSync(mapper);
9062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9063 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9064 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9065
9066 // Second finger down.
9067 processSlot(mapper, SECOND_SLOT);
9068 processId(mapper, SECOND_TRACKING_ID);
9069 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009070 processSync(mapper);
9071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009072 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009073 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9074
arthurhungcc7f9802020-04-30 17:55:40 +08009075 // If the tool type of the first finger changes to MT_TOOL_PALM,
9076 // we expect to receive ACTION_POINTER_UP with cancel flag.
9077 processSlot(mapper, FIRST_SLOT);
9078 processId(mapper, FIRST_TRACKING_ID);
9079 processToolType(mapper, MT_TOOL_PALM);
9080 processSync(mapper);
9081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009082 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009083 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9084
9085 // Second finger keeps moving.
9086 processSlot(mapper, SECOND_SLOT);
9087 processId(mapper, SECOND_TRACKING_ID);
9088 processPosition(mapper, x2 + 1, y2 + 1);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9091 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9092
9093 // second finger becomes palm, receive cancel due to only 1 finger is active.
9094 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009095 processToolType(mapper, MT_TOOL_PALM);
9096 processSync(mapper);
9097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9098 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9099
arthurhungcc7f9802020-04-30 17:55:40 +08009100 // third finger down.
9101 processSlot(mapper, THIRD_SLOT);
9102 processId(mapper, THIRD_TRACKING_ID);
9103 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009104 processPosition(mapper, x3, y3);
9105 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9107 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9108 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009109 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9110
9111 // third finger move
9112 processId(mapper, THIRD_TRACKING_ID);
9113 processPosition(mapper, x3 + 1, y3 + 1);
9114 processSync(mapper);
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9117
9118 // first finger up, third finger receive move.
9119 processSlot(mapper, FIRST_SLOT);
9120 processId(mapper, INVALID_TRACKING_ID);
9121 processSync(mapper);
9122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9124 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9125
9126 // second finger up, third finger receive move.
9127 processSlot(mapper, SECOND_SLOT);
9128 processId(mapper, INVALID_TRACKING_ID);
9129 processSync(mapper);
9130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9132 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9133
9134 // third finger up.
9135 processSlot(mapper, THIRD_SLOT);
9136 processId(mapper, INVALID_TRACKING_ID);
9137 processSync(mapper);
9138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9139 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9140 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9141}
9142
9143/**
9144 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9145 * and the active finger could still be allowed to receive the events
9146 */
9147TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9148 addConfigurationProperty("touch.deviceType", "touchScreen");
9149 prepareDisplay(DISPLAY_ORIENTATION_0);
9150 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9151 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9152
9153 NotifyMotionArgs motionArgs;
9154
9155 // default tool type is finger
9156 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9157 processId(mapper, FIRST_TRACKING_ID);
9158 processPosition(mapper, x1, y1);
9159 processSync(mapper);
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9161 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9162 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9163
9164 // Second finger down.
9165 processSlot(mapper, SECOND_SLOT);
9166 processId(mapper, SECOND_TRACKING_ID);
9167 processPosition(mapper, x2, y2);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009170 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9172
9173 // If the tool type of the second finger changes to MT_TOOL_PALM,
9174 // we expect to receive ACTION_POINTER_UP with cancel flag.
9175 processId(mapper, SECOND_TRACKING_ID);
9176 processToolType(mapper, MT_TOOL_PALM);
9177 processSync(mapper);
9178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009179 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009180 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9181
9182 // The following MOVE event should be processed.
9183 processSlot(mapper, FIRST_SLOT);
9184 processId(mapper, FIRST_TRACKING_ID);
9185 processPosition(mapper, x1 + 1, y1 + 1);
9186 processSync(mapper);
9187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9189 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9190
9191 // second finger up.
9192 processSlot(mapper, SECOND_SLOT);
9193 processId(mapper, INVALID_TRACKING_ID);
9194 processSync(mapper);
9195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9197
9198 // first finger keep moving
9199 processSlot(mapper, FIRST_SLOT);
9200 processId(mapper, FIRST_TRACKING_ID);
9201 processPosition(mapper, x1 + 2, y1 + 2);
9202 processSync(mapper);
9203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9205
9206 // first finger up.
9207 processId(mapper, INVALID_TRACKING_ID);
9208 processSync(mapper);
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9210 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9211 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009212}
9213
Arthur Hung9ad18942021-06-19 02:04:46 +00009214/**
9215 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9216 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9217 * cause slot be valid again.
9218 */
9219TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9220 addConfigurationProperty("touch.deviceType", "touchScreen");
9221 prepareDisplay(DISPLAY_ORIENTATION_0);
9222 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9223 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9224
9225 NotifyMotionArgs motionArgs;
9226
9227 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9228 // First finger down.
9229 processId(mapper, FIRST_TRACKING_ID);
9230 processPosition(mapper, x1, y1);
9231 processPressure(mapper, RAW_PRESSURE_MAX);
9232 processSync(mapper);
9233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9234 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9235 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9236
9237 // First finger move.
9238 processId(mapper, FIRST_TRACKING_ID);
9239 processPosition(mapper, x1 + 1, y1 + 1);
9240 processPressure(mapper, RAW_PRESSURE_MAX);
9241 processSync(mapper);
9242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9244 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9245
9246 // Second finger down.
9247 processSlot(mapper, SECOND_SLOT);
9248 processId(mapper, SECOND_TRACKING_ID);
9249 processPosition(mapper, x2, y2);
9250 processPressure(mapper, RAW_PRESSURE_MAX);
9251 processSync(mapper);
9252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009253 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009254 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9255
9256 // second finger up with some unexpected data.
9257 processSlot(mapper, SECOND_SLOT);
9258 processId(mapper, INVALID_TRACKING_ID);
9259 processPosition(mapper, x2, y2);
9260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009262 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009263 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9264
9265 // first finger up with some unexpected data.
9266 processSlot(mapper, FIRST_SLOT);
9267 processId(mapper, INVALID_TRACKING_ID);
9268 processPosition(mapper, x2, y2);
9269 processPressure(mapper, RAW_PRESSURE_MAX);
9270 processSync(mapper);
9271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9272 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9273 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9274}
9275
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009276TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9277 addConfigurationProperty("touch.deviceType", "touchScreen");
9278 prepareDisplay(DISPLAY_ORIENTATION_0);
9279 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9280 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9281
9282 // First finger down.
9283 processId(mapper, FIRST_TRACKING_ID);
9284 processPosition(mapper, 100, 200);
9285 processPressure(mapper, RAW_PRESSURE_MAX);
9286 processSync(mapper);
9287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9288 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9289
9290 // Second finger down.
9291 processSlot(mapper, SECOND_SLOT);
9292 processId(mapper, SECOND_TRACKING_ID);
9293 processPosition(mapper, 300, 400);
9294 processPressure(mapper, RAW_PRESSURE_MAX);
9295 processSync(mapper);
9296 ASSERT_NO_FATAL_FAILURE(
9297 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9298
9299 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009300 // preserved. Resetting should cancel the ongoing gesture.
9301 resetMapper(mapper, ARBITRARY_TIME);
9302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9303 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009304
9305 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9306 // the existing touch state to generate a down event.
9307 processPosition(mapper, 301, 302);
9308 processSync(mapper);
9309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9310 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9312 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9313
9314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9315}
9316
9317TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9318 addConfigurationProperty("touch.deviceType", "touchScreen");
9319 prepareDisplay(DISPLAY_ORIENTATION_0);
9320 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9321 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9322
9323 // First finger touches down and releases.
9324 processId(mapper, FIRST_TRACKING_ID);
9325 processPosition(mapper, 100, 200);
9326 processPressure(mapper, RAW_PRESSURE_MAX);
9327 processSync(mapper);
9328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9329 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9330 processId(mapper, INVALID_TRACKING_ID);
9331 processSync(mapper);
9332 ASSERT_NO_FATAL_FAILURE(
9333 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9334
9335 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9336 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009337 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9339
9340 // Send an empty sync frame. Since there are no pointers, no events are generated.
9341 processSync(mapper);
9342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9343}
9344
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009345// --- MultiTouchInputMapperTest_ExternalDevice ---
9346
9347class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9348protected:
Chris Yea52ade12020-08-27 16:49:20 -07009349 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009350};
9351
9352/**
9353 * Expect fallback to internal viewport if device is external and external viewport is not present.
9354 */
9355TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9356 prepareAxes(POSITION);
9357 addConfigurationProperty("touch.deviceType", "touchScreen");
9358 prepareDisplay(DISPLAY_ORIENTATION_0);
9359 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9360
9361 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9362
9363 NotifyMotionArgs motionArgs;
9364
9365 // Expect the event to be sent to the internal viewport,
9366 // because an external viewport is not present.
9367 processPosition(mapper, 100, 100);
9368 processSync(mapper);
9369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9370 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9371
9372 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009373 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009374 processPosition(mapper, 100, 100);
9375 processSync(mapper);
9376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9377 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9378}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009379
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009380TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9381 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9382 std::shared_ptr<FakePointerController> fakePointerController =
9383 std::make_shared<FakePointerController>();
9384 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9385 fakePointerController->setPosition(0, 0);
9386 fakePointerController->setButtonState(0);
9387
9388 // prepare device and capture
9389 prepareDisplay(DISPLAY_ORIENTATION_0);
9390 prepareAxes(POSITION | ID | SLOT);
9391 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9392 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9393 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009394 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009395 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9396
9397 // captured touchpad should be a touchpad source
9398 NotifyDeviceResetArgs resetArgs;
9399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9400 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9401
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009402 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009403
9404 const InputDeviceInfo::MotionRange* relRangeX =
9405 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9406 ASSERT_NE(relRangeX, nullptr);
9407 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9408 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9409 const InputDeviceInfo::MotionRange* relRangeY =
9410 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9411 ASSERT_NE(relRangeY, nullptr);
9412 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9413 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9414
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009415 // run captured pointer tests - note that this is unscaled, so input listener events should be
9416 // identical to what the hardware sends (accounting for any
9417 // calibration).
9418 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009419 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009420 processId(mapper, 1);
9421 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9422 processKey(mapper, BTN_TOUCH, 1);
9423 processSync(mapper);
9424
9425 // expect coord[0] to contain initial location of touch 0
9426 NotifyMotionArgs args;
9427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9428 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9429 ASSERT_EQ(1U, args.pointerCount);
9430 ASSERT_EQ(0, args.pointerProperties[0].id);
9431 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9432 ASSERT_NO_FATAL_FAILURE(
9433 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9434
9435 // FINGER 1 DOWN
9436 processSlot(mapper, 1);
9437 processId(mapper, 2);
9438 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9439 processSync(mapper);
9440
9441 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009443 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009444 ASSERT_EQ(2U, args.pointerCount);
9445 ASSERT_EQ(0, args.pointerProperties[0].id);
9446 ASSERT_EQ(1, args.pointerProperties[1].id);
9447 ASSERT_NO_FATAL_FAILURE(
9448 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9449 ASSERT_NO_FATAL_FAILURE(
9450 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9451
9452 // FINGER 1 MOVE
9453 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9454 processSync(mapper);
9455
9456 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9457 // from move
9458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9460 ASSERT_NO_FATAL_FAILURE(
9461 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9462 ASSERT_NO_FATAL_FAILURE(
9463 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9464
9465 // FINGER 0 MOVE
9466 processSlot(mapper, 0);
9467 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9468 processSync(mapper);
9469
9470 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9472 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9473 ASSERT_NO_FATAL_FAILURE(
9474 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9475 ASSERT_NO_FATAL_FAILURE(
9476 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9477
9478 // BUTTON DOWN
9479 processKey(mapper, BTN_LEFT, 1);
9480 processSync(mapper);
9481
9482 // touchinputmapper design sends a move before button press
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9487
9488 // BUTTON UP
9489 processKey(mapper, BTN_LEFT, 0);
9490 processSync(mapper);
9491
9492 // touchinputmapper design sends a move after button release
9493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9497
9498 // FINGER 0 UP
9499 processId(mapper, -1);
9500 processSync(mapper);
9501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9502 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9503
9504 // FINGER 1 MOVE
9505 processSlot(mapper, 1);
9506 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9507 processSync(mapper);
9508
9509 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9512 ASSERT_EQ(1U, args.pointerCount);
9513 ASSERT_EQ(1, args.pointerProperties[0].id);
9514 ASSERT_NO_FATAL_FAILURE(
9515 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9516
9517 // FINGER 1 UP
9518 processId(mapper, -1);
9519 processKey(mapper, BTN_TOUCH, 0);
9520 processSync(mapper);
9521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9522 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9523
Harry Cutts16a24cc2022-10-26 15:22:19 +00009524 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009525 mFakePolicy->setPointerCapture(false);
9526 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +00009528 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009529}
9530
9531TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9532 std::shared_ptr<FakePointerController> fakePointerController =
9533 std::make_shared<FakePointerController>();
9534 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9535 fakePointerController->setPosition(0, 0);
9536 fakePointerController->setButtonState(0);
9537
9538 // prepare device and capture
9539 prepareDisplay(DISPLAY_ORIENTATION_0);
9540 prepareAxes(POSITION | ID | SLOT);
9541 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9542 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009543 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009544 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9545 // run uncaptured pointer tests - pushes out generic events
9546 // FINGER 0 DOWN
9547 processId(mapper, 3);
9548 processPosition(mapper, 100, 100);
9549 processKey(mapper, BTN_TOUCH, 1);
9550 processSync(mapper);
9551
9552 // start at (100,100), cursor should be at (0,0) * scale
9553 NotifyMotionArgs args;
9554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9555 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9556 ASSERT_NO_FATAL_FAILURE(
9557 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9558
9559 // FINGER 0 MOVE
9560 processPosition(mapper, 200, 200);
9561 processSync(mapper);
9562
9563 // compute scaling to help with touch position checking
9564 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9565 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9566 float scale =
9567 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9568
9569 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9571 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9573 0, 0, 0, 0, 0, 0, 0));
9574}
9575
9576TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9577 std::shared_ptr<FakePointerController> fakePointerController =
9578 std::make_shared<FakePointerController>();
9579
9580 prepareDisplay(DISPLAY_ORIENTATION_0);
9581 prepareAxes(POSITION | ID | SLOT);
9582 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009583 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009584 mFakePolicy->setPointerCapture(false);
9585 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9586
Harry Cutts16a24cc2022-10-26 15:22:19 +00009587 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
9588 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009589
Harry Cutts16a24cc2022-10-26 15:22:19 +00009590 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009591 mFakePolicy->setPointerCapture(true);
9592 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9593 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9594}
9595
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009596// --- BluetoothMultiTouchInputMapperTest ---
9597
9598class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9599protected:
9600 void SetUp() override {
9601 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9602 }
9603};
9604
9605TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9606 addConfigurationProperty("touch.deviceType", "touchScreen");
9607 prepareDisplay(DISPLAY_ORIENTATION_0);
9608 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9609 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9610
9611 nsecs_t kernelEventTime = ARBITRARY_TIME;
9612 nsecs_t expectedEventTime = ARBITRARY_TIME;
9613 // Touch down.
9614 processId(mapper, FIRST_TRACKING_ID);
9615 processPosition(mapper, 100, 200);
9616 processPressure(mapper, RAW_PRESSURE_MAX);
9617 processSync(mapper, ARBITRARY_TIME);
9618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9619 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9620
9621 // Process several events that come in quick succession, according to their timestamps.
9622 for (int i = 0; i < 3; i++) {
9623 constexpr static nsecs_t delta = ms2ns(1);
9624 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9625 kernelEventTime += delta;
9626 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9627
9628 processPosition(mapper, 101 + i, 201 + i);
9629 processSync(mapper, kernelEventTime);
9630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9631 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9632 WithEventTime(expectedEventTime))));
9633 }
9634
9635 // Release the touch.
9636 processId(mapper, INVALID_TRACKING_ID);
9637 processPressure(mapper, RAW_PRESSURE_MIN);
9638 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9640 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9641 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9642}
9643
9644// --- MultiTouchPointerModeTest ---
9645
HQ Liue6983c72022-04-19 22:14:56 +00009646class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9647protected:
9648 float mPointerMovementScale;
9649 float mPointerXZoomScale;
9650 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9651 addConfigurationProperty("touch.deviceType", "pointer");
9652 std::shared_ptr<FakePointerController> fakePointerController =
9653 std::make_shared<FakePointerController>();
9654 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9655 fakePointerController->setPosition(0, 0);
9656 fakePointerController->setButtonState(0);
9657 prepareDisplay(DISPLAY_ORIENTATION_0);
9658
9659 prepareAxes(POSITION);
9660 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9661 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9662 // needs to be disabled, and the pointer gesture needs to be enabled.
9663 mFakePolicy->setPointerCapture(false);
9664 mFakePolicy->setPointerGestureEnabled(true);
9665 mFakePolicy->setPointerController(fakePointerController);
9666
9667 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9668 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9669 mPointerMovementScale =
9670 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9671 mPointerXZoomScale =
9672 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9673 }
9674
9675 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9676 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9677 /*flat*/ 0,
9678 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9679 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9680 /*flat*/ 0,
9681 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9682 }
9683};
9684
9685/**
9686 * Two fingers down on a pointer mode touch pad. The width
9687 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9688 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9689 * be greater than the both value to be freeform gesture, so that after two
9690 * fingers start to move downwards, the gesture should be swipe.
9691 */
9692TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9693 // The min freeform gesture width is 25units/mm x 30mm = 750
9694 // which is greater than fraction of the diagnal length of the touchpad (349).
9695 // Thus, MaxSwipWidth is 750.
9696 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9697 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9698 NotifyMotionArgs motionArgs;
9699
9700 // Two fingers down at once.
9701 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9702 // Pointer's initial position is used the [0,0] coordinate.
9703 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9704
9705 processId(mapper, FIRST_TRACKING_ID);
9706 processPosition(mapper, x1, y1);
9707 processMTSync(mapper);
9708 processId(mapper, SECOND_TRACKING_ID);
9709 processPosition(mapper, x2, y2);
9710 processMTSync(mapper);
9711 processSync(mapper);
9712
9713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9714 ASSERT_EQ(1U, motionArgs.pointerCount);
9715 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009717 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009718 ASSERT_NO_FATAL_FAILURE(
9719 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9720
9721 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9722 // that there should be 1 pointer.
9723 int32_t movingDistance = 200;
9724 y1 += movingDistance;
9725 y2 += movingDistance;
9726
9727 processId(mapper, FIRST_TRACKING_ID);
9728 processPosition(mapper, x1, y1);
9729 processMTSync(mapper);
9730 processId(mapper, SECOND_TRACKING_ID);
9731 processPosition(mapper, x2, y2);
9732 processMTSync(mapper);
9733 processSync(mapper);
9734
9735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9736 ASSERT_EQ(1U, motionArgs.pointerCount);
9737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009739 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009740 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9741 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9742 0, 0, 0, 0));
9743}
9744
9745/**
9746 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9747 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9748 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9749 * value to be freeform gesture, so that after two fingers start to move downwards,
9750 * the gesture should be swipe.
9751 */
9752TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9753 // The min freeform gesture width is 5units/mm x 30mm = 150
9754 // which is greater than fraction of the diagnal length of the touchpad (349).
9755 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
9756 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
9757 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9758 NotifyMotionArgs motionArgs;
9759
9760 // Two fingers down at once.
9761 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9762 // Pointer's initial position is used the [0,0] coordinate.
9763 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9764
9765 processId(mapper, FIRST_TRACKING_ID);
9766 processPosition(mapper, x1, y1);
9767 processMTSync(mapper);
9768 processId(mapper, SECOND_TRACKING_ID);
9769 processPosition(mapper, x2, y2);
9770 processMTSync(mapper);
9771 processSync(mapper);
9772
9773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9774 ASSERT_EQ(1U, motionArgs.pointerCount);
9775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009777 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009778 ASSERT_NO_FATAL_FAILURE(
9779 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9780
9781 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9782 // and there should be 1 pointer.
9783 int32_t movingDistance = 200;
9784 y1 += movingDistance;
9785 y2 += movingDistance;
9786
9787 processId(mapper, FIRST_TRACKING_ID);
9788 processPosition(mapper, x1, y1);
9789 processMTSync(mapper);
9790 processId(mapper, SECOND_TRACKING_ID);
9791 processPosition(mapper, x2, y2);
9792 processMTSync(mapper);
9793 processSync(mapper);
9794
9795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9796 ASSERT_EQ(1U, motionArgs.pointerCount);
9797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9798 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009799 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009800 // New coordinate is the scaled relative coordinate from the initial coordinate.
9801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9802 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9803 0, 0, 0, 0));
9804}
9805
9806/**
9807 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9808 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9809 * freeform gestures after two fingers start to move downwards.
9810 */
9811TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
9812 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9813 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9814
9815 NotifyMotionArgs motionArgs;
9816
9817 // Two fingers down at once. Wider than the max swipe width.
9818 // The gesture is expected to be PRESS, then transformed to FREEFORM
9819 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9820
9821 processId(mapper, FIRST_TRACKING_ID);
9822 processPosition(mapper, x1, y1);
9823 processMTSync(mapper);
9824 processId(mapper, SECOND_TRACKING_ID);
9825 processPosition(mapper, x2, y2);
9826 processMTSync(mapper);
9827 processSync(mapper);
9828
9829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9830 ASSERT_EQ(1U, motionArgs.pointerCount);
9831 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009833 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009834 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9835 ASSERT_NO_FATAL_FAILURE(
9836 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9837
9838 int32_t movingDistance = 200;
9839
9840 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9841 // then two down events for two pointers.
9842 y1 += movingDistance;
9843 y2 += movingDistance;
9844
9845 processId(mapper, FIRST_TRACKING_ID);
9846 processPosition(mapper, x1, y1);
9847 processMTSync(mapper);
9848 processId(mapper, SECOND_TRACKING_ID);
9849 processPosition(mapper, x2, y2);
9850 processMTSync(mapper);
9851 processSync(mapper);
9852
9853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9854 // The previous PRESS gesture is cancelled, because it is transformed to freeform
9855 ASSERT_EQ(1U, motionArgs.pointerCount);
9856 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9859 ASSERT_EQ(1U, motionArgs.pointerCount);
9860 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009863 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009864 ASSERT_EQ(2U, motionArgs.pointerCount);
9865 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
9866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009867 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009868 // Two pointers' scaled relative coordinates from their initial centroid.
9869 // Initial y coordinates are 0 as y1 and y2 have the same value.
9870 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9871 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9872 // When pointers move, the new coordinates equal to the initial coordinates plus
9873 // scaled moving distance.
9874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9875 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9876 0, 0, 0, 0));
9877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9878 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9879 0, 0, 0, 0));
9880
9881 // Move two fingers down again, expect one MOVE motion event.
9882 y1 += movingDistance;
9883 y2 += movingDistance;
9884
9885 processId(mapper, FIRST_TRACKING_ID);
9886 processPosition(mapper, x1, y1);
9887 processMTSync(mapper);
9888 processId(mapper, SECOND_TRACKING_ID);
9889 processPosition(mapper, x2, y2);
9890 processMTSync(mapper);
9891 processSync(mapper);
9892
9893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9894 ASSERT_EQ(2U, motionArgs.pointerCount);
9895 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9896 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009897 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9899 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9900 0, 0, 0, 0, 0));
9901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9902 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9903 0, 0, 0, 0, 0));
9904}
9905
Harry Cutts39b7ca22022-10-05 15:55:48 +00009906TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
9907 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9908 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9909 NotifyMotionArgs motionArgs;
9910
9911 // Place two fingers down.
9912 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9913
9914 processId(mapper, FIRST_TRACKING_ID);
9915 processPosition(mapper, x1, y1);
9916 processMTSync(mapper);
9917 processId(mapper, SECOND_TRACKING_ID);
9918 processPosition(mapper, x2, y2);
9919 processMTSync(mapper);
9920 processSync(mapper);
9921
9922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9923 ASSERT_EQ(1U, motionArgs.pointerCount);
9924 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9925 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
9926 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
9927 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
9928
9929 // Move the two fingers down and to the left.
9930 int32_t movingDistance = 200;
9931 x1 -= movingDistance;
9932 y1 += movingDistance;
9933 x2 -= movingDistance;
9934 y2 += movingDistance;
9935
9936 processId(mapper, FIRST_TRACKING_ID);
9937 processPosition(mapper, x1, y1);
9938 processMTSync(mapper);
9939 processId(mapper, SECOND_TRACKING_ID);
9940 processPosition(mapper, x2, y2);
9941 processMTSync(mapper);
9942 processSync(mapper);
9943
9944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9945 ASSERT_EQ(1U, motionArgs.pointerCount);
9946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9947 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
9948 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
9949 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
9950}
9951
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00009952TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
9953 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9954 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9955 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
9957
9958 // Start a stylus gesture.
9959 processKey(mapper, BTN_TOOL_PEN, 1);
9960 processId(mapper, FIRST_TRACKING_ID);
9961 processPosition(mapper, 100, 200);
9962 processSync(mapper);
9963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9964 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9965 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
9966 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9967 // TODO(b/257078296): Pointer mode generates extra event.
9968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9969 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9970 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
9971 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9973
9974 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
9975 // gesture should be disabled.
9976 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
9977 viewport->isActive = false;
9978 mFakePolicy->updateViewport(*viewport);
9979 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9981 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
9982 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
9983 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9984 // TODO(b/257078296): Pointer mode generates extra event.
9985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9986 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
9987 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
9988 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9990}
9991
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009992// --- JoystickInputMapperTest ---
9993
9994class JoystickInputMapperTest : public InputMapperTest {
9995protected:
9996 static const int32_t RAW_X_MIN;
9997 static const int32_t RAW_X_MAX;
9998 static const int32_t RAW_Y_MIN;
9999 static const int32_t RAW_Y_MAX;
10000
10001 void SetUp() override {
10002 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10003 }
10004 void prepareAxes() {
10005 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10006 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10007 }
10008
10009 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10010 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10011 }
10012
10013 void processSync(JoystickInputMapper& mapper) {
10014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10015 }
10016
10017 void prepareVirtualDisplay(int32_t orientation) {
10018 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10019 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10020 NO_PORT, ViewportType::VIRTUAL);
10021 }
10022};
10023
10024const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10025const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10026const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10027const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10028
10029TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10030 prepareAxes();
10031 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10032
10033 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10034
10035 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10036
10037 // Send an axis event
10038 processAxis(mapper, ABS_X, 100);
10039 processSync(mapper);
10040
10041 NotifyMotionArgs args;
10042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10043 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10044
10045 // Send another axis event
10046 processAxis(mapper, ABS_Y, 100);
10047 processSync(mapper);
10048
10049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10050 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10051}
10052
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010053// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010054
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010055class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010056protected:
10057 static const char* DEVICE_NAME;
10058 static const char* DEVICE_LOCATION;
10059 static const int32_t DEVICE_ID;
10060 static const int32_t DEVICE_GENERATION;
10061 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010062 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010063 static const int32_t EVENTHUB_ID;
10064
10065 std::shared_ptr<FakeEventHub> mFakeEventHub;
10066 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010067 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010068 std::unique_ptr<InstrumentedInputReader> mReader;
10069 std::shared_ptr<InputDevice> mDevice;
10070
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010071 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010072 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010073 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010074 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010075 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010076 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010077 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10078 }
10079
10080 void SetUp() override { SetUp(DEVICE_CLASSES); }
10081
10082 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010083 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010084 mFakePolicy.clear();
10085 }
10086
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010087 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010088 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10089 mReader->requestRefreshConfiguration(changes);
10090 mReader->loopOnce();
10091 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010092 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010093 }
10094
10095 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10096 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010097 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010098 InputDeviceIdentifier identifier;
10099 identifier.name = name;
10100 identifier.location = location;
10101 std::shared_ptr<InputDevice> device =
10102 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10103 identifier);
10104 mReader->pushNextDevice(device);
10105 mFakeEventHub->addDevice(eventHubId, name, classes);
10106 mReader->loopOnce();
10107 return device;
10108 }
10109
10110 template <class T, typename... Args>
10111 T& addControllerAndConfigure(Args... args) {
10112 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10113
10114 return controller;
10115 }
10116};
10117
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010118const char* PeripheralControllerTest::DEVICE_NAME = "device";
10119const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10120const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10121const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10122const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010123const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10124 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010125const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010126
10127// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010128class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010129protected:
10130 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010131 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010132 }
10133};
10134
10135TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010136 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010137
Harry Cuttsa5b71292022-11-28 12:56:17 +000010138 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10139 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10140 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010141}
10142
10143TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010144 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010145
Harry Cuttsa5b71292022-11-28 12:56:17 +000010146 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10147 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10148 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010149}
10150
10151// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010152class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010153protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010154 void SetUp() override {
10155 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10156 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010157};
10158
Chris Ye85758332021-05-16 23:05:17 -070010159TEST_F(LightControllerTest, MonoLight) {
10160 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010161 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010162 .maxBrightness = 255,
10163 .flags = InputLightClass::BRIGHTNESS,
10164 .path = ""};
10165 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010166
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010167 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010168 InputDeviceInfo info;
10169 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010170 std::vector<InputDeviceLightInfo> lights = info.getLights();
10171 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010172 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10173 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10174
10175 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10176 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10177}
10178
10179TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10180 RawLightInfo infoMono = {.id = 1,
10181 .name = "mono_keyboard_backlight",
10182 .maxBrightness = 255,
10183 .flags = InputLightClass::BRIGHTNESS |
10184 InputLightClass::KEYBOARD_BACKLIGHT,
10185 .path = ""};
10186 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10187
10188 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10189 InputDeviceInfo info;
10190 controller.populateDeviceInfo(&info);
10191 std::vector<InputDeviceLightInfo> lights = info.getLights();
10192 ASSERT_EQ(1U, lights.size());
10193 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10194 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010195
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010196 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10197 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010198}
10199
10200TEST_F(LightControllerTest, RGBLight) {
10201 RawLightInfo infoRed = {.id = 1,
10202 .name = "red",
10203 .maxBrightness = 255,
10204 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10205 .path = ""};
10206 RawLightInfo infoGreen = {.id = 2,
10207 .name = "green",
10208 .maxBrightness = 255,
10209 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10210 .path = ""};
10211 RawLightInfo infoBlue = {.id = 3,
10212 .name = "blue",
10213 .maxBrightness = 255,
10214 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10215 .path = ""};
10216 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10217 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10218 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10219
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010220 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010221 InputDeviceInfo info;
10222 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010223 std::vector<InputDeviceLightInfo> lights = info.getLights();
10224 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010225 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10226 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10227 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10228
10229 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10230 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10231}
10232
10233TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10234 RawLightInfo infoRed = {.id = 1,
10235 .name = "red_keyboard_backlight",
10236 .maxBrightness = 255,
10237 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10238 InputLightClass::KEYBOARD_BACKLIGHT,
10239 .path = ""};
10240 RawLightInfo infoGreen = {.id = 2,
10241 .name = "green_keyboard_backlight",
10242 .maxBrightness = 255,
10243 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10244 InputLightClass::KEYBOARD_BACKLIGHT,
10245 .path = ""};
10246 RawLightInfo infoBlue = {.id = 3,
10247 .name = "blue_keyboard_backlight",
10248 .maxBrightness = 255,
10249 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10250 InputLightClass::KEYBOARD_BACKLIGHT,
10251 .path = ""};
10252 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10253 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10254 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10255
10256 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10257 InputDeviceInfo info;
10258 controller.populateDeviceInfo(&info);
10259 std::vector<InputDeviceLightInfo> lights = info.getLights();
10260 ASSERT_EQ(1U, lights.size());
10261 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10262 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10263 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10264
10265 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10266 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10267}
10268
10269TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10270 RawLightInfo infoRed = {.id = 1,
10271 .name = "red",
10272 .maxBrightness = 255,
10273 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10274 .path = ""};
10275 RawLightInfo infoGreen = {.id = 2,
10276 .name = "green",
10277 .maxBrightness = 255,
10278 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10279 .path = ""};
10280 RawLightInfo infoBlue = {.id = 3,
10281 .name = "blue",
10282 .maxBrightness = 255,
10283 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10284 .path = ""};
10285 RawLightInfo infoGlobal = {.id = 3,
10286 .name = "global_keyboard_backlight",
10287 .maxBrightness = 255,
10288 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10289 InputLightClass::KEYBOARD_BACKLIGHT,
10290 .path = ""};
10291 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10292 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10293 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10294 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10295
10296 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10297 InputDeviceInfo info;
10298 controller.populateDeviceInfo(&info);
10299 std::vector<InputDeviceLightInfo> lights = info.getLights();
10300 ASSERT_EQ(1U, lights.size());
10301 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10302 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10303 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010304
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010305 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10306 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010307}
10308
10309TEST_F(LightControllerTest, MultiColorRGBLight) {
10310 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010311 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010312 .maxBrightness = 255,
10313 .flags = InputLightClass::BRIGHTNESS |
10314 InputLightClass::MULTI_INTENSITY |
10315 InputLightClass::MULTI_INDEX,
10316 .path = ""};
10317
10318 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10319
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010320 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010321 InputDeviceInfo info;
10322 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010323 std::vector<InputDeviceLightInfo> lights = info.getLights();
10324 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010325 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10326 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10327 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10328
10329 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10330 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10331}
10332
10333TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10334 RawLightInfo infoColor = {.id = 1,
10335 .name = "multi_color_keyboard_backlight",
10336 .maxBrightness = 255,
10337 .flags = InputLightClass::BRIGHTNESS |
10338 InputLightClass::MULTI_INTENSITY |
10339 InputLightClass::MULTI_INDEX |
10340 InputLightClass::KEYBOARD_BACKLIGHT,
10341 .path = ""};
10342
10343 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10344
10345 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10346 InputDeviceInfo info;
10347 controller.populateDeviceInfo(&info);
10348 std::vector<InputDeviceLightInfo> lights = info.getLights();
10349 ASSERT_EQ(1U, lights.size());
10350 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10351 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10352 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010353
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010354 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10355 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010356}
10357
10358TEST_F(LightControllerTest, PlayerIdLight) {
10359 RawLightInfo info1 = {.id = 1,
10360 .name = "player1",
10361 .maxBrightness = 255,
10362 .flags = InputLightClass::BRIGHTNESS,
10363 .path = ""};
10364 RawLightInfo info2 = {.id = 2,
10365 .name = "player2",
10366 .maxBrightness = 255,
10367 .flags = InputLightClass::BRIGHTNESS,
10368 .path = ""};
10369 RawLightInfo info3 = {.id = 3,
10370 .name = "player3",
10371 .maxBrightness = 255,
10372 .flags = InputLightClass::BRIGHTNESS,
10373 .path = ""};
10374 RawLightInfo info4 = {.id = 4,
10375 .name = "player4",
10376 .maxBrightness = 255,
10377 .flags = InputLightClass::BRIGHTNESS,
10378 .path = ""};
10379 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10380 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10381 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10382 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10383
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010384 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010385 InputDeviceInfo info;
10386 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010387 std::vector<InputDeviceLightInfo> lights = info.getLights();
10388 ASSERT_EQ(1U, lights.size());
10389 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010390 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10391 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010392
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010393 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10394 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10395 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010396}
10397
Michael Wrightd02c5b62014-02-10 15:10:22 -080010398} // namespace android