blob: c72d01fad8fab94dea3f7eaff8b4beb8ee9729f3 [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 Cutts6b5fbc52022-11-28 16:37:43 +000044#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000045#include "FakePointerController.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000046#include "TestConstants.h"
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000047#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000048#include "input/DisplayViewport.h"
49#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010050
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000051using android::hardware::input::InputDeviceCountryCode;
52
Michael Wrightd02c5b62014-02-10 15:10:22 -080053namespace android {
54
Dominik Laskowski2f01d772022-03-23 16:01:29 -070055using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000056using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070057using std::chrono_literals::operator""ms;
58
Michael Wrightd02c5b62014-02-10 15:10:22 -080059// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080060static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000061static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080062static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t DISPLAY_WIDTH = 480;
65static constexpr int32_t DISPLAY_HEIGHT = 800;
66static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
67static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
68static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070069static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070070static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080071
arthurhungcc7f9802020-04-30 17:55:40 +080072static constexpr int32_t FIRST_SLOT = 0;
73static constexpr int32_t SECOND_SLOT = 1;
74static constexpr int32_t THIRD_SLOT = 2;
75static constexpr int32_t INVALID_TRACKING_ID = -1;
76static constexpr int32_t FIRST_TRACKING_ID = 0;
77static constexpr int32_t SECOND_TRACKING_ID = 1;
78static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080079static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
80static constexpr int32_t LIGHT_COLOR = 0x7F448866;
81static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080082
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080083static constexpr int32_t ACTION_POINTER_0_DOWN =
84 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
85static constexpr int32_t ACTION_POINTER_0_UP =
86 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
87static constexpr int32_t ACTION_POINTER_1_DOWN =
88 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_1_UP =
90 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91
Michael Wrightd02c5b62014-02-10 15:10:22 -080092// Error tolerance for floating point assertions.
93static const float EPSILON = 0.001f;
94
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000095// Minimum timestamp separation between subsequent input events from a Bluetooth device.
96static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
97// Maximum smoothing time delta so that we don't generate events too far into the future.
98constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
99
Michael Wrightd02c5b62014-02-10 15:10:22 -0800100template<typename T>
101static inline T min(T a, T b) {
102 return a < b ? a : b;
103}
104
105static inline float avg(float x, float y) {
106 return (x + y) / 2;
107}
108
Chris Ye3fdbfef2021-01-06 18:45:18 -0800109// Mapping for light color name and the light color
110const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
111 {"green", LightColor::GREEN},
112 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800113
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700114static int32_t getInverseRotation(int32_t orientation) {
115 switch (orientation) {
116 case DISPLAY_ORIENTATION_90:
117 return DISPLAY_ORIENTATION_270;
118 case DISPLAY_ORIENTATION_270:
119 return DISPLAY_ORIENTATION_90;
120 default:
121 return orientation;
122 }
123}
124
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800125static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
126 InputDeviceInfo info;
127 mapper.populateDeviceInfo(&info);
128
129 const InputDeviceInfo::MotionRange* motionRange =
130 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
131 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
132}
133
134static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
135 InputDeviceInfo info;
136 mapper.populateDeviceInfo(&info);
137
138 const InputDeviceInfo::MotionRange* motionRange =
139 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
140 ASSERT_EQ(nullptr, motionRange);
141}
142
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700143[[maybe_unused]] static void dumpReader(InputReader& reader) {
144 std::string dump;
145 reader.dump(dump);
146 std::istringstream iss(dump);
147 for (std::string line; std::getline(iss, line);) {
148 ALOGE("%s", line.c_str());
149 std::this_thread::sleep_for(std::chrono::milliseconds(1));
150 }
151}
152
Michael Wrightd02c5b62014-02-10 15:10:22 -0800153// --- FakeInputMapper ---
154
155class FakeInputMapper : public InputMapper {
156 uint32_t mSources;
157 int32_t mKeyboardType;
158 int32_t mMetaState;
159 KeyedVector<int32_t, int32_t> mKeyCodeStates;
160 KeyedVector<int32_t, int32_t> mScanCodeStates;
161 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100162 // fake mapping which would normally come from keyCharacterMap
163 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800164 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800165
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700166 std::mutex mLock;
167 std::condition_variable mStateChangedCondition;
168 bool mConfigureWasCalled GUARDED_BY(mLock);
169 bool mResetWasCalled GUARDED_BY(mLock);
170 bool mProcessWasCalled GUARDED_BY(mLock);
171 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172
Arthur Hungc23540e2018-11-29 20:42:11 +0800173 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800175 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
176 : InputMapper(deviceContext),
177 mSources(sources),
178 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 mConfigureWasCalled(false),
181 mResetWasCalled(false),
182 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
Chris Yea52ade12020-08-27 16:49:20 -0700184 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185
186 void setKeyboardType(int32_t keyboardType) {
187 mKeyboardType = keyboardType;
188 }
189
190 void setMetaState(int32_t metaState) {
191 mMetaState = metaState;
192 }
193
194 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700195 std::unique_lock<std::mutex> lock(mLock);
196 base::ScopedLockAssertion assumeLocked(mLock);
197 const bool configureCalled =
198 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
199 return mConfigureWasCalled;
200 });
201 if (!configureCalled) {
202 FAIL() << "Expected configure() to have been called.";
203 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800204 mConfigureWasCalled = false;
205 }
206
207 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool resetCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mResetWasCalled;
213 });
214 if (!resetCalled) {
215 FAIL() << "Expected reset() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mResetWasCalled = false;
218 }
219
Yi Kong9b14ac62018-07-17 13:48:38 -0700220 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool processCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mProcessWasCalled;
226 });
227 if (!processCalled) {
228 FAIL() << "Expected process() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 if (outLastEvent) {
231 *outLastEvent = mLastEvent;
232 }
233 mProcessWasCalled = false;
234 }
235
236 void setKeyCodeState(int32_t keyCode, int32_t state) {
237 mKeyCodeStates.replaceValueFor(keyCode, state);
238 }
239
240 void setScanCodeState(int32_t scanCode, int32_t state) {
241 mScanCodeStates.replaceValueFor(scanCode, state);
242 }
243
244 void setSwitchState(int32_t switchCode, int32_t state) {
245 mSwitchStates.replaceValueFor(switchCode, state);
246 }
247
248 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800249 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 }
251
Philip Junker4af3b3d2021-12-14 10:36:55 +0100252 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
253 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
254 }
255
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100257 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258
Chris Yea52ade12020-08-27 16:49:20 -0700259 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 InputMapper::populateDeviceInfo(deviceInfo);
261
262 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
263 deviceInfo->setKeyboardType(mKeyboardType);
264 }
265 }
266
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700267 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
268 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700269 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800270 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800271
272 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800273 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800274 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
275 mViewport = config->getDisplayViewportByPort(*displayPort);
276 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700277
278 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700279 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280 }
281
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700282 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700283 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700285 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700286 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700289 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700290 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 mLastEvent = *rawEvent;
292 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700293 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700294 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 }
296
Chris Yea52ade12020-08-27 16:49:20 -0700297 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800298 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
299 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
300 }
301
Philip Junker4af3b3d2021-12-14 10:36:55 +0100302 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
303 auto it = mKeyCodeMapping.find(locationKeyCode);
304 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
305 }
306
Chris Yea52ade12020-08-27 16:49:20 -0700307 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
309 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
310 }
311
Chris Yea52ade12020-08-27 16:49:20 -0700312 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 ssize_t index = mSwitchStates.indexOfKey(switchCode);
314 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
315 }
316
Chris Yea52ade12020-08-27 16:49:20 -0700317 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700318 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700319 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700320 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
322 if (keyCodes[i] == mSupportedKeyCodes[j]) {
323 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800324 }
325 }
326 }
Chris Yea52ade12020-08-27 16:49:20 -0700327 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 return result;
329 }
330
331 virtual int32_t getMetaState() {
332 return mMetaState;
333 }
334
335 virtual void fadePointer() {
336 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800337
338 virtual std::optional<int32_t> getAssociatedDisplay() {
339 if (mViewport) {
340 return std::make_optional(mViewport->displayId);
341 }
342 return std::nullopt;
343 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344};
345
346
347// --- InstrumentedInputReader ---
348
349class InstrumentedInputReader : public InputReader {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800350 std::queue<std::shared_ptr<InputDevice>> mNextDevices;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351
352public:
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700353 InstrumentedInputReader(std::shared_ptr<EventHubInterface> eventHub,
354 const sp<InputReaderPolicyInterface>& policy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700355 InputListenerInterface& listener)
arthurhungdcef2dc2020-08-11 14:47:50 +0800356 : InputReader(eventHub, policy, listener), mFakeContext(this) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800357
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000358 virtual ~InstrumentedInputReader() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800360 void pushNextDevice(std::shared_ptr<InputDevice> device) { mNextDevices.push(device); }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800361
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800362 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Nathaniel R. Lewis0cab12d2019-11-05 02:17:02 +0000363 const std::string& location = "") {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800364 InputDeviceIdentifier identifier;
365 identifier.name = name;
Arthur Hungc23540e2018-11-29 20:42:11 +0800366 identifier.location = location;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367 int32_t generation = deviceId + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +0800368 return std::make_shared<InputDevice>(&mFakeContext, deviceId, generation, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369 }
370
Prabir Pradhan28efc192019-11-05 01:10:04 +0000371 // Make the protected loopOnce method accessible to tests.
372 using InputReader::loopOnce;
373
Michael Wrightd02c5b62014-02-10 15:10:22 -0800374protected:
Chris Ye1c2e0892020-11-30 21:41:44 -0800375 virtual std::shared_ptr<InputDevice> createDeviceLocked(int32_t eventHubId,
376 const InputDeviceIdentifier& identifier)
377 REQUIRES(mLock) {
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800378 if (!mNextDevices.empty()) {
379 std::shared_ptr<InputDevice> device(std::move(mNextDevices.front()));
380 mNextDevices.pop();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800381 return device;
382 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800383 return InputReader::createDeviceLocked(eventHubId, identifier);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800384 }
385
arthurhungdcef2dc2020-08-11 14:47:50 +0800386 // --- FakeInputReaderContext ---
387 class FakeInputReaderContext : public ContextImpl {
388 int32_t mGlobalMetaState;
389 bool mUpdateGlobalMetaStateWasCalled;
390 int32_t mGeneration;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +0000391 std::optional<nsecs_t> mRequestedTimeout;
392 std::vector<InputDeviceInfo> mExternalStylusDevices;
arthurhungdcef2dc2020-08-11 14:47:50 +0800393
394 public:
395 FakeInputReaderContext(InputReader* reader)
396 : ContextImpl(reader),
397 mGlobalMetaState(0),
398 mUpdateGlobalMetaStateWasCalled(false),
399 mGeneration(1) {}
400
401 virtual ~FakeInputReaderContext() {}
402
403 void assertUpdateGlobalMetaStateWasCalled() {
404 ASSERT_TRUE(mUpdateGlobalMetaStateWasCalled)
405 << "Expected updateGlobalMetaState() to have been called.";
406 mUpdateGlobalMetaStateWasCalled = false;
407 }
408
409 void setGlobalMetaState(int32_t state) { mGlobalMetaState = state; }
410
411 uint32_t getGeneration() { return mGeneration; }
412
413 void updateGlobalMetaState() override {
414 mUpdateGlobalMetaStateWasCalled = true;
415 ContextImpl::updateGlobalMetaState();
416 }
417
418 int32_t getGlobalMetaState() override {
419 return mGlobalMetaState | ContextImpl::getGlobalMetaState();
420 }
421
422 int32_t bumpGeneration() override {
423 mGeneration = ContextImpl::bumpGeneration();
424 return mGeneration;
425 }
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +0000426
427 void requestTimeoutAtTime(nsecs_t when) override { mRequestedTimeout = when; }
428
429 void assertTimeoutWasRequested(nsecs_t when) {
430 ASSERT_TRUE(mRequestedTimeout) << "Expected timeout at time " << when
431 << " but there was no timeout requested.";
432 ASSERT_EQ(when, *mRequestedTimeout);
433 mRequestedTimeout.reset();
434 }
435
436 void assertTimeoutWasNotRequested() {
437 ASSERT_FALSE(mRequestedTimeout) << "Expected no timeout to have been requested,"
438 " but one was requested at time "
439 << *mRequestedTimeout;
440 }
441
442 void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override {
443 outDevices = mExternalStylusDevices;
444 }
445
446 void setExternalStylusDevices(std::vector<InputDeviceInfo>&& devices) {
447 mExternalStylusDevices = devices;
448 }
arthurhungdcef2dc2020-08-11 14:47:50 +0800449 } mFakeContext;
450
Michael Wrightd02c5b62014-02-10 15:10:22 -0800451 friend class InputReaderTest;
arthurhungdcef2dc2020-08-11 14:47:50 +0800452
453public:
454 FakeInputReaderContext* getContext() { return &mFakeContext; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800455};
456
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700457// --- InputReaderPolicyTest ---
458class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700459protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700460 sp<FakeInputReaderPolicy> mFakePolicy;
461
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700462 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700463 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700464};
465
466/**
467 * Check that empty set of viewports is an acceptable configuration.
468 * Also try to get internal viewport two different ways - by type and by uniqueId.
469 *
470 * There will be confusion if two viewports with empty uniqueId and identical type are present.
471 * Such configuration is not currently allowed.
472 */
473TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700474 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700475
476 // We didn't add any viewports yet, so there shouldn't be any.
477 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100478 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700479 ASSERT_FALSE(internalViewport);
480
481 // Add an internal viewport, then clear it
482 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000483 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100484 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700485
486 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700487 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700488 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100489 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700490
491 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100492 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700493 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700494 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700495
496 mFakePolicy->clearViewports();
497 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700498 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700499 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100500 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700501 ASSERT_FALSE(internalViewport);
502}
503
504TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
505 const std::string internalUniqueId = "local:0";
506 const std::string externalUniqueId = "local:1";
507 const std::string virtualUniqueId1 = "virtual:2";
508 const std::string virtualUniqueId2 = "virtual:3";
509 constexpr int32_t virtualDisplayId1 = 2;
510 constexpr int32_t virtualDisplayId2 = 3;
511
512 // Add an internal viewport
513 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000514 DISPLAY_ORIENTATION_0, true /*isActive*/, internalUniqueId,
515 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700516 // Add an external viewport
517 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000518 DISPLAY_ORIENTATION_0, true /*isActive*/, externalUniqueId,
519 NO_PORT, ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700520 // Add an virtual viewport
521 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000522 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId1,
523 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700524 // Add another virtual viewport
525 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000526 DISPLAY_ORIENTATION_0, true /*isActive*/, virtualUniqueId2,
527 NO_PORT, ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700528
529 // Check matching by type for internal
530 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100531 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700532 ASSERT_TRUE(internalViewport);
533 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
534
535 // Check matching by type for external
536 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100537 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700538 ASSERT_TRUE(externalViewport);
539 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
540
541 // Check matching by uniqueId for virtual viewport #1
542 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700543 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700544 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100545 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700546 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
547 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
548
549 // Check matching by uniqueId for virtual viewport #2
550 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700551 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700552 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100553 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700554 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
555 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
556}
557
558
559/**
560 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
561 * that lookup works by checking display id.
562 * Check that 2 viewports of each kind is possible, for all existing viewport types.
563 */
564TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
565 const std::string uniqueId1 = "uniqueId1";
566 const std::string uniqueId2 = "uniqueId2";
567 constexpr int32_t displayId1 = 2;
568 constexpr int32_t displayId2 = 3;
569
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100570 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
571 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700572 for (const ViewportType& type : types) {
573 mFakePolicy->clearViewports();
574 // Add a viewport
575 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000576 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1,
577 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700578 // Add another viewport
579 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000580 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2,
581 NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700582
583 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700584 std::optional<DisplayViewport> viewport1 =
585 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700586 ASSERT_TRUE(viewport1);
587 ASSERT_EQ(displayId1, viewport1->displayId);
588 ASSERT_EQ(type, viewport1->type);
589
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700590 std::optional<DisplayViewport> viewport2 =
591 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700592 ASSERT_TRUE(viewport2);
593 ASSERT_EQ(displayId2, viewport2->displayId);
594 ASSERT_EQ(type, viewport2->type);
595
596 // When there are multiple viewports of the same kind, and uniqueId is not specified
597 // in the call to getDisplayViewport, then that situation is not supported.
598 // The viewports can be stored in any order, so we cannot rely on the order, since that
599 // is just implementation detail.
600 // However, we can check that it still returns *a* viewport, we just cannot assert
601 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700602 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700603 ASSERT_TRUE(someViewport);
604 }
605}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700607/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000608 * When we have multiple internal displays make sure we always return the default display when
609 * querying by type.
610 */
611TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
612 const std::string uniqueId1 = "uniqueId1";
613 const std::string uniqueId2 = "uniqueId2";
614 constexpr int32_t nonDefaultDisplayId = 2;
615 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
616 "Test display ID should not be ADISPLAY_ID_DEFAULT");
617
618 // Add the default display first and ensure it gets returned.
619 mFakePolicy->clearViewports();
620 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000621 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000622 ViewportType::INTERNAL);
623 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000624 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000625 ViewportType::INTERNAL);
626
627 std::optional<DisplayViewport> viewport =
628 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
629 ASSERT_TRUE(viewport);
630 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
631 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
632
633 // Add the default display second to make sure order doesn't matter.
634 mFakePolicy->clearViewports();
635 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000636 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000637 ViewportType::INTERNAL);
638 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000639 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000640 ViewportType::INTERNAL);
641
642 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
643 ASSERT_TRUE(viewport);
644 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
645 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
646}
647
648/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700649 * Check getDisplayViewportByPort
650 */
651TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100652 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700653 const std::string uniqueId1 = "uniqueId1";
654 const std::string uniqueId2 = "uniqueId2";
655 constexpr int32_t displayId1 = 1;
656 constexpr int32_t displayId2 = 2;
657 const uint8_t hdmi1 = 0;
658 const uint8_t hdmi2 = 1;
659 const uint8_t hdmi3 = 2;
660
661 mFakePolicy->clearViewports();
662 // Add a viewport that's associated with some display port that's not of interest.
663 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000664 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId1, hdmi3,
665 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700666 // Add another viewport, connected to HDMI1 port
667 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +0000668 DISPLAY_ORIENTATION_0, true /*isActive*/, uniqueId2, hdmi1,
669 type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700670
671 // Check that correct display viewport was returned by comparing the display ports.
672 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
673 ASSERT_TRUE(hdmi1Viewport);
674 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
675 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
676
677 // Check that we can still get the same viewport using the uniqueId
678 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
679 ASSERT_TRUE(hdmi1Viewport);
680 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
681 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
682 ASSERT_EQ(type, hdmi1Viewport->type);
683
684 // Check that we cannot find a port with "HDMI2", because we never added one
685 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
686 ASSERT_FALSE(hdmi2Viewport);
687}
688
Michael Wrightd02c5b62014-02-10 15:10:22 -0800689// --- InputReaderTest ---
690
691class InputReaderTest : public testing::Test {
692protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700693 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800694 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700695 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000696 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800697
Chris Yea52ade12020-08-27 16:49:20 -0700698 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700699 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700700 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700701 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800702
Prabir Pradhan28efc192019-11-05 01:10:04 +0000703 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700704 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800705 }
706
Chris Yea52ade12020-08-27 16:49:20 -0700707 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700708 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800709 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800710 }
711
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700712 void addDevice(int32_t eventHubId, const std::string& name,
713 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800714 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800715
716 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800717 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800718 }
719 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000720 mReader->loopOnce();
721 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700722 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
723 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800724 }
725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800726 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700727 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000728 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700729 }
730
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800731 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700732 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000733 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700734 }
735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800736 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700737 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700738 ftl::Flags<InputDeviceClass> classes,
739 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800740 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800741 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
742 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800743 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800744 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800745 return mapper;
746 }
747};
748
Chris Ye98d3f532020-10-01 21:48:59 -0700749TEST_F(InputReaderTest, PolicyGetInputDevices) {
750 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700751 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700752 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753
754 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700755 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800757 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100758 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800759 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
760 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000761 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800762}
763
Chris Yee7310032020-09-22 15:36:28 -0700764TEST_F(InputReaderTest, GetMergedInputDevices) {
765 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
766 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
767 // Add two subdevices to device
768 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
769 // Must add at least one mapper or the device will be ignored!
770 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
771 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
772
773 // Push same device instance for next device to be added, so they'll have same identifier.
774 mReader->pushNextDevice(device);
775 mReader->pushNextDevice(device);
776 ASSERT_NO_FATAL_FAILURE(
777 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
778 ASSERT_NO_FATAL_FAILURE(
779 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
780
781 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000782 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700783}
784
Chris Yee14523a2020-12-19 13:46:00 -0800785TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
786 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
787 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
788 // Add two subdevices to device
789 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
790 // Must add at least one mapper or the device will be ignored!
791 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
792 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
793
794 // Push same device instance for next device to be added, so they'll have same identifier.
795 mReader->pushNextDevice(device);
796 mReader->pushNextDevice(device);
797 // Sensor device is initially disabled
798 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
799 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
800 nullptr));
801 // Device is disabled because the only sub device is a sensor device and disabled initially.
802 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
803 ASSERT_FALSE(device->isEnabled());
804 ASSERT_NO_FATAL_FAILURE(
805 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
806 // The merged device is enabled if any sub device is enabled
807 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
808 ASSERT_TRUE(device->isEnabled());
809}
810
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700811TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800812 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700813 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800814 constexpr int32_t eventHubId = 1;
815 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700816 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800817 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800818 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800819 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700820
Yi Kong9b14ac62018-07-17 13:48:38 -0700821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700822
823 NotifyDeviceResetArgs resetArgs;
824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700825 ASSERT_EQ(deviceId, resetArgs.deviceId);
826
827 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800828 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000829 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700830
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700832 ASSERT_EQ(deviceId, resetArgs.deviceId);
833 ASSERT_EQ(device->isEnabled(), false);
834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800835 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000836 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700839 ASSERT_EQ(device->isEnabled(), false);
840
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800841 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000842 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700844 ASSERT_EQ(deviceId, resetArgs.deviceId);
845 ASSERT_EQ(device->isEnabled(), true);
846}
847
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800849 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700850 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800851 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800852 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800853 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800854 AINPUT_SOURCE_KEYBOARD, nullptr);
855 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856
857 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
858 AINPUT_SOURCE_ANY, AKEYCODE_A))
859 << "Should return unknown when the device id is >= 0 but unknown.";
860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800861 ASSERT_EQ(AKEY_STATE_UNKNOWN,
862 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
863 << "Should return unknown when the device id is valid but the sources are not "
864 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 ASSERT_EQ(AKEY_STATE_DOWN,
867 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
868 AKEYCODE_A))
869 << "Should return value provided by mapper when device id is valid and the device "
870 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800871
872 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
873 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
874 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
875
876 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
877 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
878 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
879}
880
Philip Junker4af3b3d2021-12-14 10:36:55 +0100881TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
882 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
883 constexpr int32_t eventHubId = 1;
884 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
885 InputDeviceClass::KEYBOARD,
886 AINPUT_SOURCE_KEYBOARD, nullptr);
887 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
888
889 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
890 << "Should return unknown when the device with the specified id is not found.";
891
892 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
893 << "Should return correct mapping when device id is valid and mapping exists.";
894
895 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
896 << "Should return the location key code when device id is valid and there's no "
897 "mapping.";
898}
899
900TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
901 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
902 constexpr int32_t eventHubId = 1;
903 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
904 InputDeviceClass::JOYSTICK,
905 AINPUT_SOURCE_GAMEPAD, nullptr);
906 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
907
908 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
909 << "Should return unknown when the device id is valid but there is no keyboard mapper";
910}
911
Michael Wrightd02c5b62014-02-10 15:10:22 -0800912TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800913 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700914 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800915 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800916 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800917 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800918 AINPUT_SOURCE_KEYBOARD, nullptr);
919 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800920
921 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
922 AINPUT_SOURCE_ANY, KEY_A))
923 << "Should return unknown when the device id is >= 0 but unknown.";
924
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800925 ASSERT_EQ(AKEY_STATE_UNKNOWN,
926 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
927 << "Should return unknown when the device id is valid but the sources are not "
928 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800930 ASSERT_EQ(AKEY_STATE_DOWN,
931 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
932 KEY_A))
933 << "Should return value provided by mapper when device id is valid and the device "
934 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800935
936 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
937 AINPUT_SOURCE_TRACKBALL, KEY_A))
938 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
939
940 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
941 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
942 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
943}
944
945TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800946 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700947 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800948 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800949 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800951 AINPUT_SOURCE_KEYBOARD, nullptr);
952 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953
954 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
955 AINPUT_SOURCE_ANY, SW_LID))
956 << "Should return unknown when the device id is >= 0 but unknown.";
957
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800958 ASSERT_EQ(AKEY_STATE_UNKNOWN,
959 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
960 << "Should return unknown when the device id is valid but the sources are not "
961 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800962
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800963 ASSERT_EQ(AKEY_STATE_DOWN,
964 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
965 SW_LID))
966 << "Should return value provided by mapper when device id is valid and the device "
967 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968
969 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
970 AINPUT_SOURCE_TRACKBALL, SW_LID))
971 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
972
973 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
974 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
975 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
976}
977
978TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800979 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700980 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800981 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800982 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800983 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800984 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100985
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800986 mapper.addSupportedKeyCode(AKEYCODE_A);
987 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800988
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700989 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800990 uint8_t flags[4] = { 0, 0, 0, 1 };
991
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700992 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 << "Should return false when device id is >= 0 but unknown.";
994 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
995
996 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700997 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800998 << "Should return false when device id is valid but the sources are not supported by "
999 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001000 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1001
1002 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001003 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001004 keyCodes, flags))
1005 << "Should return value provided by mapper when device id is valid and the device "
1006 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001007 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1008
1009 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001010 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1011 << "Should return false when the device id is < 0 but the sources are not supported by "
1012 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001013 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
1014
1015 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -07001016 ASSERT_TRUE(
1017 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
1018 << "Should return value provided by mapper when device id is < 0 and one of the "
1019 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08001020 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
1021}
1022
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001023TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001024 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -07001025 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001026
1027 NotifyConfigurationChangedArgs args;
1028
1029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
1030 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
1031}
1032
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001033TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001034 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001035 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001036 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001037 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001038 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001039 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001040 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001041 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001042
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001043 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001044 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08001045 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
1046
1047 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001048 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001049 ASSERT_EQ(when, event.when);
1050 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001051 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08001052 ASSERT_EQ(EV_KEY, event.type);
1053 ASSERT_EQ(KEY_A, event.code);
1054 ASSERT_EQ(1, event.value);
1055}
1056
Garfield Tan1c7bc862020-01-28 13:24:04 -08001057TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001058 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001059 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001060 constexpr int32_t eventHubId = 1;
1061 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001062 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001063 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001064 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001065 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001066
1067 NotifyDeviceResetArgs resetArgs;
1068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001069 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001070
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001071 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001072 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001074 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001075 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001077 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001078 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001080 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001081 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001083 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001084 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001086 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001087 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001088}
1089
Garfield Tan1c7bc862020-01-28 13:24:04 -08001090TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1091 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001092 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001093 constexpr int32_t eventHubId = 1;
1094 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1095 // Must add at least one mapper or the device will be ignored!
1096 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001097 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001098 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1099
1100 NotifyDeviceResetArgs resetArgs;
1101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1102 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1103}
1104
Arthur Hungc23540e2018-11-29 20:42:11 +08001105TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001106 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001107 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001108 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001109 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001110 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1111 FakeInputMapper& mapper =
1112 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001113 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001114
1115 const uint8_t hdmi1 = 1;
1116
1117 // Associated touch screen with second display.
1118 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1119
1120 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001121 mFakePolicy->clearViewports();
Arthur Hungc23540e2018-11-29 20:42:11 +08001122 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001123 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:0", NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001124 ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001125 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001126 DISPLAY_ORIENTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001127 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001128 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001129 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001130
1131 // Add the device, and make sure all of the callbacks are triggered.
1132 // The device is added after the input port associations are processed since
1133 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001134 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001137 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001138
Arthur Hung2c9a3342019-07-23 14:18:59 +08001139 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001140 ASSERT_EQ(deviceId, device->getId());
1141 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1142 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001143
1144 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001145 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001146 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001147 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001148}
1149
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001150TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1151 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001152 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001153 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1154 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1155 // Must add at least one mapper or the device will be ignored!
1156 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1157 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1158 mReader->pushNextDevice(device);
1159 mReader->pushNextDevice(device);
1160 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1161 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1162
1163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1164
1165 NotifyDeviceResetArgs resetArgs;
1166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1167 ASSERT_EQ(deviceId, resetArgs.deviceId);
1168 ASSERT_TRUE(device->isEnabled());
1169 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1170 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1171
1172 disableDevice(deviceId);
1173 mReader->loopOnce();
1174
1175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1176 ASSERT_EQ(deviceId, resetArgs.deviceId);
1177 ASSERT_FALSE(device->isEnabled());
1178 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1179 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1180
1181 enableDevice(deviceId);
1182 mReader->loopOnce();
1183
1184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1185 ASSERT_EQ(deviceId, resetArgs.deviceId);
1186 ASSERT_TRUE(device->isEnabled());
1187 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1188 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1189}
1190
1191TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1192 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001193 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001194 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1195 // Add two subdevices to device
1196 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1197 FakeInputMapper& mapperDevice1 =
1198 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1199 FakeInputMapper& mapperDevice2 =
1200 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1201 mReader->pushNextDevice(device);
1202 mReader->pushNextDevice(device);
1203 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1204 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1205
1206 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1207 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1208
1209 ASSERT_EQ(AKEY_STATE_DOWN,
1210 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1211 ASSERT_EQ(AKEY_STATE_DOWN,
1212 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1213 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1214 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1215}
1216
Prabir Pradhan7e186182020-11-10 13:56:45 -08001217TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1218 NotifyPointerCaptureChangedArgs args;
1219
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001220 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001221 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1222 mReader->loopOnce();
1223 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001224 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1225 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001226
1227 mFakePolicy->setPointerCapture(false);
1228 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1229 mReader->loopOnce();
1230 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001231 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001232
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001233 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001234 // does not change.
1235 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1236 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001237 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001238}
1239
Chris Ye87143712020-11-10 05:05:58 +00001240class FakeVibratorInputMapper : public FakeInputMapper {
1241public:
1242 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1243 : FakeInputMapper(deviceContext, sources) {}
1244
1245 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1246};
1247
1248TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1249 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001250 ftl::Flags<InputDeviceClass> deviceClass =
1251 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001252 constexpr int32_t eventHubId = 1;
1253 const char* DEVICE_LOCATION = "BLUETOOTH";
1254 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1255 FakeVibratorInputMapper& mapper =
1256 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1257 mReader->pushNextDevice(device);
1258
1259 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1260 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1261
1262 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1263 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1264}
1265
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001266// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001267
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001268class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001269public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001270 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001271
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001272 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001273
Andy Chenf9f1a022022-08-29 20:07:10 -04001274 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1275
Chris Yee2b1e5c2021-03-10 22:45:12 -08001276 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1277
1278 void dump(std::string& dump) override {}
1279
1280 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1281 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001282 }
1283
Chris Yee2b1e5c2021-03-10 22:45:12 -08001284 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1285 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001286 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001287
1288 bool setLightColor(int32_t lightId, int32_t color) override {
1289 getDeviceContext().setLightBrightness(lightId, color >> 24);
1290 return true;
1291 }
1292
1293 std::optional<int32_t> getLightColor(int32_t lightId) override {
1294 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1295 if (!result.has_value()) {
1296 return std::nullopt;
1297 }
1298 return result.value() << 24;
1299 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001300
1301 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1302
1303 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1304
1305private:
1306 InputDeviceContext& mDeviceContext;
1307 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1308 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001309 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001310};
1311
Chris Yee2b1e5c2021-03-10 22:45:12 -08001312TEST_F(InputReaderTest, BatteryGetCapacity) {
1313 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001314 ftl::Flags<InputDeviceClass> deviceClass =
1315 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001316 constexpr int32_t eventHubId = 1;
1317 const char* DEVICE_LOCATION = "BLUETOOTH";
1318 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001319 FakePeripheralController& controller =
1320 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001321 mReader->pushNextDevice(device);
1322
1323 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1324
Harry Cuttsa5b71292022-11-28 12:56:17 +00001325 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1326 FakeEventHub::BATTERY_CAPACITY);
1327 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001328}
1329
1330TEST_F(InputReaderTest, BatteryGetStatus) {
1331 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001332 ftl::Flags<InputDeviceClass> deviceClass =
1333 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001334 constexpr int32_t eventHubId = 1;
1335 const char* DEVICE_LOCATION = "BLUETOOTH";
1336 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001337 FakePeripheralController& controller =
1338 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001339 mReader->pushNextDevice(device);
1340
1341 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1342
Harry Cuttsa5b71292022-11-28 12:56:17 +00001343 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1344 FakeEventHub::BATTERY_STATUS);
1345 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001346}
1347
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001348TEST_F(InputReaderTest, BatteryGetDevicePath) {
1349 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1350 ftl::Flags<InputDeviceClass> deviceClass =
1351 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1352 constexpr int32_t eventHubId = 1;
1353 const char* DEVICE_LOCATION = "BLUETOOTH";
1354 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1355 device->addController<FakePeripheralController>(eventHubId);
1356 mReader->pushNextDevice(device);
1357
1358 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1359
Harry Cuttsa5b71292022-11-28 12:56:17 +00001360 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001361}
1362
Chris Ye3fdbfef2021-01-06 18:45:18 -08001363TEST_F(InputReaderTest, LightGetColor) {
1364 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001365 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001366 constexpr int32_t eventHubId = 1;
1367 const char* DEVICE_LOCATION = "BLUETOOTH";
1368 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001369 FakePeripheralController& controller =
1370 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001371 mReader->pushNextDevice(device);
1372 RawLightInfo info = {.id = 1,
1373 .name = "Mono",
1374 .maxBrightness = 255,
1375 .flags = InputLightClass::BRIGHTNESS,
1376 .path = ""};
1377 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
1378 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
1379
1380 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001381
Chris Yee2b1e5c2021-03-10 22:45:12 -08001382 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
1383 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001384 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
1385 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
1386}
1387
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001388// --- InputReaderIntegrationTest ---
1389
1390// These tests create and interact with the InputReader only through its interface.
1391// The InputReader is started during SetUp(), which starts its processing in its own
1392// thread. The tests use linux uinput to emulate input devices.
1393// NOTE: Interacting with the physical device while these tests are running may cause
1394// the tests to fail.
1395class InputReaderIntegrationTest : public testing::Test {
1396protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001397 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001398 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001399 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001400
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001401 std::shared_ptr<FakePointerController> mFakePointerController;
1402
Chris Yea52ade12020-08-27 16:49:20 -07001403 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001404#if !defined(__ANDROID__)
1405 GTEST_SKIP();
1406#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001407 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001408 mFakePointerController = std::make_shared<FakePointerController>();
1409 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001410 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
1411 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001412
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001413 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1414 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001415 ASSERT_EQ(mReader->start(), OK);
1416
1417 // Since this test is run on a real device, all the input devices connected
1418 // to the test device will show up in mReader. We wait for those input devices to
1419 // show up before beginning the tests.
1420 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1422 }
1423
Chris Yea52ade12020-08-27 16:49:20 -07001424 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001425#if !defined(__ANDROID__)
1426 return;
1427#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001428 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001429 mReader.reset();
1430 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001431 mFakePolicy.clear();
1432 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001433
1434 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1435 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1436 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1437 [&name](const InputDeviceInfo& info) {
1438 return info.getIdentifier().name == name;
1439 });
1440 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1441 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001442};
1443
1444TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1445 // An invalid input device that is only used for this test.
1446 class InvalidUinputDevice : public UinputDevice {
1447 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00001448 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001449
1450 private:
1451 void configureDevice(int fd, uinput_user_dev* device) override {}
1452 };
1453
1454 const size_t numDevices = mFakePolicy->getInputDevices().size();
1455
1456 // UinputDevice does not set any event or key bits, so InputReader should not
1457 // consider it as a valid device.
1458 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1459 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1460 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1461 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1462
1463 invalidDevice.reset();
1464 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1466 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1467}
1468
1469TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1470 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1471
1472 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1473 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1475 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1476
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001477 const auto device = findDeviceByName(keyboard->getName());
1478 ASSERT_TRUE(device.has_value());
1479 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1480 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1481 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001482
1483 keyboard.reset();
1484 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1485 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1486 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1487}
1488
1489TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1490 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1491 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1492
1493 NotifyConfigurationChangedArgs configChangedArgs;
1494 ASSERT_NO_FATAL_FAILURE(
1495 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001496 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001497 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1498
1499 NotifyKeyArgs keyArgs;
1500 keyboard->pressAndReleaseHomeKey();
1501 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1502 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001503 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001504 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001505 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001506 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001507 prevTimestamp = keyArgs.eventTime;
1508
1509 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1510 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001511 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001512 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001513 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001514}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001515
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001516TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1517 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1518 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1519
1520 const auto device = findDeviceByName(stylus->getName());
1521 ASSERT_TRUE(device.has_value());
1522
Prabir Pradhana3621852022-10-14 18:57:23 +00001523 // An external stylus with buttons should also be recognized as a keyboard.
1524 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001525 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1526 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1527
1528 const auto DOWN =
1529 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1530 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1531
1532 stylus->pressAndReleaseKey(BTN_STYLUS);
1533 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1534 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1535 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1536 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1537
1538 stylus->pressAndReleaseKey(BTN_STYLUS2);
1539 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1540 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1541 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1542 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1543
1544 stylus->pressAndReleaseKey(BTN_STYLUS3);
1545 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1546 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1547 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1548 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1549}
1550
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001551/**
1552 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1553 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1554 * are passed to the listener.
1555 */
1556static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1557TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1558 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1559 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1560 NotifyKeyArgs keyArgs;
1561
1562 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1563 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1564 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1565 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1566
1567 controller->pressAndReleaseKey(BTN_GEAR_UP);
1568 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1569 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1570 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1571}
1572
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001573// --- TouchIntegrationTest ---
1574
Arthur Hungaab25622020-01-16 11:22:11 +08001575class TouchIntegrationTest : public InputReaderIntegrationTest {
1576protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001577 const std::string UNIQUE_ID = "local:0";
1578
Chris Yea52ade12020-08-27 16:49:20 -07001579 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001580#if !defined(__ANDROID__)
1581 GTEST_SKIP();
1582#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001583 InputReaderIntegrationTest::SetUp();
1584 // At least add an internal display.
1585 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
1586 DISPLAY_ORIENTATION_0, UNIQUE_ID, NO_PORT,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001587 ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001588
1589 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1590 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1591 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001592 const auto info = findDeviceByName(mDevice->getName());
1593 ASSERT_TRUE(info);
1594 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001595 }
1596
1597 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
1598 int32_t orientation, const std::string& uniqueId,
1599 std::optional<uint8_t> physicalPort,
1600 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001601 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
1602 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001603 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1604 }
1605
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001606 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1607 NotifyMotionArgs args;
1608 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1609 EXPECT_EQ(action, args.action);
1610 ASSERT_EQ(points.size(), args.pointerCount);
1611 for (size_t i = 0; i < args.pointerCount; i++) {
1612 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1613 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1614 }
1615 }
1616
Arthur Hungaab25622020-01-16 11:22:11 +08001617 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001618 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001619};
1620
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001621TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1622 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1623 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1624 // presses).
1625 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1626 mDeviceInfo.getSources());
1627}
1628
Arthur Hungaab25622020-01-16 11:22:11 +08001629TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1630 NotifyMotionArgs args;
1631 const Point centerPoint = mDevice->getCenterPoint();
1632
1633 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001634 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001635 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001636 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001637 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1638 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1639
1640 // ACTION_MOVE
1641 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001642 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001643 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1645
1646 // ACTION_UP
1647 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001648 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001649 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1650 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1651}
1652
1653TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1654 NotifyMotionArgs args;
1655 const Point centerPoint = mDevice->getCenterPoint();
1656
1657 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001658 mDevice->sendSlot(FIRST_SLOT);
1659 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001660 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001661 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001662 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1663 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1664
1665 // ACTION_POINTER_DOWN (Second slot)
1666 const Point secondPoint = centerPoint + Point(100, 100);
1667 mDevice->sendSlot(SECOND_SLOT);
1668 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001669 mDevice->sendDown(secondPoint);
1670 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001671 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001672 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001673
1674 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001675 mDevice->sendMove(secondPoint + Point(1, 1));
1676 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001677 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1678 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1679
1680 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001681 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001682 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001683 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001684 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001685
1686 // ACTION_UP
1687 mDevice->sendSlot(FIRST_SLOT);
1688 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001689 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001690 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1691 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1692}
1693
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001694/**
1695 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1696 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1697 * data?
1698 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1699 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1700 * for Pointer 0 only is generated after.
1701 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1702 * events, we will not miss any information.
1703 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1704 * event generated afterwards that contains the newest movement of pointer 0.
1705 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1706 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1707 * losing information about non-palm pointers.
1708 */
1709TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1710 NotifyMotionArgs args;
1711 const Point centerPoint = mDevice->getCenterPoint();
1712
1713 // ACTION_DOWN
1714 mDevice->sendSlot(FIRST_SLOT);
1715 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1716 mDevice->sendDown(centerPoint);
1717 mDevice->sendSync();
1718 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1719
1720 // ACTION_POINTER_DOWN (Second slot)
1721 const Point secondPoint = centerPoint + Point(100, 100);
1722 mDevice->sendSlot(SECOND_SLOT);
1723 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1724 mDevice->sendDown(secondPoint);
1725 mDevice->sendSync();
1726 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1727
1728 // ACTION_MOVE (First slot)
1729 mDevice->sendSlot(FIRST_SLOT);
1730 mDevice->sendMove(centerPoint + Point(5, 5));
1731 // ACTION_POINTER_UP (Second slot)
1732 mDevice->sendSlot(SECOND_SLOT);
1733 mDevice->sendPointerUp();
1734 // Send a single sync for the above 2 pointer updates
1735 mDevice->sendSync();
1736
1737 // First, we should get POINTER_UP for the second pointer
1738 assertReceivedMotion(ACTION_POINTER_1_UP,
1739 {/*first pointer */ centerPoint + Point(5, 5),
1740 /*second pointer*/ secondPoint});
1741
1742 // Next, the MOVE event for the first pointer
1743 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1744}
1745
1746/**
1747 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1748 * move, and then it will go up, all in the same frame.
1749 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1750 * gets sent to the listener.
1751 */
1752TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1753 NotifyMotionArgs args;
1754 const Point centerPoint = mDevice->getCenterPoint();
1755
1756 // ACTION_DOWN
1757 mDevice->sendSlot(FIRST_SLOT);
1758 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1759 mDevice->sendDown(centerPoint);
1760 mDevice->sendSync();
1761 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1762
1763 // ACTION_POINTER_DOWN (Second slot)
1764 const Point secondPoint = centerPoint + Point(100, 100);
1765 mDevice->sendSlot(SECOND_SLOT);
1766 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1767 mDevice->sendDown(secondPoint);
1768 mDevice->sendSync();
1769 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1770
1771 // ACTION_MOVE (First slot)
1772 mDevice->sendSlot(FIRST_SLOT);
1773 mDevice->sendMove(centerPoint + Point(5, 5));
1774 // ACTION_POINTER_UP (Second slot)
1775 mDevice->sendSlot(SECOND_SLOT);
1776 mDevice->sendMove(secondPoint + Point(6, 6));
1777 mDevice->sendPointerUp();
1778 // Send a single sync for the above 2 pointer updates
1779 mDevice->sendSync();
1780
1781 // First, we should get POINTER_UP for the second pointer
1782 // The movement of the second pointer during the liftoff frame is ignored.
1783 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1784 assertReceivedMotion(ACTION_POINTER_1_UP,
1785 {/*first pointer */ centerPoint + Point(5, 5),
1786 /*second pointer*/ secondPoint});
1787
1788 // Next, the MOVE event for the first pointer
1789 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1790}
1791
Arthur Hungaab25622020-01-16 11:22:11 +08001792TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1793 NotifyMotionArgs args;
1794 const Point centerPoint = mDevice->getCenterPoint();
1795
1796 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001797 mDevice->sendSlot(FIRST_SLOT);
1798 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001799 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001800 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001801 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1802 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1803
arthurhungcc7f9802020-04-30 17:55:40 +08001804 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001805 const Point secondPoint = centerPoint + Point(100, 100);
1806 mDevice->sendSlot(SECOND_SLOT);
1807 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1808 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001809 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001810 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001811 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001812
arthurhungcc7f9802020-04-30 17:55:40 +08001813 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001814 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001815 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1818
arthurhungcc7f9802020-04-30 17:55:40 +08001819 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1820 // a palm event.
1821 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001822 mDevice->sendToolType(MT_TOOL_PALM);
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));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001825 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001826 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001827
arthurhungcc7f9802020-04-30 17:55:40 +08001828 // Send up to second slot, expect first slot send moving.
1829 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001830 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001831 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1832 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001833
arthurhungcc7f9802020-04-30 17:55:40 +08001834 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001835 mDevice->sendSlot(FIRST_SLOT);
1836 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001837 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001838
arthurhungcc7f9802020-04-30 17:55:40 +08001839 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001841}
1842
Prabir Pradhanda20b172022-09-26 17:01:18 +00001843TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1844 const Point centerPoint = mDevice->getCenterPoint();
1845
1846 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1847 mDevice->sendSlot(FIRST_SLOT);
1848 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1849 mDevice->sendToolType(MT_TOOL_PEN);
1850 mDevice->sendDown(centerPoint);
1851 mDevice->sendSync();
1852 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1853 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1854 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1855
1856 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1857
1858 // Release the stylus touch.
1859 mDevice->sendUp();
1860 mDevice->sendSync();
1861 ASSERT_NO_FATAL_FAILURE(
1862 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1863
1864 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1865
1866 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1867 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1868 mDevice->sendToolType(MT_TOOL_FINGER);
1869 mDevice->sendDown(centerPoint);
1870 mDevice->sendSync();
1871 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1872 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1873 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
1874
1875 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1876
1877 mDevice->sendUp();
1878 mDevice->sendSync();
1879 ASSERT_NO_FATAL_FAILURE(
1880 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1881
1882 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1883 // The policy should be notified of the stylus presence.
1884 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1885 mDevice->sendToolType(MT_TOOL_PEN);
1886 mDevice->sendMove(centerPoint);
1887 mDevice->sendSync();
1888 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1889 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1890 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1891
1892 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1893}
1894
Prabir Pradhan124ea442022-10-28 20:27:44 +00001895// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001896
Prabir Pradhan124ea442022-10-28 20:27:44 +00001897// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1898// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1899// stylus.
1900template <typename UinputStylusDevice>
1901class StylusButtonIntegrationTest : public TouchIntegrationTest {
1902protected:
1903 void SetUp() override {
1904#if !defined(__ANDROID__)
1905 GTEST_SKIP();
1906#endif
1907 TouchIntegrationTest::SetUp();
1908 mTouchscreen = mDevice.get();
1909 mTouchscreenInfo = mDeviceInfo;
1910
1911 setUpStylusDevice();
1912 }
1913
1914 UinputStylusDevice* mStylus{nullptr};
1915 InputDeviceInfo mStylusInfo{};
1916
1917 UinputTouchScreen* mTouchscreen{nullptr};
1918 InputDeviceInfo mTouchscreenInfo{};
1919
1920private:
1921 // When we are attempting to test stylus button events that are sent from the touchscreen,
1922 // use the same Uinput device for the touchscreen and the stylus.
1923 template <typename T = UinputStylusDevice>
1924 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1925 mStylus = mDevice.get();
1926 mStylusInfo = mDeviceInfo;
1927 }
1928
1929 // When we are attempting to stylus buttons from an external stylus being merged with touches
1930 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1931 template <typename T = UinputStylusDevice>
1932 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1933 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1934 mStylus = mStylusDeviceLifecycleTracker.get();
1935 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1936 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1937 const auto info = findDeviceByName(mStylus->getName());
1938 ASSERT_TRUE(info);
1939 mStylusInfo = *info;
1940 }
1941
1942 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1943
1944 // Hide the base class's device to expose it with a different name for readability.
1945 using TouchIntegrationTest::mDevice;
1946 using TouchIntegrationTest::mDeviceInfo;
1947};
1948
1949using StylusButtonIntegrationTestTypes =
1950 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1951TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1952
1953TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
1954 const auto stylusId = TestFixture::mStylusInfo.getId();
1955
1956 TestFixture::mStylus->pressKey(BTN_STYLUS);
1957 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1958 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1959 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1960
1961 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1962 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001963 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001964 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001965}
1966
Prabir Pradhan124ea442022-10-28 20:27:44 +00001967TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
1968 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1969 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1970 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001971
1972 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001973 TestFixture::mStylus->pressKey(BTN_STYLUS);
1974 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001975 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001976 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001977
1978 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001979 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1980 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1981 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1982 TestFixture::mTouchscreen->sendDown(centerPoint);
1983 TestFixture::mTouchscreen->sendSync();
1984 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001985 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1986 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001987 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1988 WithDeviceId(touchscreenId))));
1989 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001990 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1991 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001992 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1993 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001994
Prabir Pradhan124ea442022-10-28 20:27:44 +00001995 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1996 TestFixture::mTouchscreen->sendSync();
1997 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001998 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001999 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2000 WithDeviceId(touchscreenId))));
2001 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002002 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002003 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2004 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002005
2006 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002007 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2008 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002009 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002010 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002011}
2012
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002013TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
2014 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2015 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2016 const auto stylusId = TestFixture::mStylusInfo.getId();
2017 auto toolTypeDevice =
2018 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
2019
2020 // Press the stylus button.
2021 TestFixture::mStylus->pressKey(BTN_STYLUS);
2022 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2023 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2024 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2025
2026 // Start hovering with the stylus.
2027 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2028 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2029 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2030 TestFixture::mTouchscreen->sendMove(centerPoint);
2031 TestFixture::mTouchscreen->sendSync();
2032 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2033 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2034 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2035 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2036 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2037 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2038 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2039 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2040 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2041
2042 // Touch down with the stylus.
2043 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2044 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2045 TestFixture::mTouchscreen->sendDown(centerPoint);
2046 TestFixture::mTouchscreen->sendSync();
2047 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2048 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2049 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2050
2051 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2052 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2053 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2054
2055 // Stop touching with the stylus, and start hovering.
2056 TestFixture::mTouchscreen->sendUp();
2057 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2058 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2059 TestFixture::mTouchscreen->sendMove(centerPoint);
2060 TestFixture::mTouchscreen->sendSync();
2061 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2062 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2063 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2064 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2065 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2066 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2067 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2068 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2069 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2070
2071 // Stop hovering.
2072 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2073 TestFixture::mTouchscreen->sendSync();
2074 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2075 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2076 WithButtonState(0))));
2077 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2078 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2079 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2080 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2081
2082 // Release the stylus button.
2083 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2084 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2085 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2086 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2087}
2088
Prabir Pradhan124ea442022-10-28 20:27:44 +00002089TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
2090 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2091 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2092 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002093
2094 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002095 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2096 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2097 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2098 TestFixture::mTouchscreen->sendDown(centerPoint);
2099 TestFixture::mTouchscreen->sendSync();
2100 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002101 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002102 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2103 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002104
2105 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002106 TestFixture::mStylus->pressKey(BTN_STYLUS);
2107 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002108 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002109 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2110 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002111 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2112 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002113 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2114 WithDeviceId(touchscreenId))));
2115 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2117 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002118 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2119 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002120
Prabir Pradhan124ea442022-10-28 20:27:44 +00002121 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2122 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002123 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002124 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2125 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002127 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2128 WithDeviceId(touchscreenId))));
2129 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002130 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002131 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2132 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002133
2134 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002135 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2136 TestFixture::mTouchscreen->sendSync();
2137 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002138 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002139 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2140 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002141}
2142
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002143// --- ExternalStylusIntegrationTest ---
2144
2145// Verify the behavior of an external stylus. An external stylus can report pressure or button
2146// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2147// ongoing stylus gesture that is being emitted by the touchscreen.
2148using ExternalStylusIntegrationTest = TouchIntegrationTest;
2149
2150TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
2151 const Point centerPoint = mDevice->getCenterPoint();
2152
2153 // Create an external stylus capable of reporting pressure data that
2154 // should be fused with a touch pointer.
2155 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2156 createUinputDevice<UinputExternalStylusWithPressure>();
2157 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2158 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2159 const auto stylusInfo = findDeviceByName(stylus->getName());
2160 ASSERT_TRUE(stylusInfo);
2161
2162 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2163
2164 const auto touchscreenId = mDeviceInfo.getId();
2165
2166 // Set a pressure value on the stylus. It doesn't generate any events.
2167 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2168 stylus->setPressure(100);
2169 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2170
2171 // Start a finger gesture, and ensure it shows up as stylus gesture
2172 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002173 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002174 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002175 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002176 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002177 mDevice->sendSync();
2178 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2179 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002180 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2181 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002182
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002183 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2184 // event with the updated pressure.
2185 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002186 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2187 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002188 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2189 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002190
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002191 // The external stylus did not generate any events.
2192 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2193 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2194}
2195
2196TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
2197 const Point centerPoint = mDevice->getCenterPoint();
2198
2199 // Create an external stylus capable of reporting pressure data that
2200 // should be fused with a touch pointer.
2201 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2202 createUinputDevice<UinputExternalStylusWithPressure>();
2203 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2204 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2205 const auto stylusInfo = findDeviceByName(stylus->getName());
2206 ASSERT_TRUE(stylusInfo);
2207
2208 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2209
2210 const auto touchscreenId = mDeviceInfo.getId();
2211
2212 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2213 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002214 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2215 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002216 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002217 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002218
2219 // Start a finger gesture. The touch device will withhold generating any touches for
2220 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2221 mDevice->sendSlot(FIRST_SLOT);
2222 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2223 mDevice->sendToolType(MT_TOOL_FINGER);
2224 mDevice->sendDown(centerPoint);
2225 auto waitUntil = std::chrono::system_clock::now() +
2226 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002227 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002228 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002229
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002230 // Since the external stylus did not report a pressure value within the timeout,
2231 // it shows up as a finger pointer.
2232 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2233 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2234 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
2235 WithPressure(1.f))));
2236
2237 // Change the pressure on the external stylus. Since the pressure was not present at the start
2238 // of the gesture, it is ignored for now.
2239 stylus->setPressure(200);
2240 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2241
2242 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002243 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2244 mDevice->sendSync();
2245 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2246 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002247 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2248
2249 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2250 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2251 mDevice->sendToolType(MT_TOOL_FINGER);
2252 mDevice->sendDown(centerPoint);
2253 mDevice->sendSync();
2254 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2255 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2256 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2257 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2258
2259 // The external stylus did not generate any events.
2260 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2261 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002263
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002264TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
2265 const Point centerPoint = mDevice->getCenterPoint();
2266
2267 // Create an external stylus device that does not support pressure. It should not affect any
2268 // touch pointers.
2269 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2270 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2271 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2272 const auto stylusInfo = findDeviceByName(stylus->getName());
2273 ASSERT_TRUE(stylusInfo);
2274
2275 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2276
2277 const auto touchscreenId = mDeviceInfo.getId();
2278
2279 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2280 // pressure data from the external stylus.
2281 mDevice->sendSlot(FIRST_SLOT);
2282 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2283 mDevice->sendToolType(MT_TOOL_FINGER);
2284 mDevice->sendDown(centerPoint);
2285 auto waitUntil = std::chrono::system_clock::now() +
2286 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2287 mDevice->sendSync();
2288 ASSERT_NO_FATAL_FAILURE(
2289 mTestListener
2290 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2291 WithToolType(
2292 AMOTION_EVENT_TOOL_TYPE_FINGER),
2293 WithButtonState(0),
2294 WithDeviceId(touchscreenId),
2295 WithPressure(1.f)),
2296 waitUntil));
2297
2298 // The external stylus did not generate any events.
2299 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2300 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2301}
2302
Michael Wrightd02c5b62014-02-10 15:10:22 -08002303// --- InputDeviceTest ---
2304class InputDeviceTest : public testing::Test {
2305protected:
2306 static const char* DEVICE_NAME;
2307 static const char* DEVICE_LOCATION;
2308 static const int32_t DEVICE_ID;
2309 static const int32_t DEVICE_GENERATION;
2310 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002311 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002312 static const int32_t EVENTHUB_ID;
2313 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2314
2315 std::shared_ptr<FakeEventHub> mFakeEventHub;
2316 sp<FakeInputReaderPolicy> mFakePolicy;
2317 std::unique_ptr<TestInputListener> mFakeListener;
2318 std::unique_ptr<InstrumentedInputReader> mReader;
2319 std::shared_ptr<InputDevice> mDevice;
2320
2321 void SetUp() override {
2322 mFakeEventHub = std::make_unique<FakeEventHub>();
2323 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2324 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002325 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002326 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002327 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002328 identifier.name = DEVICE_NAME;
2329 identifier.location = DEVICE_LOCATION;
2330 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2331 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2332 identifier);
2333 mReader->pushNextDevice(mDevice);
2334 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002335 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002336 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002337
2338 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002339 mFakeListener.reset();
2340 mFakePolicy.clear();
2341 }
2342};
2343
2344const char* InputDeviceTest::DEVICE_NAME = "device";
2345const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2346const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2347const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002348const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002349const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2350 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002351const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2353
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002354TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002355 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002356 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2357 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002358}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002359
Michael Wrightd02c5b62014-02-10 15:10:22 -08002360TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002361 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2362
Michael Wrightd02c5b62014-02-10 15:10:22 -08002363 // Configuration
2364 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002365 InputReaderConfiguration config;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002366 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2367
Michael Wrightd02c5b62014-02-10 15:10:22 -08002368 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2369}
2370
2371TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2372 ASSERT_EQ(mDevice->isEnabled(), false);
2373}
2374
2375TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2376 // Configuration.
2377 InputReaderConfiguration config;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002378 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002379
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002380 // Reset.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 unused += mDevice->reset(ARBITRARY_TIME);
2382
2383 NotifyDeviceResetArgs resetArgs;
2384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2385 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2386 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2387
2388 // Metadata.
2389 ASSERT_TRUE(mDevice->isIgnored());
2390 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2391
2392 InputDeviceInfo info = mDevice->getDeviceInfo();
2393 ASSERT_EQ(DEVICE_ID, info.getId());
2394 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2395 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2396 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2397
2398 // State queries.
2399 ASSERT_EQ(0, mDevice->getMetaState());
2400
2401 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002402 << "Ignored device should return unknown key code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002403 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002404 << "Ignored device should return unknown scan code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2406 << "Ignored device should return unknown switch state.";
2407
2408 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2409 uint8_t flags[2] = { 0, 1 };
2410 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002411 << "Ignored device should never mark any key codes.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2413 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2414}
2415
2416TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2417 // Configuration.
2418 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2419
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002420 FakeInputMapper& mapper1 =
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002421 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002422 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002423 mapper1.setMetaState(AMETA_ALT_ON);
2424 mapper1.addSupportedKeyCode(AKEYCODE_A);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002425 mapper1.addSupportedKeyCode(AKEYCODE_B);
2426 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002427 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2428 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2429 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2430 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002431
Arthur Hung2c9a3342019-07-23 14:18:59 +08002432 FakeInputMapper& mapper2 =
2433 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002434 mapper2.setMetaState(AMETA_SHIFT_ON);
2435
Arthur Hung2c9a3342019-07-23 14:18:59 +08002436 InputReaderConfiguration config;
2437 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2438
2439 std::string propertyValue;
2440 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
2441 << "Device should have read configuration during configuration phase.";
2442 ASSERT_EQ("value", propertyValue);
2443
2444 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002445 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
2446
Arthur Hung2c9a3342019-07-23 14:18:59 +08002447 // Reset
2448 unused += mDevice->reset(ARBITRARY_TIME);
2449 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2450 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
2451
2452 NotifyDeviceResetArgs resetArgs;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2454 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002455 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2456
Arthur Hung2c9a3342019-07-23 14:18:59 +08002457 // Metadata.
2458 ASSERT_FALSE(mDevice->isIgnored());
2459 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2460
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002461 InputDeviceInfo info = mDevice->getDeviceInfo();
2462 ASSERT_EQ(DEVICE_ID, info.getId());
Arthur Hung2c9a3342019-07-23 14:18:59 +08002463 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2464 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2465 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002466
2467 // State queries.
Arthur Hung2c9a3342019-07-23 14:18:59 +08002468 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2469 << "Should query mappers and combine meta states.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002470
Christine Franks1ba71cc2021-04-07 14:37:42 -07002471 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2472 << "Should return unknown key code state when source not supported.";
2473 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2474 << "Should return unknown scan code state when source not supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002475 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2476 << "Should return unknown switch state when source not supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002477
2478 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2479 << "Should query mapper when source is supported.";
2480 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
Christine Franks2a2293c2022-01-18 11:51:16 -08002481 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002482 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2483 << "Should query mapper when source is supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002484
2485 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
2486 uint8_t flags[4] = { 0, 0, 0, 1 };
2487 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
2488 << "Should do nothing when source is unsupported.";
2489 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002490 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2491 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002492 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2493
2494 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2495 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002496 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2497 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002498 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2499 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2500
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002501 // Event handling.
2502 RawEvent event;
Christine Franks1ba71cc2021-04-07 14:37:42 -07002503 event.deviceId = EVENTHUB_ID;
2504 unused += mDevice->process(&event, 1);
2505
Christine Franks2a2293c2022-01-18 11:51:16 -08002506 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2507 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
2508}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002509
2510// A single input device is associated with a specific display. Check that:
Christine Franks2a2293c2022-01-18 11:51:16 -08002511// 1. Device is disabled if the viewport corresponding to the associated display is not found
Christine Franks2a2293c2022-01-18 11:51:16 -08002512// 2. Device is disabled when setEnabled API is called
2513TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
2514 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2515
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002516 // First Configuration.
2517 std::list<NotifyArgs> unused =
Christine Franks2a2293c2022-01-18 11:51:16 -08002518 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2519
2520 // Device should be enabled by default.
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002521 ASSERT_TRUE(mDevice->isEnabled());
2522
2523 // Prepare associated info.
2524 constexpr uint8_t hdmi = 1;
2525 const std::string UNIQUE_ID = "local:1";
2526
2527 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2528 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2529 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2530 // Device should be disabled because it is associated with a specific display via
2531 // input port <-> display port association, but the corresponding display is not found
2532 ASSERT_FALSE(mDevice->isEnabled());
2533
2534 // Prepare displays.
2535 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002536 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
2537 ViewportType::INTERNAL);
2538 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2539 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2540 ASSERT_TRUE(mDevice->isEnabled());
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002541
Michael Wrightd02c5b62014-02-10 15:10:22 -08002542 // Device should be disabled after set disable.
2543 mFakePolicy->addDisabledDevice(mDevice->getId());
2544 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002545 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002546 ASSERT_FALSE(mDevice->isEnabled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002547
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002548 // Device should still be disabled even found the associated display.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002549 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002550 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
arthurhungdcef2dc2020-08-11 14:47:50 +08002551 ASSERT_FALSE(mDevice->isEnabled());
2552}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002554TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002555 // Device should be enabled by default.
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07002556 mFakePolicy->clearViewports();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002557 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
arthurhungdcef2dc2020-08-11 14:47:50 +08002558 std::list<NotifyArgs> unused =
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002559 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
arthurhungdcef2dc2020-08-11 14:47:50 +08002560 ASSERT_TRUE(mDevice->isEnabled());
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002561
2562 // Device should be disabled because it is associated with a specific display, but the
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563 // corresponding display is not found.
2564 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002565 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhanc14266f2021-05-12 15:56:24 -07002566 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2567 ASSERT_FALSE(mDevice->isEnabled());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002568
Chris Yea52ade12020-08-27 16:49:20 -07002569 // Device should be enabled when a display is found.
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002570 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002572 NO_PORT, ViewportType::INTERNAL);
2573 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2574 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002575 ASSERT_TRUE(mDevice->isEnabled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
2577 // Device should be disabled after set disable.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002578 mFakePolicy->addDisabledDevice(mDevice->getId());
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00002579 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2580 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2581 ASSERT_FALSE(mDevice->isEnabled());
2582
arthurhungdcef2dc2020-08-11 14:47:50 +08002583 // Device should still be disabled even found the associated display.
2584 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhanc7ef27e2020-02-03 19:19:15 -08002585 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002586 ASSERT_FALSE(mDevice->isEnabled());
2587}
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002588
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002589TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2590 mFakePolicy->clearViewports();
2591 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002592 std::list<NotifyArgs> unused =
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002593 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002594
2595 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
arthurhungdcef2dc2020-08-11 14:47:50 +08002596 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
2597 DISPLAY_ORIENTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002598 NO_PORT, ViewportType::INTERNAL);
arthurhungdcef2dc2020-08-11 14:47:50 +08002599 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2600 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2601 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2602}
2603
2604/**
2605 * This test reproduces a crash caused by a dangling reference that remains after device is added
2606 * and removed. The reference is accessed in InputDevice::dump(..);
2607 */
2608TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2609 constexpr int32_t TEST_EVENTHUB_ID = 10;
2610 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002611
2612 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002613 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002614 device.removeEventHubDevice(TEST_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002615 std::string dumpStr, eventHubDevStr;
2616 device.dump(dumpStr, eventHubDevStr);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002617}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002618
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002619TEST_F(InputDeviceTest, GetBluetoothAddress) {
2620 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2621 ASSERT_TRUE(address);
2622 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2623}
2624
Michael Wrightd02c5b62014-02-10 15:10:22 -08002625// --- InputMapperTest ---
2626
2627class InputMapperTest : public testing::Test {
2628protected:
2629 static const char* DEVICE_NAME;
2630 static const char* DEVICE_LOCATION;
2631 static const int32_t DEVICE_ID;
2632 static const int32_t DEVICE_GENERATION;
2633 static const int32_t DEVICE_CONTROLLER_NUMBER;
2634 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
2635 static const int32_t EVENTHUB_ID;
2636
2637 std::shared_ptr<FakeEventHub> mFakeEventHub;
2638 sp<FakeInputReaderPolicy> mFakePolicy;
2639 std::unique_ptr<TestInputListener> mFakeListener;
2640 std::unique_ptr<InstrumentedInputReader> mReader;
2641 std::shared_ptr<InputDevice> mDevice;
2642
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002643 virtual void SetUp(ftl::Flags<InputDeviceClass> classes, int bus = 0) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 mFakeEventHub = std::make_unique<FakeEventHub>();
2645 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2646 mFakeListener = std::make_unique<TestInputListener>();
2647 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
2648 *mFakeListener);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002649 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes, bus);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650 // Consume the device reset notification generated when adding a new device.
2651 mFakeListener->assertNotifyDeviceResetWasCalled();
2652 }
2653
2654 void SetUp() override {
2655 SetUp(DEVICE_CLASSES);
2656 }
2657
2658 void TearDown() override {
2659 mFakeListener.reset();
2660 mFakePolicy.clear();
2661 }
2662
2663 void addConfigurationProperty(const char* key, const char* value) {
2664 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, key, value);
2665 }
2666
2667 std::list<NotifyArgs> configureDevice(uint32_t changes) {
2668 if (!changes ||
2669 (changes &
2670 (InputReaderConfiguration::CHANGE_DISPLAY_INFO |
2671 InputReaderConfiguration::CHANGE_POINTER_CAPTURE))) {
2672 mReader->requestRefreshConfiguration(changes);
2673 mReader->loopOnce();
2674 }
2675 std::list<NotifyArgs> out =
2676 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
2677 // Loop the reader to flush the input listener queue.
2678 for (const NotifyArgs& args : out) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002679 mFakeListener->notify(args);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680 }
2681 mReader->loopOnce();
2682 return out;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002683 }
2684
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002685 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002686 const std::string& location, int32_t eventHubId,
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002687 ftl::Flags<InputDeviceClass> classes, int bus = 0) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07002688 InputDeviceIdentifier identifier;
2689 identifier.name = name;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002690 identifier.location = location;
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002691 identifier.bus = bus;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002692 std::shared_ptr<InputDevice> device =
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
2694 identifier);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002695 mReader->pushNextDevice(device);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00002696 mFakeEventHub->addDevice(eventHubId, name, classes, bus);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002697 mReader->loopOnce();
2698 return device;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002699 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700
2701 template <class T, typename... Args>
2702 T& addMapperAndConfigure(Args... args) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002703 T& mapper = mDevice->addMapper<T>(EVENTHUB_ID, args...);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704 configureDevice(0);
2705 std::list<NotifyArgs> resetArgList = mDevice->reset(ARBITRARY_TIME);
Chris Ye42b06822020-08-07 11:39:33 -07002706 resetArgList += mapper.reset(ARBITRARY_TIME);
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002707 // Loop the reader to flush the input listener queue.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002708 for (const NotifyArgs& loopArgs : resetArgList) {
2709 mFakeListener->notify(loopArgs);
2710 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002711 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002712 return mapper;
2713 }
2714
2715 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
2716 int32_t orientation, const std::string& uniqueId,
2717 std::optional<uint8_t> physicalPort, ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002718 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
2719 uniqueId, physicalPort, viewportType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2721 }
2722
2723 void clearViewports() {
2724 mFakePolicy->clearViewports();
2725 }
2726
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002727 std::list<NotifyArgs> process(InputMapper& mapper, nsecs_t when, nsecs_t readTime, int32_t type,
2728 int32_t code, int32_t value) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002729 RawEvent event;
2730 event.when = when;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002731 event.readTime = readTime;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002732 event.deviceId = mapper.getDeviceContext().getEventHubId();
2733 event.type = type;
2734 event.code = code;
2735 event.value = value;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002736 std::list<NotifyArgs> processArgList = mapper.process(&event);
2737 for (const NotifyArgs& args : processArgList) {
2738 mFakeListener->notify(args);
2739 }
Prabir Pradhanb5174de2022-08-05 22:26:56 +00002740 // Loop the reader to flush the input listener queue.
arthurhungdcef2dc2020-08-11 14:47:50 +08002741 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002742 return processArgList;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743 }
2744
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00002745 void resetMapper(InputMapper& mapper, nsecs_t when) {
2746 const auto resetArgs = mapper.reset(when);
2747 for (const auto args : resetArgs) {
2748 mFakeListener->notify(args);
2749 }
2750 // Loop the reader to flush the input listener queue.
2751 mReader->loopOnce();
2752 }
2753
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00002754 std::list<NotifyArgs> handleTimeout(InputMapper& mapper, nsecs_t when) {
2755 std::list<NotifyArgs> generatedArgs = mapper.timeoutExpired(when);
2756 for (const NotifyArgs& args : generatedArgs) {
2757 mFakeListener->notify(args);
2758 }
2759 // Loop the reader to flush the input listener queue.
2760 mReader->loopOnce();
2761 return generatedArgs;
2762 }
2763
Michael Wrightd02c5b62014-02-10 15:10:22 -08002764 static void assertMotionRange(const InputDeviceInfo& info,
2765 int32_t axis, uint32_t source, float min, float max, float flat, float fuzz) {
2766 const InputDeviceInfo::MotionRange* range = info.getMotionRange(axis, source);
Yi Kong9b14ac62018-07-17 13:48:38 -07002767 ASSERT_TRUE(range != nullptr) << "Axis: " << axis << " Source: " << source;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002768 ASSERT_EQ(axis, range->axis) << "Axis: " << axis << " Source: " << source;
2769 ASSERT_EQ(source, range->source) << "Axis: " << axis << " Source: " << source;
2770 ASSERT_NEAR(min, range->min, EPSILON) << "Axis: " << axis << " Source: " << source;
2771 ASSERT_NEAR(max, range->max, EPSILON) << "Axis: " << axis << " Source: " << source;
2772 ASSERT_NEAR(flat, range->flat, EPSILON) << "Axis: " << axis << " Source: " << source;
2773 ASSERT_NEAR(fuzz, range->fuzz, EPSILON) << "Axis: " << axis << " Source: " << source;
2774 }
2775
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002776 static void assertPointerCoords(const PointerCoords& coords, float x, float y, float pressure,
2777 float size, float touchMajor, float touchMinor, float toolMajor,
2778 float toolMinor, float orientation, float distance,
2779 float scaledAxisEpsilon = 1.f) {
2780 ASSERT_NEAR(x, coords.getAxisValue(AMOTION_EVENT_AXIS_X), scaledAxisEpsilon);
2781 ASSERT_NEAR(y, coords.getAxisValue(AMOTION_EVENT_AXIS_Y), scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002782 ASSERT_NEAR(pressure, coords.getAxisValue(AMOTION_EVENT_AXIS_PRESSURE), EPSILON);
2783 ASSERT_NEAR(size, coords.getAxisValue(AMOTION_EVENT_AXIS_SIZE), EPSILON);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07002784 ASSERT_NEAR(touchMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MAJOR),
2785 scaledAxisEpsilon);
2786 ASSERT_NEAR(touchMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR),
2787 scaledAxisEpsilon);
2788 ASSERT_NEAR(toolMajor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR),
2789 scaledAxisEpsilon);
2790 ASSERT_NEAR(toolMinor, coords.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR),
2791 scaledAxisEpsilon);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002792 ASSERT_NEAR(orientation, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), EPSILON);
2793 ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
2794 }
2795
Michael Wright17db18e2020-06-26 20:51:44 +01002796 static void assertPosition(const FakePointerController& controller, float x, float y) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002797 float actualX, actualY;
Michael Wright17db18e2020-06-26 20:51:44 +01002798 controller.getPosition(&actualX, &actualY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002799 ASSERT_NEAR(x, actualX, 1);
2800 ASSERT_NEAR(y, actualY, 1);
2801 }
2802};
2803
2804const char* InputMapperTest::DEVICE_NAME = "device";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002805const char* InputMapperTest::DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002806const int32_t InputMapperTest::DEVICE_ID = END_RESERVED_ID + 1000;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002807const int32_t InputMapperTest::DEVICE_GENERATION = 2;
2808const int32_t InputMapperTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002809const ftl::Flags<InputDeviceClass> InputMapperTest::DEVICE_CLASSES =
2810 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002811const int32_t InputMapperTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002812
2813// --- SwitchInputMapperTest ---
2814
2815class SwitchInputMapperTest : public InputMapperTest {
2816protected:
2817};
2818
2819TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002820 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002822 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002823}
2824
2825TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002826 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002828 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002829 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002830
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002831 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002832 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002833}
2834
2835TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002836 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002837 std::list<NotifyArgs> out;
2838 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2839 ASSERT_TRUE(out.empty());
2840 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2841 ASSERT_TRUE(out.empty());
2842 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2843 ASSERT_TRUE(out.empty());
2844 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002845
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002846 ASSERT_EQ(1u, out.size());
2847 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002848 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002849 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2850 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002851 args.switchMask);
2852 ASSERT_EQ(uint32_t(0), args.policyFlags);
2853}
2854
Chris Ye87143712020-11-10 05:05:58 +00002855// --- VibratorInputMapperTest ---
2856class VibratorInputMapperTest : public InputMapperTest {
2857protected:
2858 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2859};
2860
2861TEST_F(VibratorInputMapperTest, GetSources) {
2862 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2863
2864 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2865}
2866
2867TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2868 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2869
2870 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2871}
2872
2873TEST_F(VibratorInputMapperTest, Vibrate) {
2874 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002875 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002876 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2877
2878 VibrationElement pattern(2);
2879 VibrationSequence sequence(2);
2880 pattern.duration = std::chrono::milliseconds(200);
2881 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2882 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2883 sequence.addElement(pattern);
2884 pattern.duration = std::chrono::milliseconds(500);
2885 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2886 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2887 sequence.addElement(pattern);
2888
2889 std::vector<int64_t> timings = {0, 1};
2890 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2891
2892 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002893 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002894 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002895 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002896 // Verify vibrator state listener was notified.
2897 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002898 ASSERT_EQ(1u, out.size());
2899 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2900 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2901 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002902 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002903 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002904 ASSERT_FALSE(mapper.isVibrating());
2905 // Verify vibrator state listener was notified.
2906 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002907 ASSERT_EQ(1u, out.size());
2908 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2909 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2910 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002911}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002912
Chris Yef59a2f42020-10-16 12:55:26 -07002913// --- SensorInputMapperTest ---
2914
2915class SensorInputMapperTest : public InputMapperTest {
2916protected:
2917 static const int32_t ACCEL_RAW_MIN;
2918 static const int32_t ACCEL_RAW_MAX;
2919 static const int32_t ACCEL_RAW_FUZZ;
2920 static const int32_t ACCEL_RAW_FLAT;
2921 static const int32_t ACCEL_RAW_RESOLUTION;
2922
2923 static const int32_t GYRO_RAW_MIN;
2924 static const int32_t GYRO_RAW_MAX;
2925 static const int32_t GYRO_RAW_FUZZ;
2926 static const int32_t GYRO_RAW_FLAT;
2927 static const int32_t GYRO_RAW_RESOLUTION;
2928
2929 static const float GRAVITY_MS2_UNIT;
2930 static const float DEGREE_RADIAN_UNIT;
2931
2932 void prepareAccelAxes();
2933 void prepareGyroAxes();
2934 void setAccelProperties();
2935 void setGyroProperties();
2936 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2937};
2938
2939const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2940const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2941const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2942const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2943const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2944
2945const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2946const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2947const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2948const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2949const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2950
2951const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2952const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2953
2954void SensorInputMapperTest::prepareAccelAxes() {
2955 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2956 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2957 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2958 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2959 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2960 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2961}
2962
2963void SensorInputMapperTest::prepareGyroAxes() {
2964 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2965 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2966 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2967 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2968 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2969 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2970}
2971
2972void SensorInputMapperTest::setAccelProperties() {
2973 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2974 /* sensorDataIndex */ 0);
2975 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2976 /* sensorDataIndex */ 1);
2977 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2978 /* sensorDataIndex */ 2);
2979 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2980 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2981 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2982 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2983 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2984}
2985
2986void SensorInputMapperTest::setGyroProperties() {
2987 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2988 /* sensorDataIndex */ 0);
2989 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2990 /* sensorDataIndex */ 1);
2991 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2992 /* sensorDataIndex */ 2);
2993 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2994 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2995 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2996 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2997 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2998}
2999
3000TEST_F(SensorInputMapperTest, GetSources) {
3001 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3002
3003 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3004}
3005
3006TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3007 setAccelProperties();
3008 prepareAccelAxes();
3009 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3010
3011 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3012 std::chrono::microseconds(10000),
3013 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003014 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003020
3021 NotifySensorArgs args;
3022 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3023 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3024 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3025
3026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3027 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3028 ASSERT_EQ(args.deviceId, DEVICE_ID);
3029 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3030 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3031 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3032 ASSERT_EQ(args.values, values);
3033 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3034}
3035
3036TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3037 setGyroProperties();
3038 prepareGyroAxes();
3039 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
3040
3041 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3042 std::chrono::microseconds(10000),
3043 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003044 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003045 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3046 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3049 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003050
3051 NotifySensorArgs args;
3052 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3053 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3054 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3055
3056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3057 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3058 ASSERT_EQ(args.deviceId, DEVICE_ID);
3059 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3060 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3061 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3062 ASSERT_EQ(args.values, values);
3063 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3064}
3065
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066// --- KeyboardInputMapperTest ---
3067
3068class KeyboardInputMapperTest : public InputMapperTest {
3069protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003070 const std::string UNIQUE_ID = "local:0";
3071
3072 void prepareDisplay(int32_t orientation);
3073
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003074 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003075 int32_t originalKeyCode, int32_t rotatedKeyCode,
3076 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077};
3078
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003079/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3080 * orientation.
3081 */
3082void KeyboardInputMapperTest::prepareDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003083 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3084 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003085}
3086
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003087void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003088 int32_t originalScanCode, int32_t originalKeyCode,
3089 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090 NotifyKeyArgs args;
3091
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3094 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3095 ASSERT_EQ(originalScanCode, args.scanCode);
3096 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003097 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3101 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3102 ASSERT_EQ(originalScanCode, args.scanCode);
3103 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003104 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003105}
3106
Michael Wrightd02c5b62014-02-10 15:10:22 -08003107TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003108 KeyboardInputMapper& mapper =
3109 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3110 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003111
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003112 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003113}
3114
3115TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3116 const int32_t USAGE_A = 0x070004;
3117 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003118 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3119 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003120 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3121 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3122 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003123
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003124 KeyboardInputMapper& mapper =
3125 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3126 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003127 // Initial metastate is AMETA_NONE.
3128 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003129
3130 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003132 NotifyKeyArgs args;
3133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3134 ASSERT_EQ(DEVICE_ID, args.deviceId);
3135 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3136 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3137 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3138 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3139 ASSERT_EQ(KEY_HOME, args.scanCode);
3140 ASSERT_EQ(AMETA_NONE, args.metaState);
3141 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3142 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3143 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3144
3145 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003146 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3148 ASSERT_EQ(DEVICE_ID, args.deviceId);
3149 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3150 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3151 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3152 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3153 ASSERT_EQ(KEY_HOME, args.scanCode);
3154 ASSERT_EQ(AMETA_NONE, args.metaState);
3155 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3156 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3157 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3158
3159 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3163 ASSERT_EQ(DEVICE_ID, args.deviceId);
3164 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3165 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3166 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3167 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3168 ASSERT_EQ(0, args.scanCode);
3169 ASSERT_EQ(AMETA_NONE, args.metaState);
3170 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3171 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3172 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3173
3174 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3176 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3178 ASSERT_EQ(DEVICE_ID, args.deviceId);
3179 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3180 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3181 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3182 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3183 ASSERT_EQ(0, args.scanCode);
3184 ASSERT_EQ(AMETA_NONE, args.metaState);
3185 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3186 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3187 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3188
3189 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3193 ASSERT_EQ(DEVICE_ID, args.deviceId);
3194 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3195 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3196 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3197 ASSERT_EQ(0, args.keyCode);
3198 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3199 ASSERT_EQ(AMETA_NONE, args.metaState);
3200 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3201 ASSERT_EQ(0U, args.policyFlags);
3202 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3203
3204 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3206 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3208 ASSERT_EQ(DEVICE_ID, args.deviceId);
3209 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3210 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3211 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3212 ASSERT_EQ(0, args.keyCode);
3213 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3214 ASSERT_EQ(AMETA_NONE, args.metaState);
3215 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3216 ASSERT_EQ(0U, args.policyFlags);
3217 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3218}
3219
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003220/**
3221 * Ensure that the readTime is set to the time when the EV_KEY is received.
3222 */
3223TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3224 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3225
3226 KeyboardInputMapper& mapper =
3227 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3228 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3229 NotifyKeyArgs args;
3230
3231 // Key down
3232 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
3233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3234 ASSERT_EQ(12, args.readTime);
3235
3236 // Key up
3237 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
3238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3239 ASSERT_EQ(15, args.readTime);
3240}
3241
Michael Wrightd02c5b62014-02-10 15:10:22 -08003242TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003243 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3244 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003245 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3246 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3247 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003249 KeyboardInputMapper& mapper =
3250 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3251 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
Arthur Hung95f68612022-04-07 14:08:22 +08003253 // Initial metastate is AMETA_NONE.
3254 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255
3256 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258 NotifyKeyArgs args;
3259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3260 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003261 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003262 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003263
3264 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003265 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3267 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269
3270 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003271 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3273 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003275
3276 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003277 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3279 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003280 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003281 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282}
3283
3284TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003285 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3287 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3288 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 KeyboardInputMapper& mapper =
3291 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3292 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003293
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003294 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3296 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3297 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3298 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3299 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3300 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3301 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3302 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3303}
3304
3305TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003306 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3307 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3308 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3309 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310
Michael Wrightd02c5b62014-02-10 15:10:22 -08003311 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003312 KeyboardInputMapper& mapper =
3313 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3314 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003316 prepareDisplay(DISPLAY_ORIENTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003317 ASSERT_NO_FATAL_FAILURE(
3318 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3319 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3320 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3321 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3322 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3323 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3324 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003326 clearViewports();
3327 prepareDisplay(DISPLAY_ORIENTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003328 ASSERT_NO_FATAL_FAILURE(
3329 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3330 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3331 AKEYCODE_DPAD_UP, DISPLAY_ID));
3332 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3333 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3334 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3335 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003336
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003337 clearViewports();
3338 prepareDisplay(DISPLAY_ORIENTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003339 ASSERT_NO_FATAL_FAILURE(
3340 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3341 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3342 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3343 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3344 AKEYCODE_DPAD_UP, DISPLAY_ID));
3345 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3346 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003347
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003348 clearViewports();
3349 prepareDisplay(DISPLAY_ORIENTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003350 ASSERT_NO_FATAL_FAILURE(
3351 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3352 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3353 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3354 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3355 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3356 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3357 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358
3359 // Special case: if orientation changes while key is down, we still emit the same keycode
3360 // in the key up as we did in the key down.
3361 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003362 clearViewports();
3363 prepareDisplay(DISPLAY_ORIENTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3366 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3367 ASSERT_EQ(KEY_UP, args.scanCode);
3368 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3369
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003370 clearViewports();
3371 prepareDisplay(DISPLAY_ORIENTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3374 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3375 ASSERT_EQ(KEY_UP, args.scanCode);
3376 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3377}
3378
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003379TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3380 // If the keyboard is not orientation aware,
3381 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003382 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003383
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003384 KeyboardInputMapper& mapper =
3385 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3386 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003387 NotifyKeyArgs args;
3388
3389 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3394 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3395
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003396 prepareDisplay(DISPLAY_ORIENTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3402}
3403
3404TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3405 // If the keyboard is orientation aware,
3406 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003407 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003408
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003409 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003410 KeyboardInputMapper& mapper =
3411 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3412 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003413 NotifyKeyArgs args;
3414
3415 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3416 // ^--- already checked by the previous test
3417
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003418 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003419 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003422 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3424 ASSERT_EQ(DISPLAY_ID, args.displayId);
3425
3426 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003427 clearViewports();
3428 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003429 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3434 ASSERT_EQ(newDisplayId, args.displayId);
3435}
3436
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003438 KeyboardInputMapper& mapper =
3439 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3440 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003442 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003443 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003445 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003446 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447}
3448
Philip Junker4af3b3d2021-12-14 10:36:55 +01003449TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3450 KeyboardInputMapper& mapper =
3451 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3452 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3453
3454 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3455 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3456 << "If a mapping is available, the result is equal to the mapping";
3457
3458 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3459 << "If no mapping is available, the result is the key location";
3460}
3461
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003463 KeyboardInputMapper& mapper =
3464 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3465 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003467 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003468 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003469
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003470 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003471 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472}
3473
3474TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003475 KeyboardInputMapper& mapper =
3476 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3477 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003478
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003482 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 ASSERT_TRUE(flags[0]);
3484 ASSERT_FALSE(flags[1]);
3485}
3486
3487TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003488 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3489 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3490 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3491 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3492 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003495 KeyboardInputMapper& mapper =
3496 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3497 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003498 // Initial metastate is AMETA_NONE.
3499 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500
3501 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003502 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3503 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3504 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003505
3506 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003509 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3510 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3511 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003512 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513
3514 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003515 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003517 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3518 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3519 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003520 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521
3522 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003525 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3526 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3527 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003528 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529
3530 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003533 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3534 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3535 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003536 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537
3538 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003541 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3542 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3543 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003544 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545
3546 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003547 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003549 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3550 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3551 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003552 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553}
3554
Chris Yea52ade12020-08-27 16:49:20 -07003555TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3556 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3558 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3559 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3560
3561 KeyboardInputMapper& mapper =
3562 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3563 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3564
Chris Yea52ade12020-08-27 16:49:20 -07003565 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003566 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003567 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3568 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3569 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3570 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3571
3572 NotifyKeyArgs args;
3573 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003574 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3576 ASSERT_EQ(AMETA_NONE, args.metaState);
3577 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3578 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3579 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3580
3581 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003582 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3584 ASSERT_EQ(AMETA_NONE, args.metaState);
3585 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3586 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3587 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3588}
3589
Arthur Hung2c9a3342019-07-23 14:18:59 +08003590TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3591 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003592 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3593 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3594 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3595 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003596
3597 // keyboard 2.
3598 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003599 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003600 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003601 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003602 std::shared_ptr<InputDevice> device2 =
3603 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003604 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003605
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003606 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3607 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3608 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3609 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003611 KeyboardInputMapper& mapper =
3612 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3613 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003614
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003615 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003616 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003617 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003618 std::list<NotifyArgs> unused =
3619 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3620 0 /*changes*/);
3621 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003622
3623 // Prepared displays and associated info.
3624 constexpr uint8_t hdmi1 = 0;
3625 constexpr uint8_t hdmi2 = 1;
3626 const std::string SECONDARY_UNIQUE_ID = "local:1";
3627
3628 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3629 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3630
3631 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003632 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3633 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003634 ASSERT_FALSE(device2->isEnabled());
3635
3636 // Prepare second display.
3637 constexpr int32_t newDisplayId = 2;
3638 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003639 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003640 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, DISPLAY_ORIENTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003641 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003642 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003643 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3644 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003645
3646 // Device should be enabled after the associated display is found.
3647 ASSERT_TRUE(mDevice->isEnabled());
3648 ASSERT_TRUE(device2->isEnabled());
3649
3650 // Test pad key events
3651 ASSERT_NO_FATAL_FAILURE(
3652 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3653 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3654 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3655 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3656 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3657 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3658 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3659
3660 ASSERT_NO_FATAL_FAILURE(
3661 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3662 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3663 AKEYCODE_DPAD_RIGHT, newDisplayId));
3664 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3665 AKEYCODE_DPAD_DOWN, newDisplayId));
3666 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3667 AKEYCODE_DPAD_LEFT, newDisplayId));
3668}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669
arthurhungc903df12020-08-11 15:08:42 +08003670TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3671 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3672 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3673 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3674 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3675 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3677
3678 KeyboardInputMapper& mapper =
3679 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3680 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003681 // Initial metastate is AMETA_NONE.
3682 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003683
3684 // Initialization should have turned all of the lights off.
3685 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3686 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3687 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3688
3689 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003692 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3693 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3694
3695 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003698 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3699 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3700
3701 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003704 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3705 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3706
3707 mFakeEventHub->removeDevice(EVENTHUB_ID);
3708 mReader->loopOnce();
3709
3710 // keyboard 2 should default toggle keys.
3711 const std::string USB2 = "USB2";
3712 const std::string DEVICE_NAME2 = "KEYBOARD2";
3713 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3714 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3715 std::shared_ptr<InputDevice> device2 =
3716 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003717 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003718 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3719 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3720 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3721 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3722 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3723 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3724
arthurhung6fe95782020-10-05 22:41:16 +08003725 KeyboardInputMapper& mapper2 =
3726 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3727 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003728 std::list<NotifyArgs> unused =
3729 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3730 0 /*changes*/);
3731 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003732
3733 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3734 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3735 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003736 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3737 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003738}
3739
Arthur Hungcb40a002021-08-03 14:31:01 +00003740TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3741 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3742 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3743 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3744
3745 // Suppose we have two mappers. (DPAD + KEYBOARD)
3746 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3747 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3748 KeyboardInputMapper& mapper =
3749 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3750 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003751 // Initial metastate is AMETA_NONE.
3752 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003753
3754 mReader->toggleCapsLockState(DEVICE_ID);
3755 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3756}
3757
Arthur Hungfb3cc112022-04-13 07:39:50 +00003758TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3759 // keyboard 1.
3760 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3761 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3762 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3763 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3764 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3765 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3766
3767 KeyboardInputMapper& mapper1 =
3768 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3769 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3770
3771 // keyboard 2.
3772 const std::string USB2 = "USB2";
3773 const std::string DEVICE_NAME2 = "KEYBOARD2";
3774 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3775 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3776 std::shared_ptr<InputDevice> device2 =
3777 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3778 ftl::Flags<InputDeviceClass>(0));
3779 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3780 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3781 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3782 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3783 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3784 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3785
3786 KeyboardInputMapper& mapper2 =
3787 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3788 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003789 std::list<NotifyArgs> unused =
3790 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3791 0 /*changes*/);
3792 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003793
Arthur Hung95f68612022-04-07 14:08:22 +08003794 // Initial metastate is AMETA_NONE.
3795 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3796 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3797
3798 // Toggle num lock on and off.
3799 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3800 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003801 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3802 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3803 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3804
3805 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3806 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3807 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3808 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3809 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3810
3811 // Toggle caps lock on and off.
3812 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3813 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3814 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3815 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3816 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3817
3818 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3819 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3820 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3821 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3822 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3823
3824 // Toggle scroll lock on and off.
3825 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3826 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3827 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3828 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3829 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3830
3831 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3832 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3833 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3834 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3835 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3836}
3837
Arthur Hung2141d542022-08-23 07:45:21 +00003838TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3839 const int32_t USAGE_A = 0x070004;
3840 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3841 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3842
3843 KeyboardInputMapper& mapper =
3844 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3845 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3846 // Key down by scan code.
3847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3848 NotifyKeyArgs args;
3849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3850 ASSERT_EQ(DEVICE_ID, args.deviceId);
3851 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3852 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3853 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3854 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3855 ASSERT_EQ(KEY_HOME, args.scanCode);
3856 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3857
3858 // Disable device, it should synthesize cancellation events for down events.
3859 mFakePolicy->addDisabledDevice(DEVICE_ID);
3860 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3861
3862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3863 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3864 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3865 ASSERT_EQ(KEY_HOME, args.scanCode);
3866 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3867}
3868
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003869// --- KeyboardInputMapperTest_ExternalDevice ---
3870
3871class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3872protected:
Chris Yea52ade12020-08-27 16:49:20 -07003873 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003874};
3875
3876TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003877 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3878 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003879
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003880 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3881 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3882 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3883 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 KeyboardInputMapper& mapper =
3886 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3887 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003888
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003890 NotifyKeyArgs args;
3891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3892 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3893
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003894 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3896 ASSERT_EQ(uint32_t(0), args.policyFlags);
3897
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3900 ASSERT_EQ(uint32_t(0), args.policyFlags);
3901
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003902 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3904 ASSERT_EQ(uint32_t(0), args.policyFlags);
3905
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3908 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3909
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003910 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3912 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3913}
3914
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003915TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003916 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003917
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003918 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3919 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3920 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003921
Powei Fengd041c5d2019-05-03 17:11:33 -07003922 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003923 KeyboardInputMapper& mapper =
3924 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3925 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003926
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003927 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003928 NotifyKeyArgs args;
3929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3930 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3931
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003932 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3934 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3935
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3938 ASSERT_EQ(uint32_t(0), args.policyFlags);
3939
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003940 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3942 ASSERT_EQ(uint32_t(0), args.policyFlags);
3943
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3946 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3947
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003948 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3950 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3951}
3952
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953// --- CursorInputMapperTest ---
3954
3955class CursorInputMapperTest : public InputMapperTest {
3956protected:
3957 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3958
Michael Wright17db18e2020-06-26 20:51:44 +01003959 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960
Chris Yea52ade12020-08-27 16:49:20 -07003961 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962 InputMapperTest::SetUp();
3963
Michael Wright17db18e2020-06-26 20:51:44 +01003964 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003965 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966 }
3967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003968 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3969 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003970
3971 void prepareDisplay(int32_t orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003972 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3973 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3974 }
3975
3976 void prepareSecondaryDisplay() {
3977 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
3978 DISPLAY_ORIENTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
3979 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003980 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003981
3982 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3983 float pressure) {
3984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3985 0.0f, 0.0f, 0.0f, EPSILON));
3986 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003987};
3988
3989const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003991void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3992 int32_t originalY, int32_t rotatedX,
3993 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003994 NotifyMotionArgs args;
3995
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4000 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004001 ASSERT_NO_FATAL_FAILURE(
4002 assertCursorPointerCoords(args.pointerCoords[0],
4003 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4004 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005}
4006
4007TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004008 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004009 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004011 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004012}
4013
4014TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004015 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004016 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004018 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004019}
4020
4021TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004022 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004023 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004024
4025 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004026 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004027
4028 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004029 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4030 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004031 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4032 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4033
4034 // When the bounds are set, then there should be a valid motion range.
4035 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
4036
4037 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004038 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039
4040 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4041 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4042 1, 800 - 1, 0.0f, 0.0f));
4043 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4044 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4045 2, 480 - 1, 0.0f, 0.0f));
4046 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4047 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4048 0.0f, 1.0f, 0.0f, 0.0f));
4049}
4050
4051TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004052 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004053 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004054
4055 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004056 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004057
4058 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4059 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4060 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4061 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4062 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4063 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4064 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4065 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4066 0.0f, 1.0f, 0.0f, 0.0f));
4067}
4068
4069TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004070 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004071 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072
arthurhungdcef2dc2020-08-11 14:47:50 +08004073 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074
4075 NotifyMotionArgs args;
4076
4077 // Button press.
4078 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4082 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4083 ASSERT_EQ(DEVICE_ID, args.deviceId);
4084 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4085 ASSERT_EQ(uint32_t(0), args.policyFlags);
4086 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4087 ASSERT_EQ(0, args.flags);
4088 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4089 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4090 ASSERT_EQ(0, args.edgeFlags);
4091 ASSERT_EQ(uint32_t(1), args.pointerCount);
4092 ASSERT_EQ(0, args.pointerProperties[0].id);
4093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004094 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004095 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4096 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4097 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4098
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4100 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4101 ASSERT_EQ(DEVICE_ID, args.deviceId);
4102 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4103 ASSERT_EQ(uint32_t(0), args.policyFlags);
4104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4105 ASSERT_EQ(0, args.flags);
4106 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4107 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4108 ASSERT_EQ(0, args.edgeFlags);
4109 ASSERT_EQ(uint32_t(1), args.pointerCount);
4110 ASSERT_EQ(0, args.pointerProperties[0].id);
4111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004112 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004113 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4114 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4115 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4116
Michael Wrightd02c5b62014-02-10 15:10:22 -08004117 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4119 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4121 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4122 ASSERT_EQ(DEVICE_ID, args.deviceId);
4123 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4124 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4126 ASSERT_EQ(0, args.flags);
4127 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4128 ASSERT_EQ(0, args.buttonState);
4129 ASSERT_EQ(0, args.edgeFlags);
4130 ASSERT_EQ(uint32_t(1), args.pointerCount);
4131 ASSERT_EQ(0, args.pointerProperties[0].id);
4132 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004133 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004134 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4135 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4136 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4137
4138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4139 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4140 ASSERT_EQ(DEVICE_ID, args.deviceId);
4141 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4142 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4144 ASSERT_EQ(0, args.flags);
4145 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4146 ASSERT_EQ(0, args.buttonState);
4147 ASSERT_EQ(0, args.edgeFlags);
4148 ASSERT_EQ(uint32_t(1), args.pointerCount);
4149 ASSERT_EQ(0, args.pointerProperties[0].id);
4150 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004151 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004152 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4153 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4154 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4155}
4156
4157TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004158 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004159 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
4161 NotifyMotionArgs args;
4162
4163 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004168 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4169 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4170 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171
4172 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004177 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4178 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179}
4180
4181TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004183 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
4185 NotifyMotionArgs args;
4186
4187 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4191 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004192 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4195 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004196 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004197
Michael Wrightd02c5b62014-02-10 15:10:22 -08004198 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004202 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004203 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004204
4205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004207 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208}
4209
4210TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004211 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004212 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213
4214 NotifyMotionArgs args;
4215
4216 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4222 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4224 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4225 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4228 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004229 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4230 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4231 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004232
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4238 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004239 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4240 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4241 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242
4243 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004248 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004249
4250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004252 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253}
4254
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004255TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004256 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004258 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4259 // need to be rotated.
4260 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004261 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004262
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004263 prepareDisplay(DISPLAY_ORIENTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004264 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4265 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4266 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4267 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4268 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4269 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4270 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4271 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4272}
4273
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004274TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004275 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004277 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4278 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004279 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004281 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004282 prepareDisplay(DISPLAY_ORIENTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4284 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4285 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4286 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4287 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4288 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4289 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4290 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4291
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004292 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004293 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004294 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4295 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4296 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4297 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4298 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4299 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4300 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4301 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004303 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004304 prepareDisplay(DISPLAY_ORIENTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4306 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4307 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4308 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4309 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4310 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4311 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4312 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4313
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004314 clearViewports();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004315 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004316 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4317 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4318 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4319 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4320 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4321 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4322 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4323 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324}
4325
4326TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004328 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004329
4330 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4331 mFakePointerController->setPosition(100, 200);
4332 mFakePointerController->setButtonState(0);
4333
4334 NotifyMotionArgs motionArgs;
4335 NotifyKeyArgs keyArgs;
4336
4337 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4341 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4342 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4343 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004344 ASSERT_NO_FATAL_FAILURE(
4345 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4348 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4349 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4350 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004351 ASSERT_NO_FATAL_FAILURE(
4352 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004354 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004357 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_EQ(0, motionArgs.buttonState);
4359 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004360 ASSERT_NO_FATAL_FAILURE(
4361 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362
4363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004364 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365 ASSERT_EQ(0, motionArgs.buttonState);
4366 ASSERT_EQ(0, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004372 ASSERT_EQ(0, motionArgs.buttonState);
4373 ASSERT_EQ(0, 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
4377 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4383 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4384 motionArgs.buttonState);
4385 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4386 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004387 ASSERT_NO_FATAL_FAILURE(
4388 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4391 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4392 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4393 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4394 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004395 ASSERT_NO_FATAL_FAILURE(
4396 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004397
4398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4400 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4401 motionArgs.buttonState);
4402 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4403 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004404 ASSERT_NO_FATAL_FAILURE(
4405 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004406
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4412 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(
4414 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004415
4416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004418 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4419 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004420 ASSERT_NO_FATAL_FAILURE(
4421 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004426 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4427 ASSERT_EQ(0, motionArgs.buttonState);
4428 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004429 ASSERT_NO_FATAL_FAILURE(
4430 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004433
4434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 ASSERT_EQ(0, motionArgs.buttonState);
4436 ASSERT_EQ(0, mFakePointerController->getButtonState());
4437 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004438 ASSERT_NO_FATAL_FAILURE(
4439 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004440
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4442 ASSERT_EQ(0, motionArgs.buttonState);
4443 ASSERT_EQ(0, mFakePointerController->getButtonState());
4444 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004445 ASSERT_NO_FATAL_FAILURE(
4446 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447
4448 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4452 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4453 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004454
Michael Wrightd02c5b62014-02-10 15:10:22 -08004455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004456 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4458 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004459 ASSERT_NO_FATAL_FAILURE(
4460 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004461
4462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4464 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4465 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004466 ASSERT_NO_FATAL_FAILURE(
4467 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004469 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4470 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004472 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 ASSERT_EQ(0, motionArgs.buttonState);
4474 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004475 ASSERT_NO_FATAL_FAILURE(
4476 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004477
4478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004480 ASSERT_EQ(0, motionArgs.buttonState);
4481 ASSERT_EQ(0, mFakePointerController->getButtonState());
4482
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004483 ASSERT_NO_FATAL_FAILURE(
4484 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4486 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4487 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4488
4489 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4493 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4494 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004495
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004497 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4499 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004500 ASSERT_NO_FATAL_FAILURE(
4501 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004502
4503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4505 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4506 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004507 ASSERT_NO_FATAL_FAILURE(
4508 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004513 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 ASSERT_EQ(0, motionArgs.buttonState);
4515 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004516 ASSERT_NO_FATAL_FAILURE(
4517 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004518
4519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4520 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4521 ASSERT_EQ(0, motionArgs.buttonState);
4522 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004523 ASSERT_NO_FATAL_FAILURE(
4524 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004525
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4527 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4528 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4529
4530 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4534 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4535 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004536
Michael Wrightd02c5b62014-02-10 15:10:22 -08004537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4540 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004541 ASSERT_NO_FATAL_FAILURE(
4542 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004543
4544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4545 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4546 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4547 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004548 ASSERT_NO_FATAL_FAILURE(
4549 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004554 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 ASSERT_EQ(0, motionArgs.buttonState);
4556 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004557 ASSERT_NO_FATAL_FAILURE(
4558 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004559
4560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4561 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4562 ASSERT_EQ(0, motionArgs.buttonState);
4563 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004564 ASSERT_NO_FATAL_FAILURE(
4565 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4568 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4569 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4570
4571 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4575 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4576 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004577
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4581 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004582 ASSERT_NO_FATAL_FAILURE(
4583 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004584
4585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4587 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4588 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004589 ASSERT_NO_FATAL_FAILURE(
4590 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004595 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596 ASSERT_EQ(0, motionArgs.buttonState);
4597 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004598 ASSERT_NO_FATAL_FAILURE(
4599 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004600
4601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4603 ASSERT_EQ(0, motionArgs.buttonState);
4604 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004605 ASSERT_NO_FATAL_FAILURE(
4606 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004607
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4609 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4610 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4611}
4612
4613TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004615 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004616
4617 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4618 mFakePointerController->setPosition(100, 200);
4619 mFakePointerController->setButtonState(0);
4620
4621 NotifyMotionArgs args;
4622
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004627 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4628 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4630 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 +01004631 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004632}
4633
4634TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004635 addConfigurationProperty("cursor.mode", "pointer");
4636 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004637 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004638
4639 NotifyDeviceResetArgs resetArgs;
4640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4641 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4642 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4643
4644 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4645 mFakePointerController->setPosition(100, 200);
4646 mFakePointerController->setButtonState(0);
4647
4648 NotifyMotionArgs args;
4649
4650 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004651 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);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4655 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4658 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 +01004659 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004660
4661 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4665 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4666 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4668 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4670 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4673 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4674
4675 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004676 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4677 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4679 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4680 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4681 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4682 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4684 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4685 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4687 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4688
4689 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4692 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4694 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4697 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 +01004698 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004699
4700 // Disable pointer capture and check that the device generation got bumped
4701 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004702 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004703 mFakePolicy->setPointerCapture(false);
4704 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004705 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004706
4707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004708 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4709
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4714 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4717 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 +01004718 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719}
4720
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004721/**
4722 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4723 * pointer acceleration or speed processing should not be applied.
4724 */
4725TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4726 addConfigurationProperty("cursor.mode", "pointer");
4727 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4728 100.f /*high threshold*/, 10.f /*acceleration*/);
4729 mFakePolicy->setVelocityControlParams(testParams);
4730 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4731
4732 NotifyDeviceResetArgs resetArgs;
4733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4734 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4735 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4736
4737 NotifyMotionArgs args;
4738
4739 // Move and verify scale is applied.
4740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4744 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4745 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4746 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4747 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4748 ASSERT_GT(relX, 10);
4749 ASSERT_GT(relY, 20);
4750
4751 // Enable Pointer Capture
4752 mFakePolicy->setPointerCapture(true);
4753 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4754 NotifyPointerCaptureChangedArgs captureArgs;
4755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4756 ASSERT_TRUE(captureArgs.request.enable);
4757
4758 // Move and verify scale is not applied.
4759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4760 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4763 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4765 ASSERT_EQ(10, args.pointerCoords[0].getX());
4766 ASSERT_EQ(20, args.pointerCoords[0].getY());
4767}
4768
Prabir Pradhan208360b2022-06-24 18:37:04 +00004769TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4770 addConfigurationProperty("cursor.mode", "pointer");
4771 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4772
4773 NotifyDeviceResetArgs resetArgs;
4774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4775 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4776 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4777
4778 // Ensure the display is rotated.
4779 prepareDisplay(DISPLAY_ORIENTATION_90);
4780
4781 NotifyMotionArgs args;
4782
4783 // Verify that the coordinates are rotated.
4784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4786 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4788 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4789 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4790 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4791 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4792
4793 // Enable Pointer Capture.
4794 mFakePolicy->setPointerCapture(true);
4795 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4796 NotifyPointerCaptureChangedArgs captureArgs;
4797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4798 ASSERT_TRUE(captureArgs.request.enable);
4799
4800 // Move and verify rotation is not applied.
4801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4803 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4805 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4807 ASSERT_EQ(10, args.pointerCoords[0].getX());
4808 ASSERT_EQ(20, args.pointerCoords[0].getY());
4809}
4810
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004811TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004812 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004813
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004814 // Set up the default display.
4815 prepareDisplay(DISPLAY_ORIENTATION_90);
4816
4817 // Set up the secondary display as the display on which the pointer should be shown.
4818 // The InputDevice is not associated with any display.
4819 prepareSecondaryDisplay();
4820 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004821 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4822
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004823 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004824 mFakePointerController->setPosition(100, 200);
4825 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004826
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004827 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004828 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004832 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4833 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4834 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01004835 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004836}
4837
4838TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4839 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4840
4841 // Set up the default display.
4842 prepareDisplay(DISPLAY_ORIENTATION_90);
4843
4844 // Set up the secondary display as the display on which the pointer should be shown,
4845 // and associate the InputDevice with the secondary display.
4846 prepareSecondaryDisplay();
4847 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4848 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4849 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4850
4851 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4852 mFakePointerController->setPosition(100, 200);
4853 mFakePointerController->setButtonState(0);
4854
4855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004859 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4860 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4861 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004862 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
4863}
4864
4865TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4866 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4867
4868 // Set up the default display as the display on which the pointer should be shown.
4869 prepareDisplay(DISPLAY_ORIENTATION_90);
4870 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4871
4872 // Associate the InputDevice with the secondary display.
4873 prepareSecondaryDisplay();
4874 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4875 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4876
4877 // The mapper should not generate any events because it is associated with a display that is
4878 // different from the pointer display.
4879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004883}
4884
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004885// --- BluetoothCursorInputMapperTest ---
4886
4887class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4888protected:
4889 void SetUp() override {
4890 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4891
4892 mFakePointerController = std::make_shared<FakePointerController>();
4893 mFakePolicy->setPointerController(mFakePointerController);
4894 }
4895};
4896
4897TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4898 addConfigurationProperty("cursor.mode", "pointer");
4899 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4900
4901 nsecs_t kernelEventTime = ARBITRARY_TIME;
4902 nsecs_t expectedEventTime = ARBITRARY_TIME;
4903 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4904 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4906 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4907 WithEventTime(expectedEventTime))));
4908
4909 // Process several events that come in quick succession, according to their timestamps.
4910 for (int i = 0; i < 3; i++) {
4911 constexpr static nsecs_t delta = ms2ns(1);
4912 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4913 kernelEventTime += delta;
4914 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4915
4916 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4917 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4919 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4920 WithEventTime(expectedEventTime))));
4921 }
4922}
4923
4924TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4925 addConfigurationProperty("cursor.mode", "pointer");
4926 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4927
4928 nsecs_t expectedEventTime = ARBITRARY_TIME;
4929 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4932 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4933 WithEventTime(expectedEventTime))));
4934
4935 // Process several events with the same timestamp from the kernel.
4936 // Ensure that we do not generate events too far into the future.
4937 constexpr static int32_t numEvents =
4938 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4939 for (int i = 0; i < numEvents; i++) {
4940 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4941
4942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4945 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4946 WithEventTime(expectedEventTime))));
4947 }
4948
4949 // By processing more events with the same timestamp, we should not generate events with a
4950 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4951 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4952 for (int i = 0; i < 3; i++) {
4953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4956 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4957 WithEventTime(cappedEventTime))));
4958 }
4959}
4960
4961TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4962 addConfigurationProperty("cursor.mode", "pointer");
4963 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4964
4965 nsecs_t kernelEventTime = ARBITRARY_TIME;
4966 nsecs_t expectedEventTime = ARBITRARY_TIME;
4967 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4968 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4970 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4971 WithEventTime(expectedEventTime))));
4972
4973 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4974 // smoothening is not needed, its timestamp is not affected.
4975 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4976 expectedEventTime = kernelEventTime;
4977
4978 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4979 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4981 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4982 WithEventTime(expectedEventTime))));
4983}
4984
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985// --- TouchInputMapperTest ---
4986
4987class TouchInputMapperTest : public InputMapperTest {
4988protected:
4989 static const int32_t RAW_X_MIN;
4990 static const int32_t RAW_X_MAX;
4991 static const int32_t RAW_Y_MIN;
4992 static const int32_t RAW_Y_MAX;
4993 static const int32_t RAW_TOUCH_MIN;
4994 static const int32_t RAW_TOUCH_MAX;
4995 static const int32_t RAW_TOOL_MIN;
4996 static const int32_t RAW_TOOL_MAX;
4997 static const int32_t RAW_PRESSURE_MIN;
4998 static const int32_t RAW_PRESSURE_MAX;
4999 static const int32_t RAW_ORIENTATION_MIN;
5000 static const int32_t RAW_ORIENTATION_MAX;
5001 static const int32_t RAW_DISTANCE_MIN;
5002 static const int32_t RAW_DISTANCE_MAX;
5003 static const int32_t RAW_TILT_MIN;
5004 static const int32_t RAW_TILT_MAX;
5005 static const int32_t RAW_ID_MIN;
5006 static const int32_t RAW_ID_MAX;
5007 static const int32_t RAW_SLOT_MIN;
5008 static const int32_t RAW_SLOT_MAX;
5009 static const float X_PRECISION;
5010 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005011 static const float X_PRECISION_VIRTUAL;
5012 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013
5014 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005015 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016
5017 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5018
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005019 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005020 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005021
Michael Wrightd02c5b62014-02-10 15:10:22 -08005022 enum Axes {
5023 POSITION = 1 << 0,
5024 TOUCH = 1 << 1,
5025 TOOL = 1 << 2,
5026 PRESSURE = 1 << 3,
5027 ORIENTATION = 1 << 4,
5028 MINOR = 1 << 5,
5029 ID = 1 << 6,
5030 DISTANCE = 1 << 7,
5031 TILT = 1 << 8,
5032 SLOT = 1 << 9,
5033 TOOL_TYPE = 1 << 10,
5034 };
5035
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005036 void prepareDisplay(int32_t orientation, std::optional<uint8_t> port = NO_PORT);
5037 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005038 void prepareVirtualDisplay(int32_t orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005040 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041 int32_t toRawX(float displayX);
5042 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005043 int32_t toRotatedRawX(float displayX);
5044 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005045 float toCookedX(float rawX, float rawY);
5046 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005048 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005050 float toDisplayY(int32_t rawY, int32_t displayHeight);
5051
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052};
5053
5054const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5055const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5056const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5057const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5058const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5059const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5060const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5061const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005062const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5063const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5065const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5066const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5067const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5068const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5069const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5070const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5071const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5072const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5073const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5074const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5075const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005076const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5077 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5078const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5079 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005080const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5081 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082
5083const float TouchInputMapperTest::GEOMETRIC_SCALE =
5084 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5085 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5086
5087const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5088 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5089 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5090};
5091
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005092void TouchInputMapperTest::prepareDisplay(int32_t orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005093 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5094 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005095}
5096
5097void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5098 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
5099 DISPLAY_ORIENTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100}
5101
Santos Cordonfa5cf462017-04-05 10:37:00 -07005102void TouchInputMapperTest::prepareVirtualDisplay(int32_t orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005103 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5104 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5105 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005106}
5107
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005109 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5110 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5111 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5112 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113}
5114
Jason Gerecke489fda82012-09-07 17:19:40 -07005115void TouchInputMapperTest::prepareLocationCalibration() {
5116 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5117}
5118
Michael Wrightd02c5b62014-02-10 15:10:22 -08005119int32_t TouchInputMapperTest::toRawX(float displayX) {
5120 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5121}
5122
5123int32_t TouchInputMapperTest::toRawY(float displayY) {
5124 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5125}
5126
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005127int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5128 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5129}
5130
5131int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5132 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5133}
5134
Jason Gerecke489fda82012-09-07 17:19:40 -07005135float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5136 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5137 return rawX;
5138}
5139
5140float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5141 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5142 return rawY;
5143}
5144
Michael Wrightd02c5b62014-02-10 15:10:22 -08005145float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005146 return toDisplayX(rawX, DISPLAY_WIDTH);
5147}
5148
5149float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5150 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005151}
5152
5153float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005154 return toDisplayY(rawY, DISPLAY_HEIGHT);
5155}
5156
5157float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5158 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005159}
5160
5161
5162// --- SingleTouchInputMapperTest ---
5163
5164class SingleTouchInputMapperTest : public TouchInputMapperTest {
5165protected:
5166 void prepareButtons();
5167 void prepareAxes(int axes);
5168
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005169 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5170 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5171 void processUp(SingleTouchInputMapper& mappery);
5172 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5173 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5174 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5175 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5176 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5177 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005178};
5179
5180void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005181 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005182}
5183
5184void SingleTouchInputMapperTest::prepareAxes(int axes) {
5185 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005186 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5187 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005188 }
5189 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005190 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5191 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005192 }
5193 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005194 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5195 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005196 }
5197 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005198 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5199 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005200 }
5201 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005202 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5203 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005204 }
5205}
5206
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005207void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005211}
5212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005213void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005216}
5217
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005218void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005220}
5221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005222void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005224}
5225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005226void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5227 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005229}
5230
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005233}
5234
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005235void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5236 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005239}
5240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005241void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5242 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244}
5245
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005246void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005248}
5249
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005251 prepareButtons();
5252 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005253 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005254
Harry Cutts16a24cc2022-10-26 15:22:19 +00005255 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005256}
5257
Michael Wrightd02c5b62014-02-10 15:10:22 -08005258TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005259 prepareButtons();
5260 prepareAxes(POSITION);
5261 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005262 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005263
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005264 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005265}
5266
5267TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005268 addConfigurationProperty("touch.deviceType", "touchScreen");
5269 prepareDisplay(DISPLAY_ORIENTATION_0);
5270 prepareButtons();
5271 prepareAxes(POSITION);
5272 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005273 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005274
5275 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005276 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005277
5278 // Virtual key is down.
5279 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5280 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5281 processDown(mapper, x, y);
5282 processSync(mapper);
5283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5284
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005285 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005286
5287 // Virtual key is up.
5288 processUp(mapper);
5289 processSync(mapper);
5290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5291
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005292 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005293}
5294
5295TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296 addConfigurationProperty("touch.deviceType", "touchScreen");
5297 prepareDisplay(DISPLAY_ORIENTATION_0);
5298 prepareButtons();
5299 prepareAxes(POSITION);
5300 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005301 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302
5303 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005304 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005305
5306 // Virtual key is down.
5307 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5308 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5309 processDown(mapper, x, y);
5310 processSync(mapper);
5311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5312
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005313 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005314
5315 // Virtual key is up.
5316 processUp(mapper);
5317 processSync(mapper);
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5319
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005320 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005321}
5322
5323TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005324 addConfigurationProperty("touch.deviceType", "touchScreen");
5325 prepareDisplay(DISPLAY_ORIENTATION_0);
5326 prepareButtons();
5327 prepareAxes(POSITION);
5328 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005329 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005330
Michael Wrightd02c5b62014-02-10 15:10:22 -08005331 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005332 ASSERT_TRUE(
5333 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005334 ASSERT_TRUE(flags[0]);
5335 ASSERT_FALSE(flags[1]);
5336}
5337
5338TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005339 addConfigurationProperty("touch.deviceType", "touchScreen");
5340 prepareDisplay(DISPLAY_ORIENTATION_0);
5341 prepareButtons();
5342 prepareAxes(POSITION);
5343 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005344 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005345
arthurhungdcef2dc2020-08-11 14:47:50 +08005346 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005347
5348 NotifyKeyArgs args;
5349
5350 // Press virtual key.
5351 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5352 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5353 processDown(mapper, x, y);
5354 processSync(mapper);
5355
5356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5357 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5358 ASSERT_EQ(DEVICE_ID, args.deviceId);
5359 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5360 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5361 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5362 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5363 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5364 ASSERT_EQ(KEY_HOME, args.scanCode);
5365 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5366 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5367
5368 // Release virtual key.
5369 processUp(mapper);
5370 processSync(mapper);
5371
5372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5373 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5374 ASSERT_EQ(DEVICE_ID, args.deviceId);
5375 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5376 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5377 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5378 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5379 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5380 ASSERT_EQ(KEY_HOME, args.scanCode);
5381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5382 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5383
5384 // Should not have sent any motions.
5385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5386}
5387
5388TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005389 addConfigurationProperty("touch.deviceType", "touchScreen");
5390 prepareDisplay(DISPLAY_ORIENTATION_0);
5391 prepareButtons();
5392 prepareAxes(POSITION);
5393 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005394 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005395
arthurhungdcef2dc2020-08-11 14:47:50 +08005396 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397
5398 NotifyKeyArgs keyArgs;
5399
5400 // Press virtual key.
5401 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5402 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5403 processDown(mapper, x, y);
5404 processSync(mapper);
5405
5406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5407 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5408 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5409 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5410 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5411 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5412 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5413 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5414 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5415 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5416 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5417
5418 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5419 // into the display area.
5420 y -= 100;
5421 processMove(mapper, x, y);
5422 processSync(mapper);
5423
5424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5425 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5426 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5427 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5428 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5429 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5430 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5431 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5432 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5433 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5434 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5435 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5436
5437 NotifyMotionArgs motionArgs;
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5439 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5440 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5441 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5442 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5443 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5444 ASSERT_EQ(0, motionArgs.flags);
5445 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5446 ASSERT_EQ(0, motionArgs.buttonState);
5447 ASSERT_EQ(0, motionArgs.edgeFlags);
5448 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5449 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5450 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5452 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5453 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5454 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5456
5457 // Keep moving out of bounds. Should generate a pointer move.
5458 y -= 50;
5459 processMove(mapper, x, y);
5460 processSync(mapper);
5461
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5463 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5464 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5465 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5466 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5468 ASSERT_EQ(0, motionArgs.flags);
5469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5470 ASSERT_EQ(0, motionArgs.buttonState);
5471 ASSERT_EQ(0, motionArgs.edgeFlags);
5472 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5476 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5477 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5478 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5480
5481 // Release out of bounds. Should generate a pointer up.
5482 processUp(mapper);
5483 processSync(mapper);
5484
5485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5486 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5487 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5488 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5489 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5490 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5491 ASSERT_EQ(0, motionArgs.flags);
5492 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5493 ASSERT_EQ(0, motionArgs.buttonState);
5494 ASSERT_EQ(0, motionArgs.edgeFlags);
5495 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5496 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5497 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5498 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5499 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5500 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5501 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5502 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5503
5504 // Should not have sent any more keys or motions.
5505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5507}
5508
5509TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510 addConfigurationProperty("touch.deviceType", "touchScreen");
5511 prepareDisplay(DISPLAY_ORIENTATION_0);
5512 prepareButtons();
5513 prepareAxes(POSITION);
5514 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005515 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516
arthurhungdcef2dc2020-08-11 14:47:50 +08005517 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518
5519 NotifyMotionArgs motionArgs;
5520
5521 // Initially go down out of bounds.
5522 int32_t x = -10;
5523 int32_t y = -10;
5524 processDown(mapper, x, y);
5525 processSync(mapper);
5526
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5528
5529 // Move into the display area. Should generate a pointer down.
5530 x = 50;
5531 y = 75;
5532 processMove(mapper, x, y);
5533 processSync(mapper);
5534
5535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5536 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5537 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5538 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5539 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5540 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5541 ASSERT_EQ(0, motionArgs.flags);
5542 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5543 ASSERT_EQ(0, motionArgs.buttonState);
5544 ASSERT_EQ(0, motionArgs.edgeFlags);
5545 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5546 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5549 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5550 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5551 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5553
5554 // Release. Should generate a pointer up.
5555 processUp(mapper);
5556 processSync(mapper);
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5560 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5561 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5562 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5563 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5564 ASSERT_EQ(0, motionArgs.flags);
5565 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5566 ASSERT_EQ(0, motionArgs.buttonState);
5567 ASSERT_EQ(0, motionArgs.edgeFlags);
5568 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5572 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5573 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5574 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5575 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5576
5577 // Should not have sent any more keys or motions.
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5580}
5581
Santos Cordonfa5cf462017-04-05 10:37:00 -07005582TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005583 addConfigurationProperty("touch.deviceType", "touchScreen");
5584 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5585
5586 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
5587 prepareButtons();
5588 prepareAxes(POSITION);
5589 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005590 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005591
arthurhungdcef2dc2020-08-11 14:47:50 +08005592 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005593
5594 NotifyMotionArgs motionArgs;
5595
5596 // Down.
5597 int32_t x = 100;
5598 int32_t y = 125;
5599 processDown(mapper, x, y);
5600 processSync(mapper);
5601
5602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5603 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5604 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5605 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5606 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5607 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5608 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5609 ASSERT_EQ(0, motionArgs.flags);
5610 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5611 ASSERT_EQ(0, motionArgs.buttonState);
5612 ASSERT_EQ(0, motionArgs.edgeFlags);
5613 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5614 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5617 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5618 1, 0, 0, 0, 0, 0, 0, 0));
5619 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5620 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5621 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5622
5623 // Move.
5624 x += 50;
5625 y += 75;
5626 processMove(mapper, x, y);
5627 processSync(mapper);
5628
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5630 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5631 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5632 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5633 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5634 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5636 ASSERT_EQ(0, motionArgs.flags);
5637 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5638 ASSERT_EQ(0, motionArgs.buttonState);
5639 ASSERT_EQ(0, motionArgs.edgeFlags);
5640 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5641 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5643 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5644 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5645 1, 0, 0, 0, 0, 0, 0, 0));
5646 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5647 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5648 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5649
5650 // Up.
5651 processUp(mapper);
5652 processSync(mapper);
5653
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5655 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5656 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5657 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5658 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5659 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5660 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5661 ASSERT_EQ(0, motionArgs.flags);
5662 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5663 ASSERT_EQ(0, motionArgs.buttonState);
5664 ASSERT_EQ(0, motionArgs.edgeFlags);
5665 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5666 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5669 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5670 1, 0, 0, 0, 0, 0, 0, 0));
5671 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5672 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5673 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5674
5675 // Should not have sent any more keys or motions.
5676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5678}
5679
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 addConfigurationProperty("touch.deviceType", "touchScreen");
5682 prepareDisplay(DISPLAY_ORIENTATION_0);
5683 prepareButtons();
5684 prepareAxes(POSITION);
5685 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005686 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005687
arthurhungdcef2dc2020-08-11 14:47:50 +08005688 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689
5690 NotifyMotionArgs motionArgs;
5691
5692 // Down.
5693 int32_t x = 100;
5694 int32_t y = 125;
5695 processDown(mapper, x, y);
5696 processSync(mapper);
5697
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5699 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5700 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5701 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5702 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5703 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5704 ASSERT_EQ(0, motionArgs.flags);
5705 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5706 ASSERT_EQ(0, motionArgs.buttonState);
5707 ASSERT_EQ(0, motionArgs.edgeFlags);
5708 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5709 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5712 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5713 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5714 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5715 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5716
5717 // Move.
5718 x += 50;
5719 y += 75;
5720 processMove(mapper, x, y);
5721 processSync(mapper);
5722
5723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5724 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5725 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5726 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5727 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5729 ASSERT_EQ(0, motionArgs.flags);
5730 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5731 ASSERT_EQ(0, motionArgs.buttonState);
5732 ASSERT_EQ(0, motionArgs.edgeFlags);
5733 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5734 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5737 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5738 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5739 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5740 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5741
5742 // Up.
5743 processUp(mapper);
5744 processSync(mapper);
5745
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5747 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5748 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5749 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5750 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5751 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5752 ASSERT_EQ(0, motionArgs.flags);
5753 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5754 ASSERT_EQ(0, motionArgs.buttonState);
5755 ASSERT_EQ(0, motionArgs.edgeFlags);
5756 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5757 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5760 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5761 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5762 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5763 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5764
5765 // Should not have sent any more keys or motions.
5766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5768}
5769
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005770TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005771 addConfigurationProperty("touch.deviceType", "touchScreen");
5772 prepareButtons();
5773 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005774 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5775 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005776 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777
5778 NotifyMotionArgs args;
5779
5780 // Rotation 90.
5781 prepareDisplay(DISPLAY_ORIENTATION_90);
5782 processDown(mapper, toRawX(50), toRawY(75));
5783 processSync(mapper);
5784
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5786 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5787 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5788
5789 processUp(mapper);
5790 processSync(mapper);
5791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5792}
5793
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005794TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795 addConfigurationProperty("touch.deviceType", "touchScreen");
5796 prepareButtons();
5797 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005798 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5799 // orientation-aware are affected by display rotation.
5800 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005801 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802
5803 NotifyMotionArgs args;
5804
5805 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005806 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 prepareDisplay(DISPLAY_ORIENTATION_0);
5808 processDown(mapper, toRawX(50), toRawY(75));
5809 processSync(mapper);
5810
5811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5812 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5813 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5814
5815 processUp(mapper);
5816 processSync(mapper);
5817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5818
5819 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005820 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005821 prepareDisplay(DISPLAY_ORIENTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005822 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005823 processSync(mapper);
5824
5825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5826 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5827 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5828
5829 processUp(mapper);
5830 processSync(mapper);
5831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5832
5833 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005834 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835 prepareDisplay(DISPLAY_ORIENTATION_180);
5836 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5837 processSync(mapper);
5838
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5840 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5841 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5842
5843 processUp(mapper);
5844 processSync(mapper);
5845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5846
5847 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005848 clearViewports();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005849 prepareDisplay(DISPLAY_ORIENTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005850 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005851 processSync(mapper);
5852
5853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5854 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5855 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5856
5857 processUp(mapper);
5858 processSync(mapper);
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5860}
5861
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005862TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5863 addConfigurationProperty("touch.deviceType", "touchScreen");
5864 prepareButtons();
5865 prepareAxes(POSITION);
5866 addConfigurationProperty("touch.orientationAware", "1");
5867 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5868 clearViewports();
5869 prepareDisplay(DISPLAY_ORIENTATION_0);
5870 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5871 NotifyMotionArgs args;
5872
5873 // Orientation 0.
5874 processDown(mapper, toRawX(50), toRawY(75));
5875 processSync(mapper);
5876
5877 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5878 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5879 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5880
5881 processUp(mapper);
5882 processSync(mapper);
5883 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5884}
5885
5886TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5887 addConfigurationProperty("touch.deviceType", "touchScreen");
5888 prepareButtons();
5889 prepareAxes(POSITION);
5890 addConfigurationProperty("touch.orientationAware", "1");
5891 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5892 clearViewports();
5893 prepareDisplay(DISPLAY_ORIENTATION_0);
5894 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5895 NotifyMotionArgs args;
5896
5897 // Orientation 90.
5898 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5899 processSync(mapper);
5900
5901 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5902 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5903 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5904
5905 processUp(mapper);
5906 processSync(mapper);
5907 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5908}
5909
5910TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5911 addConfigurationProperty("touch.deviceType", "touchScreen");
5912 prepareButtons();
5913 prepareAxes(POSITION);
5914 addConfigurationProperty("touch.orientationAware", "1");
5915 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5916 clearViewports();
5917 prepareDisplay(DISPLAY_ORIENTATION_0);
5918 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5919 NotifyMotionArgs args;
5920
5921 // Orientation 180.
5922 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5923 processSync(mapper);
5924
5925 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5926 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5927 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5928
5929 processUp(mapper);
5930 processSync(mapper);
5931 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5932}
5933
5934TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5935 addConfigurationProperty("touch.deviceType", "touchScreen");
5936 prepareButtons();
5937 prepareAxes(POSITION);
5938 addConfigurationProperty("touch.orientationAware", "1");
5939 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5940 clearViewports();
5941 prepareDisplay(DISPLAY_ORIENTATION_0);
5942 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5943 NotifyMotionArgs args;
5944
5945 // Orientation 270.
5946 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5947 processSync(mapper);
5948
5949 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5950 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5951 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5952
5953 processUp(mapper);
5954 processSync(mapper);
5955 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5956}
5957
5958TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5959 addConfigurationProperty("touch.deviceType", "touchScreen");
5960 prepareButtons();
5961 prepareAxes(POSITION);
5962 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5963 // orientation-aware are affected by display rotation.
5964 addConfigurationProperty("touch.orientationAware", "0");
5965 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5966 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5967
5968 NotifyMotionArgs args;
5969
5970 // Orientation 90, Rotation 0.
5971 clearViewports();
5972 prepareDisplay(DISPLAY_ORIENTATION_0);
5973 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5974 processSync(mapper);
5975
5976 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5977 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5978 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5979
5980 processUp(mapper);
5981 processSync(mapper);
5982 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5983
5984 // Orientation 90, Rotation 90.
5985 clearViewports();
5986 prepareDisplay(DISPLAY_ORIENTATION_90);
5987 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5988 processSync(mapper);
5989
5990 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5991 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5992 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5993
5994 processUp(mapper);
5995 processSync(mapper);
5996 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5997
5998 // Orientation 90, Rotation 180.
5999 clearViewports();
6000 prepareDisplay(DISPLAY_ORIENTATION_180);
6001 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6002 processSync(mapper);
6003
6004 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6005 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6006 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6007
6008 processUp(mapper);
6009 processSync(mapper);
6010 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6011
6012 // Orientation 90, Rotation 270.
6013 clearViewports();
6014 prepareDisplay(DISPLAY_ORIENTATION_270);
6015 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
6016 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
6017 processSync(mapper);
6018
6019 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6020 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6021 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6022
6023 processUp(mapper);
6024 processSync(mapper);
6025 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6026}
6027
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006029 addConfigurationProperty("touch.deviceType", "touchScreen");
6030 prepareDisplay(DISPLAY_ORIENTATION_0);
6031 prepareButtons();
6032 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006033 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034
6035 // These calculations are based on the input device calibration documentation.
6036 int32_t rawX = 100;
6037 int32_t rawY = 200;
6038 int32_t rawPressure = 10;
6039 int32_t rawToolMajor = 12;
6040 int32_t rawDistance = 2;
6041 int32_t rawTiltX = 30;
6042 int32_t rawTiltY = 110;
6043
6044 float x = toDisplayX(rawX);
6045 float y = toDisplayY(rawY);
6046 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6047 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6048 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6049 float distance = float(rawDistance);
6050
6051 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6052 float tiltScale = M_PI / 180;
6053 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6054 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6055 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6056 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6057
6058 processDown(mapper, rawX, rawY);
6059 processPressure(mapper, rawPressure);
6060 processToolMajor(mapper, rawToolMajor);
6061 processDistance(mapper, rawDistance);
6062 processTilt(mapper, rawTiltX, rawTiltY);
6063 processSync(mapper);
6064
6065 NotifyMotionArgs args;
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6068 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6069 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6070}
6071
Jason Gerecke489fda82012-09-07 17:19:40 -07006072TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006073 addConfigurationProperty("touch.deviceType", "touchScreen");
6074 prepareDisplay(DISPLAY_ORIENTATION_0);
6075 prepareLocationCalibration();
6076 prepareButtons();
6077 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006078 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006079
6080 int32_t rawX = 100;
6081 int32_t rawY = 200;
6082
6083 float x = toDisplayX(toCookedX(rawX, rawY));
6084 float y = toDisplayY(toCookedY(rawX, rawY));
6085
6086 processDown(mapper, rawX, rawY);
6087 processSync(mapper);
6088
6089 NotifyMotionArgs args;
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6092 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6093}
6094
Michael Wrightd02c5b62014-02-10 15:10:22 -08006095TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096 addConfigurationProperty("touch.deviceType", "touchScreen");
6097 prepareDisplay(DISPLAY_ORIENTATION_0);
6098 prepareButtons();
6099 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006100 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006101
6102 NotifyMotionArgs motionArgs;
6103 NotifyKeyArgs keyArgs;
6104
6105 processDown(mapper, 100, 200);
6106 processSync(mapper);
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6109 ASSERT_EQ(0, motionArgs.buttonState);
6110
6111 // press BTN_LEFT, release BTN_LEFT
6112 processKey(mapper, BTN_LEFT, 1);
6113 processSync(mapper);
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6116 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6117
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6120 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6121
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122 processKey(mapper, BTN_LEFT, 0);
6123 processSync(mapper);
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006127
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006130 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006131
6132 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6133 processKey(mapper, BTN_RIGHT, 1);
6134 processKey(mapper, BTN_MIDDLE, 1);
6135 processSync(mapper);
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6138 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6139 motionArgs.buttonState);
6140
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6143 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6144
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6146 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6147 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6148 motionArgs.buttonState);
6149
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150 processKey(mapper, BTN_RIGHT, 0);
6151 processSync(mapper);
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006153 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006154 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006155
6156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006158 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006159
6160 processKey(mapper, BTN_MIDDLE, 0);
6161 processSync(mapper);
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006163 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006164 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006165
6166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006168 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006169
6170 // press BTN_BACK, release BTN_BACK
6171 processKey(mapper, BTN_BACK, 1);
6172 processSync(mapper);
6173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6174 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6175 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006176
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006179 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6180
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6182 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6183 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006184
6185 processKey(mapper, BTN_BACK, 0);
6186 processSync(mapper);
6187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006189 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006190
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006193 ASSERT_EQ(0, motionArgs.buttonState);
6194
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6196 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6197 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6198
6199 // press BTN_SIDE, release BTN_SIDE
6200 processKey(mapper, BTN_SIDE, 1);
6201 processSync(mapper);
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6203 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6204 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006205
Michael Wrightd02c5b62014-02-10 15:10:22 -08006206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006208 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6209
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6212 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006213
6214 processKey(mapper, BTN_SIDE, 0);
6215 processSync(mapper);
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006218 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006219
6220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006222 ASSERT_EQ(0, motionArgs.buttonState);
6223
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6225 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6226 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6227
6228 // press BTN_FORWARD, release BTN_FORWARD
6229 processKey(mapper, BTN_FORWARD, 1);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6232 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6233 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006234
Michael Wrightd02c5b62014-02-10 15:10:22 -08006235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006237 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6238
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6240 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6241 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006242
6243 processKey(mapper, BTN_FORWARD, 0);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006247 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006248
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006251 ASSERT_EQ(0, motionArgs.buttonState);
6252
Michael Wrightd02c5b62014-02-10 15:10:22 -08006253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6254 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6255 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6256
6257 // press BTN_EXTRA, release BTN_EXTRA
6258 processKey(mapper, BTN_EXTRA, 1);
6259 processSync(mapper);
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6261 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6262 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006263
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006266 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6267
6268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6269 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6270 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006271
6272 processKey(mapper, BTN_EXTRA, 0);
6273 processSync(mapper);
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006276 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006277
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006280 ASSERT_EQ(0, motionArgs.buttonState);
6281
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6283 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6284 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6285
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6287
Michael Wrightd02c5b62014-02-10 15:10:22 -08006288 // press BTN_STYLUS, release BTN_STYLUS
6289 processKey(mapper, BTN_STYLUS, 1);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006293 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6294
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6297 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006298
6299 processKey(mapper, BTN_STYLUS, 0);
6300 processSync(mapper);
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006303 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006304
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006307 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006308
6309 // press BTN_STYLUS2, release BTN_STYLUS2
6310 processKey(mapper, BTN_STYLUS2, 1);
6311 processSync(mapper);
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006314 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6315
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6318 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006319
6320 processKey(mapper, BTN_STYLUS2, 0);
6321 processSync(mapper);
6322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006324 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006325
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006327 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006328 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006329
6330 // release touch
6331 processUp(mapper);
6332 processSync(mapper);
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6334 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6335 ASSERT_EQ(0, motionArgs.buttonState);
6336}
6337
6338TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339 addConfigurationProperty("touch.deviceType", "touchScreen");
6340 prepareDisplay(DISPLAY_ORIENTATION_0);
6341 prepareButtons();
6342 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006343 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006344
6345 NotifyMotionArgs motionArgs;
6346
6347 // default tool type is finger
6348 processDown(mapper, 100, 200);
6349 processSync(mapper);
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6353
6354 // eraser
6355 processKey(mapper, BTN_TOOL_RUBBER, 1);
6356 processSync(mapper);
6357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6360
6361 // stylus
6362 processKey(mapper, BTN_TOOL_RUBBER, 0);
6363 processKey(mapper, BTN_TOOL_PEN, 1);
6364 processSync(mapper);
6365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6368
6369 // brush
6370 processKey(mapper, BTN_TOOL_PEN, 0);
6371 processKey(mapper, BTN_TOOL_BRUSH, 1);
6372 processSync(mapper);
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6376
6377 // pencil
6378 processKey(mapper, BTN_TOOL_BRUSH, 0);
6379 processKey(mapper, BTN_TOOL_PENCIL, 1);
6380 processSync(mapper);
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6384
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006385 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 processKey(mapper, BTN_TOOL_PENCIL, 0);
6387 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6392
6393 // mouse
6394 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6395 processKey(mapper, BTN_TOOL_MOUSE, 1);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6400
6401 // lens
6402 processKey(mapper, BTN_TOOL_MOUSE, 0);
6403 processKey(mapper, BTN_TOOL_LENS, 1);
6404 processSync(mapper);
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6408
6409 // double-tap
6410 processKey(mapper, BTN_TOOL_LENS, 0);
6411 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6412 processSync(mapper);
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6416
6417 // triple-tap
6418 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6419 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6424
6425 // quad-tap
6426 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6427 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6432
6433 // finger
6434 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6435 processKey(mapper, BTN_TOOL_FINGER, 1);
6436 processSync(mapper);
6437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6440
6441 // stylus trumps finger
6442 processKey(mapper, BTN_TOOL_PEN, 1);
6443 processSync(mapper);
6444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6447
6448 // eraser trumps stylus
6449 processKey(mapper, BTN_TOOL_RUBBER, 1);
6450 processSync(mapper);
6451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6454
6455 // mouse trumps eraser
6456 processKey(mapper, BTN_TOOL_MOUSE, 1);
6457 processSync(mapper);
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6461
6462 // back to default tool type
6463 processKey(mapper, BTN_TOOL_MOUSE, 0);
6464 processKey(mapper, BTN_TOOL_RUBBER, 0);
6465 processKey(mapper, BTN_TOOL_PEN, 0);
6466 processKey(mapper, BTN_TOOL_FINGER, 0);
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6470 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6471}
6472
6473TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006474 addConfigurationProperty("touch.deviceType", "touchScreen");
6475 prepareDisplay(DISPLAY_ORIENTATION_0);
6476 prepareButtons();
6477 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006478 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006479 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006480
6481 NotifyMotionArgs motionArgs;
6482
6483 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6484 processKey(mapper, BTN_TOOL_FINGER, 1);
6485 processMove(mapper, 100, 200);
6486 processSync(mapper);
6487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6490 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6491
6492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6493 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6495 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6496
6497 // move a little
6498 processMove(mapper, 150, 250);
6499 processSync(mapper);
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6501 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6503 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6504
6505 // down when BTN_TOUCH is pressed, pressure defaults to 1
6506 processKey(mapper, BTN_TOUCH, 1);
6507 processSync(mapper);
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6509 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6511 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6512
6513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6514 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6516 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6517
6518 // up when BTN_TOUCH is released, hover restored
6519 processKey(mapper, BTN_TOUCH, 0);
6520 processSync(mapper);
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6522 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6524 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6525
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6529 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6530
6531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6532 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6534 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6535
6536 // exit hover when pointer goes away
6537 processKey(mapper, BTN_TOOL_FINGER, 0);
6538 processSync(mapper);
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6540 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6542 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6543}
6544
6545TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006546 addConfigurationProperty("touch.deviceType", "touchScreen");
6547 prepareDisplay(DISPLAY_ORIENTATION_0);
6548 prepareButtons();
6549 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006550 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006551
6552 NotifyMotionArgs motionArgs;
6553
6554 // initially hovering because pressure is 0
6555 processDown(mapper, 100, 200);
6556 processPressure(mapper, 0);
6557 processSync(mapper);
6558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6561 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6562
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6566 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6567
6568 // move a little
6569 processMove(mapper, 150, 250);
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6574 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6575
6576 // down when pressure is non-zero
6577 processPressure(mapper, RAW_PRESSURE_MAX);
6578 processSync(mapper);
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6580 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6582 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6583
6584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6585 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6587 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6588
6589 // up when pressure becomes 0, hover restored
6590 processPressure(mapper, 0);
6591 processSync(mapper);
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6593 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6595 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6596
6597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6600 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6601
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6605 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6606
6607 // exit hover when pointer goes away
6608 processUp(mapper);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6613 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6614}
6615
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006616TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6617 addConfigurationProperty("touch.deviceType", "touchScreen");
6618 prepareDisplay(DISPLAY_ORIENTATION_0);
6619 prepareButtons();
6620 prepareAxes(POSITION | PRESSURE);
6621 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6622
6623 // Touch down.
6624 processDown(mapper, 100, 200);
6625 processPressure(mapper, 1);
6626 processSync(mapper);
6627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6628 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6629
6630 // Reset the mapper. This should cancel the ongoing gesture.
6631 resetMapper(mapper, ARBITRARY_TIME);
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6633 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6634
6635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6636}
6637
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006638TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6639 addConfigurationProperty("touch.deviceType", "touchScreen");
6640 prepareDisplay(DISPLAY_ORIENTATION_0);
6641 prepareButtons();
6642 prepareAxes(POSITION | PRESSURE);
6643 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6644
6645 // Set the initial state for the touch pointer.
6646 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6647 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6648 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6649 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6650
6651 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006652 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6653 // does not generate any events.
6654 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006655
6656 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6657 // the recreated touch state to generate a down event.
6658 processSync(mapper);
6659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6660 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6661
6662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6663}
6664
lilinnan687e58f2022-07-19 16:00:50 +08006665TEST_F(SingleTouchInputMapperTest,
6666 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6667 addConfigurationProperty("touch.deviceType", "touchScreen");
6668 prepareDisplay(DISPLAY_ORIENTATION_0);
6669 prepareButtons();
6670 prepareAxes(POSITION);
6671 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6672 NotifyMotionArgs motionArgs;
6673
6674 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006675 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006676 processSync(mapper);
6677
6678 // We should receive a down event
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6680 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6681
6682 // Change display id
6683 clearViewports();
6684 prepareSecondaryDisplay(ViewportType::INTERNAL);
6685
6686 // We should receive a cancel event
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6688 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6689 // Then receive reset called
6690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6691}
6692
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006693TEST_F(SingleTouchInputMapperTest,
6694 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6695 addConfigurationProperty("touch.deviceType", "touchScreen");
6696 prepareDisplay(DISPLAY_ORIENTATION_0);
6697 prepareButtons();
6698 prepareAxes(POSITION);
6699 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6701 NotifyMotionArgs motionArgs;
6702
6703 // Start a new gesture.
6704 processDown(mapper, 100, 200);
6705 processSync(mapper);
6706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6708
6709 // Make the viewport inactive. This will put the device in disabled mode.
6710 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6711 viewport->isActive = false;
6712 mFakePolicy->updateViewport(*viewport);
6713 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6714
6715 // We should receive a cancel event for the ongoing gesture.
6716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6717 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6718 // Then we should be notified that the device was reset.
6719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6720
6721 // No events are generated while the viewport is inactive.
6722 processMove(mapper, 101, 201);
6723 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006724 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006725 processSync(mapper);
6726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6727
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006728 // Start a new gesture while the viewport is still inactive.
6729 processDown(mapper, 300, 400);
6730 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6731 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6732 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6733 processSync(mapper);
6734
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006735 // Make the viewport active again. The device should resume processing events.
6736 viewport->isActive = true;
6737 mFakePolicy->updateViewport(*viewport);
6738 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6739
6740 // The device is reset because it changes back to direct mode, without generating any events.
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6743
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006744 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006745 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6747 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006748
6749 // No more events.
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6752}
6753
Prabir Pradhan211ba622022-10-31 21:09:21 +00006754TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6755 addConfigurationProperty("touch.deviceType", "touchScreen");
6756 prepareDisplay(DISPLAY_ORIENTATION_0);
6757 prepareButtons();
6758 prepareAxes(POSITION);
6759 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6761
6762 // Press a stylus button.
6763 processKey(mapper, BTN_STYLUS, 1);
6764 processSync(mapper);
6765
6766 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6767 processDown(mapper, 100, 200);
6768 processSync(mapper);
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6770 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6771 WithCoords(toDisplayX(100), toDisplayY(200)),
6772 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6774 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6775 WithCoords(toDisplayX(100), toDisplayY(200)),
6776 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6777
6778 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6779 // the button has not actually been released, since there will be no pointers through which the
6780 // button state can be reported. The event is generated at the location of the pointer before
6781 // it went up.
6782 processUp(mapper);
6783 processSync(mapper);
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6785 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6786 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6788 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6789 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6790}
6791
Prabir Pradhan5632d622021-09-06 07:57:20 -07006792// --- TouchDisplayProjectionTest ---
6793
6794class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6795public:
6796 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6797 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6798 // rotated equivalent of the given un-rotated physical display bounds.
6799 void configurePhysicalDisplay(int32_t orientation, Rect naturalPhysicalDisplay) {
6800 uint32_t inverseRotationFlags;
6801 auto width = DISPLAY_WIDTH;
6802 auto height = DISPLAY_HEIGHT;
6803 switch (orientation) {
6804 case DISPLAY_ORIENTATION_90:
6805 inverseRotationFlags = ui::Transform::ROT_270;
6806 std::swap(width, height);
6807 break;
6808 case DISPLAY_ORIENTATION_180:
6809 inverseRotationFlags = ui::Transform::ROT_180;
6810 break;
6811 case DISPLAY_ORIENTATION_270:
6812 inverseRotationFlags = ui::Transform::ROT_90;
6813 std::swap(width, height);
6814 break;
6815 case DISPLAY_ORIENTATION_0:
6816 inverseRotationFlags = ui::Transform::ROT_0;
6817 break;
6818 default:
6819 FAIL() << "Invalid orientation: " << orientation;
6820 }
6821
6822 const ui::Transform rotation(inverseRotationFlags, width, height);
6823 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6824
6825 std::optional<DisplayViewport> internalViewport =
6826 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6827 DisplayViewport& v = *internalViewport;
6828 v.displayId = DISPLAY_ID;
6829 v.orientation = orientation;
6830
6831 v.logicalLeft = 0;
6832 v.logicalTop = 0;
6833 v.logicalRight = 100;
6834 v.logicalBottom = 100;
6835
6836 v.physicalLeft = rotatedPhysicalDisplay.left;
6837 v.physicalTop = rotatedPhysicalDisplay.top;
6838 v.physicalRight = rotatedPhysicalDisplay.right;
6839 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6840
6841 v.deviceWidth = width;
6842 v.deviceHeight = height;
6843
6844 v.isActive = true;
6845 v.uniqueId = UNIQUE_ID;
6846 v.type = ViewportType::INTERNAL;
6847 mFakePolicy->updateViewport(v);
6848 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6849 }
6850
6851 void assertReceivedMove(const Point& point) {
6852 NotifyMotionArgs motionArgs;
6853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6855 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6857 1, 0, 0, 0, 0, 0, 0, 0));
6858 }
6859};
6860
6861TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6862 addConfigurationProperty("touch.deviceType", "touchScreen");
6863 prepareDisplay(DISPLAY_ORIENTATION_0);
6864
6865 prepareButtons();
6866 prepareAxes(POSITION);
6867 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6868
6869 NotifyMotionArgs motionArgs;
6870
6871 // Configure the DisplayViewport such that the logical display maps to a subsection of
6872 // the display panel called the physical display. Here, the physical display is bounded by the
6873 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6874 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6875 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6876 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6877
6878 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6879 DISPLAY_ORIENTATION_270}) {
6880 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6881
6882 // Touches outside the physical display should be ignored, and should not generate any
6883 // events. Ensure touches at the following points that lie outside of the physical display
6884 // area do not generate any events.
6885 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6886 processDown(mapper, toRawX(point.x), toRawY(point.y));
6887 processSync(mapper);
6888 processUp(mapper);
6889 processSync(mapper);
6890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6891 << "Unexpected event generated for touch outside physical display at point: "
6892 << point.x << ", " << point.y;
6893 }
6894 }
6895}
6896
6897TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6898 addConfigurationProperty("touch.deviceType", "touchScreen");
6899 prepareDisplay(DISPLAY_ORIENTATION_0);
6900
6901 prepareButtons();
6902 prepareAxes(POSITION);
6903 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6904
6905 NotifyMotionArgs motionArgs;
6906
6907 // Configure the DisplayViewport such that the logical display maps to a subsection of
6908 // the display panel called the physical display. Here, the physical display is bounded by the
6909 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6910 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6911
6912 for (auto orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90, DISPLAY_ORIENTATION_180,
6913 DISPLAY_ORIENTATION_270}) {
6914 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6915
6916 // Touches that start outside the physical display should be ignored until it enters the
6917 // physical display bounds, at which point it should generate a down event. Start a touch at
6918 // the point (5, 100), which is outside the physical display bounds.
6919 static const Point kOutsidePoint{5, 100};
6920 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6921 processSync(mapper);
6922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6923
6924 // Move the touch into the physical display area. This should generate a pointer down.
6925 processMove(mapper, toRawX(11), toRawY(21));
6926 processSync(mapper);
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6928 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6929 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6930 ASSERT_NO_FATAL_FAILURE(
6931 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6932
6933 // Move the touch inside the physical display area. This should generate a pointer move.
6934 processMove(mapper, toRawX(69), toRawY(159));
6935 processSync(mapper);
6936 assertReceivedMove({69, 159});
6937
6938 // Move outside the physical display area. Since the pointer is already down, this should
6939 // now continue generating events.
6940 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6941 processSync(mapper);
6942 assertReceivedMove(kOutsidePoint);
6943
6944 // Release. This should generate a pointer up.
6945 processUp(mapper);
6946 processSync(mapper);
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6948 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6950 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6951
6952 // Ensure no more events were generated.
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6955 }
6956}
6957
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006958// --- ExternalStylusFusionTest ---
6959
6960class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6961public:
6962 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6963 addConfigurationProperty("touch.deviceType", "touchScreen");
6964 prepareDisplay(DISPLAY_ORIENTATION_0);
6965 prepareButtons();
6966 prepareAxes(POSITION);
6967 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6968
6969 mStylusState.when = ARBITRARY_TIME;
6970 mStylusState.pressure = 0.f;
6971 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6972 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
6973 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
6974 processExternalStylusState(mapper);
6975 return mapper;
6976 }
6977
6978 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6979 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6980 for (const NotifyArgs& args : generatedArgs) {
6981 mFakeListener->notify(args);
6982 }
6983 // Loop the reader to flush the input listener queue.
6984 mReader->loopOnce();
6985 return generatedArgs;
6986 }
6987
6988protected:
6989 StylusState mStylusState{};
6990 static constexpr uint32_t EXPECTED_SOURCE =
6991 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
6992
6993 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6994 auto toolTypeSource =
6995 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6996
6997 // The first pointer is withheld.
6998 processDown(mapper, 100, 200);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7001 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7002 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7003
7004 // The external stylus reports pressure. The withheld finger pointer is released as a
7005 // stylus.
7006 mStylusState.pressure = 1.f;
7007 processExternalStylusState(mapper);
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7009 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7010 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7011
7012 // Subsequent pointer events are not withheld.
7013 processMove(mapper, 101, 201);
7014 processSync(mapper);
7015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7016 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7017
7018 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7020 }
7021
7022 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7023 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7024
7025 // Releasing the touch pointer ends the gesture.
7026 processUp(mapper);
7027 processSync(mapper);
7028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7029 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7030 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7031
7032 mStylusState.pressure = 0.f;
7033 processExternalStylusState(mapper);
7034 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7036 }
7037
7038 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7039 auto toolTypeSource =
7040 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7041
7042 // The first pointer is withheld when an external stylus is connected,
7043 // and a timeout is requested.
7044 processDown(mapper, 100, 200);
7045 processSync(mapper);
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7047 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7048 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7049
7050 // If the timeout expires early, it is requested again.
7051 handleTimeout(mapper, ARBITRARY_TIME + 1);
7052 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7053 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7054
7055 // When the timeout expires, the withheld touch is released as a finger pointer.
7056 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7058 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7059
7060 // Subsequent pointer events are not withheld.
7061 processMove(mapper, 101, 201);
7062 processSync(mapper);
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7064 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7065 processUp(mapper);
7066 processSync(mapper);
7067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7068 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7069
7070 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7072 }
7073
7074private:
7075 InputDeviceInfo mExternalStylusDeviceInfo{};
7076};
7077
7078TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7079 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7080 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7081}
7082
7083TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7084 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7085 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7086}
7087
7088TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7089 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7090 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7091}
7092
7093// Test a successful stylus fusion gesture where the pressure is reported by the external
7094// before the touch is reported by the touchscreen.
7095TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7096 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7097 auto toolTypeSource =
7098 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7099
7100 // The external stylus reports pressure first. It is ignored for now.
7101 mStylusState.pressure = 1.f;
7102 processExternalStylusState(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7104 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7105
7106 // When the touch goes down afterwards, it is reported as a stylus pointer.
7107 processDown(mapper, 100, 200);
7108 processSync(mapper);
7109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7110 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7111 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7112
7113 processMove(mapper, 101, 201);
7114 processSync(mapper);
7115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7116 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7117 processUp(mapper);
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7120 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7121
7122 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7124}
7125
7126TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7127 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7128
7129 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7130 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7131
7132 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7133 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7134 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7135 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7136}
7137
7138TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7139 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7140 auto toolTypeSource =
7141 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7142
7143 mStylusState.pressure = 0.8f;
7144 processExternalStylusState(mapper);
7145 processDown(mapper, 100, 200);
7146 processSync(mapper);
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7148 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7149 WithPressure(0.8f))));
7150 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7151
7152 // The external stylus reports a pressure change. We wait for some time for a touch event.
7153 mStylusState.pressure = 0.6f;
7154 processExternalStylusState(mapper);
7155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7156 ASSERT_NO_FATAL_FAILURE(
7157 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7158
7159 // If a touch is reported within the timeout, it reports the updated pressure.
7160 processMove(mapper, 101, 201);
7161 processSync(mapper);
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7163 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7164 WithPressure(0.6f))));
7165 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7166
7167 // There is another pressure change.
7168 mStylusState.pressure = 0.5f;
7169 processExternalStylusState(mapper);
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7171 ASSERT_NO_FATAL_FAILURE(
7172 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7173
7174 // If a touch is not reported within the timeout, a move event is generated to report
7175 // the new pressure.
7176 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7178 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7179 WithPressure(0.5f))));
7180
7181 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7182 // repeated indefinitely.
7183 mStylusState.pressure = 0.0f;
7184 processExternalStylusState(mapper);
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7186 ASSERT_NO_FATAL_FAILURE(
7187 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7188 processMove(mapper, 102, 202);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7191 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7192 WithPressure(0.5f))));
7193 processMove(mapper, 103, 203);
7194 processSync(mapper);
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7196 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7197 WithPressure(0.5f))));
7198
7199 processUp(mapper);
7200 processSync(mapper);
7201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7202 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7203 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7204
7205 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7207}
7208
7209TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7210 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7211 auto source = WithSource(EXPECTED_SOURCE);
7212
7213 mStylusState.pressure = 1.f;
7214 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7215 processExternalStylusState(mapper);
7216 processDown(mapper, 100, 200);
7217 processSync(mapper);
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7219 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7220 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7221 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7222
7223 // The external stylus reports a tool change. We wait for some time for a touch event.
7224 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7225 processExternalStylusState(mapper);
7226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7227 ASSERT_NO_FATAL_FAILURE(
7228 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7229
7230 // If a touch is reported within the timeout, it reports the updated pressure.
7231 processMove(mapper, 101, 201);
7232 processSync(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7234 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7235 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7236 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7237
7238 // There is another tool type change.
7239 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7240 processExternalStylusState(mapper);
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7242 ASSERT_NO_FATAL_FAILURE(
7243 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7244
7245 // If a touch is not reported within the timeout, a move event is generated to report
7246 // the new tool type.
7247 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7249 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7250 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7251
7252 processUp(mapper);
7253 processSync(mapper);
7254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7255 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7256 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7257
7258 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7260}
7261
7262TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7263 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7264 auto toolTypeSource =
7265 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7266
7267 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7268
7269 // The external stylus reports a button change. We wait for some time for a touch event.
7270 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7271 processExternalStylusState(mapper);
7272 ASSERT_NO_FATAL_FAILURE(
7273 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7274
7275 // If a touch is reported within the timeout, it reports the updated button state.
7276 processMove(mapper, 101, 201);
7277 processSync(mapper);
7278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7279 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7280 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7282 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7283 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7284 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7285
7286 // The button is now released.
7287 mStylusState.buttons = 0;
7288 processExternalStylusState(mapper);
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7290 ASSERT_NO_FATAL_FAILURE(
7291 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7292
7293 // If a touch is not reported within the timeout, a move event is generated to report
7294 // the new button state.
7295 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7297 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7298 WithButtonState(0))));
7299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007300 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7301 WithButtonState(0))));
7302
7303 processUp(mapper);
7304 processSync(mapper);
7305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007306 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7307
7308 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7310}
7311
Michael Wrightd02c5b62014-02-10 15:10:22 -08007312// --- MultiTouchInputMapperTest ---
7313
7314class MultiTouchInputMapperTest : public TouchInputMapperTest {
7315protected:
7316 void prepareAxes(int axes);
7317
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007318 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7319 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7320 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7321 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7322 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7323 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7324 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7325 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7326 void processId(MultiTouchInputMapper& mapper, int32_t id);
7327 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7328 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7329 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007330 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007331 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007332 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7333 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007334};
7335
7336void MultiTouchInputMapperTest::prepareAxes(int axes) {
7337 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007338 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7339 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007340 }
7341 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007342 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7343 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007344 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007345 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7346 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007347 }
7348 }
7349 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007350 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7351 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007352 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007353 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007354 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007355 }
7356 }
7357 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007358 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7359 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360 }
7361 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007362 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7363 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007364 }
7365 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007366 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7367 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007368 }
7369 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007370 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7371 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007372 }
7373 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007374 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7375 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007376 }
7377 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007378 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007379 }
7380}
7381
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007382void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7383 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007384 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007386}
7387
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007388void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7389 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007391}
7392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007393void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7394 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396}
7397
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007398void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007399 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007400}
7401
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007402void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007404}
7405
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007406void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7407 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007409}
7410
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007411void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007413}
7414
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007415void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007417}
7418
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007419void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007420 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007421}
7422
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007423void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007425}
7426
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007427void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007429}
7430
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007431void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7432 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007434}
7435
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007436void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7437 int32_t value) {
7438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7440}
7441
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007442void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007444}
7445
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007446void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7447 nsecs_t readTime) {
7448 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007449}
7450
Michael Wrightd02c5b62014-02-10 15:10:22 -08007451TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007452 addConfigurationProperty("touch.deviceType", "touchScreen");
7453 prepareDisplay(DISPLAY_ORIENTATION_0);
7454 prepareAxes(POSITION);
7455 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007456 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007457
arthurhungdcef2dc2020-08-11 14:47:50 +08007458 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007459
7460 NotifyMotionArgs motionArgs;
7461
7462 // Two fingers down at once.
7463 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7464 processPosition(mapper, x1, y1);
7465 processMTSync(mapper);
7466 processPosition(mapper, x2, y2);
7467 processMTSync(mapper);
7468 processSync(mapper);
7469
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7471 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7472 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7473 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7474 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7476 ASSERT_EQ(0, motionArgs.flags);
7477 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7478 ASSERT_EQ(0, motionArgs.buttonState);
7479 ASSERT_EQ(0, motionArgs.edgeFlags);
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 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7486 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7487 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7488
7489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7490 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7491 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7492 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7493 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007494 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007495 ASSERT_EQ(0, motionArgs.flags);
7496 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7497 ASSERT_EQ(0, motionArgs.buttonState);
7498 ASSERT_EQ(0, motionArgs.edgeFlags);
7499 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7500 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7501 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7502 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7504 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7505 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7507 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7508 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7509 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7510 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7511
7512 // Move.
7513 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7514 processPosition(mapper, x1, y1);
7515 processMTSync(mapper);
7516 processPosition(mapper, x2, y2);
7517 processMTSync(mapper);
7518 processSync(mapper);
7519
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7521 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7522 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7523 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7524 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7525 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7526 ASSERT_EQ(0, motionArgs.flags);
7527 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7528 ASSERT_EQ(0, motionArgs.buttonState);
7529 ASSERT_EQ(0, motionArgs.edgeFlags);
7530 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7531 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7533 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7534 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7536 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7538 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7539 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7540 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7541 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7542
7543 // First finger up.
7544 x2 += 15; y2 -= 20;
7545 processPosition(mapper, x2, y2);
7546 processMTSync(mapper);
7547 processSync(mapper);
7548
7549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7550 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7551 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7552 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7553 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007554 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007555 ASSERT_EQ(0, motionArgs.flags);
7556 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7557 ASSERT_EQ(0, motionArgs.buttonState);
7558 ASSERT_EQ(0, motionArgs.edgeFlags);
7559 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7560 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7562 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7565 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7567 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7568 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7569 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7570 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7571
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7573 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7574 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7575 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7576 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7578 ASSERT_EQ(0, motionArgs.flags);
7579 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7580 ASSERT_EQ(0, motionArgs.buttonState);
7581 ASSERT_EQ(0, motionArgs.edgeFlags);
7582 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7583 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7586 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7587 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7588 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7589 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7590
7591 // Move.
7592 x2 += 20; y2 -= 25;
7593 processPosition(mapper, x2, y2);
7594 processMTSync(mapper);
7595 processSync(mapper);
7596
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7598 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7599 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7600 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7601 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7603 ASSERT_EQ(0, motionArgs.flags);
7604 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7605 ASSERT_EQ(0, motionArgs.buttonState);
7606 ASSERT_EQ(0, motionArgs.edgeFlags);
7607 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7608 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7609 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7611 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7612 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7613 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7614 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7615
7616 // New finger down.
7617 int32_t x3 = 700, y3 = 300;
7618 processPosition(mapper, x2, y2);
7619 processMTSync(mapper);
7620 processPosition(mapper, x3, y3);
7621 processMTSync(mapper);
7622 processSync(mapper);
7623
7624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7625 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7626 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7627 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7628 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007629 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007630 ASSERT_EQ(0, motionArgs.flags);
7631 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7632 ASSERT_EQ(0, motionArgs.buttonState);
7633 ASSERT_EQ(0, motionArgs.edgeFlags);
7634 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7635 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7637 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7638 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7640 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7642 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7643 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7644 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7645 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7646
7647 // Second finger up.
7648 x3 += 30; y3 -= 20;
7649 processPosition(mapper, x3, y3);
7650 processMTSync(mapper);
7651 processSync(mapper);
7652
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7655 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7656 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7657 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007658 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007659 ASSERT_EQ(0, motionArgs.flags);
7660 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7661 ASSERT_EQ(0, motionArgs.buttonState);
7662 ASSERT_EQ(0, motionArgs.edgeFlags);
7663 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7664 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7666 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7667 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7668 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7669 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7671 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7672 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7673 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7674 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7675
7676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7677 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7678 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7679 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7680 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7681 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7682 ASSERT_EQ(0, motionArgs.flags);
7683 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7684 ASSERT_EQ(0, motionArgs.buttonState);
7685 ASSERT_EQ(0, motionArgs.edgeFlags);
7686 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7687 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7690 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7691 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7692 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7693 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7694
7695 // Last finger up.
7696 processMTSync(mapper);
7697 processSync(mapper);
7698
7699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7700 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7701 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7702 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7703 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7704 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7705 ASSERT_EQ(0, motionArgs.flags);
7706 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7707 ASSERT_EQ(0, motionArgs.buttonState);
7708 ASSERT_EQ(0, motionArgs.edgeFlags);
7709 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7710 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7711 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7713 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7714 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7715 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7716 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7717
7718 // Should not have sent any more keys or motions.
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7721}
7722
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007723TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7724 addConfigurationProperty("touch.deviceType", "touchScreen");
7725 prepareDisplay(DISPLAY_ORIENTATION_0);
7726
7727 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7728 /*fuzz*/ 0, /*resolution*/ 10);
7729 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7730 /*fuzz*/ 0, /*resolution*/ 11);
7731 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7732 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7733 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7734 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7735 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7736 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7737 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7738 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7739
7740 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7741
7742 // X and Y axes
7743 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7744 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7745 // Touch major and minor
7746 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7747 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7748 // Tool major and minor
7749 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7750 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7751}
7752
7753TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7754 addConfigurationProperty("touch.deviceType", "touchScreen");
7755 prepareDisplay(DISPLAY_ORIENTATION_0);
7756
7757 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7758 /*fuzz*/ 0, /*resolution*/ 10);
7759 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7760 /*fuzz*/ 0, /*resolution*/ 11);
7761
7762 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7763
7764 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7765
7766 // Touch major and minor
7767 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7768 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7769 // Tool major and minor
7770 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7771 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7772}
7773
Michael Wrightd02c5b62014-02-10 15:10:22 -08007774TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007775 addConfigurationProperty("touch.deviceType", "touchScreen");
7776 prepareDisplay(DISPLAY_ORIENTATION_0);
7777 prepareAxes(POSITION | ID);
7778 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007779 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007780
arthurhungdcef2dc2020-08-11 14:47:50 +08007781 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007782
7783 NotifyMotionArgs motionArgs;
7784
7785 // Two fingers down at once.
7786 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7787 processPosition(mapper, x1, y1);
7788 processId(mapper, 1);
7789 processMTSync(mapper);
7790 processPosition(mapper, x2, y2);
7791 processId(mapper, 2);
7792 processMTSync(mapper);
7793 processSync(mapper);
7794
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7796 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7797 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7798 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7801 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7802
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007804 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007805 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7806 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7808 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7811 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7813 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7814
7815 // Move.
7816 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7817 processPosition(mapper, x1, y1);
7818 processId(mapper, 1);
7819 processMTSync(mapper);
7820 processPosition(mapper, x2, y2);
7821 processId(mapper, 2);
7822 processMTSync(mapper);
7823 processSync(mapper);
7824
7825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7827 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7828 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7829 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7830 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7833 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7835 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7836
7837 // First finger up.
7838 x2 += 15; y2 -= 20;
7839 processPosition(mapper, x2, y2);
7840 processId(mapper, 2);
7841 processMTSync(mapper);
7842 processSync(mapper);
7843
7844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007845 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007846 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7847 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7849 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7850 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7852 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7853 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7854 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7855
7856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7858 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7859 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7860 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7862 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7863
7864 // Move.
7865 x2 += 20; y2 -= 25;
7866 processPosition(mapper, x2, y2);
7867 processId(mapper, 2);
7868 processMTSync(mapper);
7869 processSync(mapper);
7870
7871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7873 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7874 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7877 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7878
7879 // New finger down.
7880 int32_t x3 = 700, y3 = 300;
7881 processPosition(mapper, x2, y2);
7882 processId(mapper, 2);
7883 processMTSync(mapper);
7884 processPosition(mapper, x3, y3);
7885 processId(mapper, 3);
7886 processMTSync(mapper);
7887 processSync(mapper);
7888
7889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007890 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7892 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7893 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7894 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7897 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7899 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7900
7901 // Second finger up.
7902 x3 += 30; y3 -= 20;
7903 processPosition(mapper, x3, y3);
7904 processId(mapper, 3);
7905 processMTSync(mapper);
7906 processSync(mapper);
7907
7908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007909 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007910 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7911 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7913 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7916 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7918 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7919
7920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7922 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7923 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7924 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7926 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7927
7928 // Last finger up.
7929 processMTSync(mapper);
7930 processSync(mapper);
7931
7932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7933 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7934 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7935 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7936 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7938 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7939
7940 // Should not have sent any more keys or motions.
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7943}
7944
7945TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946 addConfigurationProperty("touch.deviceType", "touchScreen");
7947 prepareDisplay(DISPLAY_ORIENTATION_0);
7948 prepareAxes(POSITION | ID | SLOT);
7949 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007950 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951
arthurhungdcef2dc2020-08-11 14:47:50 +08007952 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953
7954 NotifyMotionArgs motionArgs;
7955
7956 // Two fingers down at once.
7957 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7958 processPosition(mapper, x1, y1);
7959 processId(mapper, 1);
7960 processSlot(mapper, 1);
7961 processPosition(mapper, x2, y2);
7962 processId(mapper, 2);
7963 processSync(mapper);
7964
7965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7966 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7967 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7968 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7971 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7972
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007974 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7976 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7977 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7978 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7979 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7981 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7983 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7984
7985 // Move.
7986 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7987 processSlot(mapper, 0);
7988 processPosition(mapper, x1, y1);
7989 processSlot(mapper, 1);
7990 processPosition(mapper, x2, y2);
7991 processSync(mapper);
7992
7993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7995 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7996 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7998 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8001 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8003 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8004
8005 // First finger up.
8006 x2 += 15; y2 -= 20;
8007 processSlot(mapper, 0);
8008 processId(mapper, -1);
8009 processSlot(mapper, 1);
8010 processPosition(mapper, x2, y2);
8011 processSync(mapper);
8012
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008014 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8016 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8018 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8021 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8023 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8024
8025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8027 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8028 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8030 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8031 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8032
8033 // Move.
8034 x2 += 20; y2 -= 25;
8035 processPosition(mapper, x2, y2);
8036 processSync(mapper);
8037
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8039 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8040 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8041 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8044 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8045
8046 // New finger down.
8047 int32_t x3 = 700, y3 = 300;
8048 processPosition(mapper, x2, y2);
8049 processSlot(mapper, 0);
8050 processId(mapper, 3);
8051 processPosition(mapper, x3, y3);
8052 processSync(mapper);
8053
8054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008055 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008056 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8057 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8059 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8062 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8064 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8065
8066 // Second finger up.
8067 x3 += 30; y3 -= 20;
8068 processSlot(mapper, 1);
8069 processId(mapper, -1);
8070 processSlot(mapper, 0);
8071 processPosition(mapper, x3, y3);
8072 processSync(mapper);
8073
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008075 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008076 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8079 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8080 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8082 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8084 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8085
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8088 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8089 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8090 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8091 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8092 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8093
8094 // Last finger up.
8095 processId(mapper, -1);
8096 processSync(mapper);
8097
8098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8099 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8100 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8101 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8102 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8104 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8105
8106 // Should not have sent any more keys or motions.
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8109}
8110
8111TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112 addConfigurationProperty("touch.deviceType", "touchScreen");
8113 prepareDisplay(DISPLAY_ORIENTATION_0);
8114 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008115 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008116
8117 // These calculations are based on the input device calibration documentation.
8118 int32_t rawX = 100;
8119 int32_t rawY = 200;
8120 int32_t rawTouchMajor = 7;
8121 int32_t rawTouchMinor = 6;
8122 int32_t rawToolMajor = 9;
8123 int32_t rawToolMinor = 8;
8124 int32_t rawPressure = 11;
8125 int32_t rawDistance = 0;
8126 int32_t rawOrientation = 3;
8127 int32_t id = 5;
8128
8129 float x = toDisplayX(rawX);
8130 float y = toDisplayY(rawY);
8131 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8132 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8133 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8134 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8135 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8136 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8137 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8138 float distance = float(rawDistance);
8139
8140 processPosition(mapper, rawX, rawY);
8141 processTouchMajor(mapper, rawTouchMajor);
8142 processTouchMinor(mapper, rawTouchMinor);
8143 processToolMajor(mapper, rawToolMajor);
8144 processToolMinor(mapper, rawToolMinor);
8145 processPressure(mapper, rawPressure);
8146 processOrientation(mapper, rawOrientation);
8147 processDistance(mapper, rawDistance);
8148 processId(mapper, id);
8149 processMTSync(mapper);
8150 processSync(mapper);
8151
8152 NotifyMotionArgs args;
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8154 ASSERT_EQ(0, args.pointerProperties[0].id);
8155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8156 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8157 orientation, distance));
8158}
8159
8160TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 addConfigurationProperty("touch.deviceType", "touchScreen");
8162 prepareDisplay(DISPLAY_ORIENTATION_0);
8163 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8164 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008165 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166
8167 // These calculations are based on the input device calibration documentation.
8168 int32_t rawX = 100;
8169 int32_t rawY = 200;
8170 int32_t rawTouchMajor = 140;
8171 int32_t rawTouchMinor = 120;
8172 int32_t rawToolMajor = 180;
8173 int32_t rawToolMinor = 160;
8174
8175 float x = toDisplayX(rawX);
8176 float y = toDisplayY(rawY);
8177 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8178 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8179 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8180 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8181 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8182
8183 processPosition(mapper, rawX, rawY);
8184 processTouchMajor(mapper, rawTouchMajor);
8185 processTouchMinor(mapper, rawTouchMinor);
8186 processToolMajor(mapper, rawToolMajor);
8187 processToolMinor(mapper, rawToolMinor);
8188 processMTSync(mapper);
8189 processSync(mapper);
8190
8191 NotifyMotionArgs args;
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8194 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8195}
8196
8197TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 addConfigurationProperty("touch.deviceType", "touchScreen");
8199 prepareDisplay(DISPLAY_ORIENTATION_0);
8200 prepareAxes(POSITION | TOUCH | TOOL);
8201 addConfigurationProperty("touch.size.calibration", "diameter");
8202 addConfigurationProperty("touch.size.scale", "10");
8203 addConfigurationProperty("touch.size.bias", "160");
8204 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008205 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008206
8207 // These calculations are based on the input device calibration documentation.
8208 // Note: We only provide a single common touch/tool value because the device is assumed
8209 // not to emit separate values for each pointer (isSummed = 1).
8210 int32_t rawX = 100;
8211 int32_t rawY = 200;
8212 int32_t rawX2 = 150;
8213 int32_t rawY2 = 250;
8214 int32_t rawTouchMajor = 5;
8215 int32_t rawToolMajor = 8;
8216
8217 float x = toDisplayX(rawX);
8218 float y = toDisplayY(rawY);
8219 float x2 = toDisplayX(rawX2);
8220 float y2 = toDisplayY(rawY2);
8221 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8222 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8223 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8224
8225 processPosition(mapper, rawX, rawY);
8226 processTouchMajor(mapper, rawTouchMajor);
8227 processToolMajor(mapper, rawToolMajor);
8228 processMTSync(mapper);
8229 processPosition(mapper, rawX2, rawY2);
8230 processTouchMajor(mapper, rawTouchMajor);
8231 processToolMajor(mapper, rawToolMajor);
8232 processMTSync(mapper);
8233 processSync(mapper);
8234
8235 NotifyMotionArgs args;
8236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8237 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8238
8239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008240 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241 ASSERT_EQ(size_t(2), args.pointerCount);
8242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8243 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8245 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8246}
8247
8248TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 addConfigurationProperty("touch.deviceType", "touchScreen");
8250 prepareDisplay(DISPLAY_ORIENTATION_0);
8251 prepareAxes(POSITION | TOUCH | TOOL);
8252 addConfigurationProperty("touch.size.calibration", "area");
8253 addConfigurationProperty("touch.size.scale", "43");
8254 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008255 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008256
8257 // These calculations are based on the input device calibration documentation.
8258 int32_t rawX = 100;
8259 int32_t rawY = 200;
8260 int32_t rawTouchMajor = 5;
8261 int32_t rawToolMajor = 8;
8262
8263 float x = toDisplayX(rawX);
8264 float y = toDisplayY(rawY);
8265 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8266 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8267 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8268
8269 processPosition(mapper, rawX, rawY);
8270 processTouchMajor(mapper, rawTouchMajor);
8271 processToolMajor(mapper, rawToolMajor);
8272 processMTSync(mapper);
8273 processSync(mapper);
8274
8275 NotifyMotionArgs args;
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8278 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8279}
8280
8281TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 addConfigurationProperty("touch.deviceType", "touchScreen");
8283 prepareDisplay(DISPLAY_ORIENTATION_0);
8284 prepareAxes(POSITION | PRESSURE);
8285 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8286 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008287 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008288
Michael Wrightaa449c92017-12-13 21:21:43 +00008289 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008290 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008291 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8292 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8293 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8294
Michael Wrightd02c5b62014-02-10 15:10:22 -08008295 // These calculations are based on the input device calibration documentation.
8296 int32_t rawX = 100;
8297 int32_t rawY = 200;
8298 int32_t rawPressure = 60;
8299
8300 float x = toDisplayX(rawX);
8301 float y = toDisplayY(rawY);
8302 float pressure = float(rawPressure) * 0.01f;
8303
8304 processPosition(mapper, rawX, rawY);
8305 processPressure(mapper, rawPressure);
8306 processMTSync(mapper);
8307 processSync(mapper);
8308
8309 NotifyMotionArgs args;
8310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8312 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8313}
8314
8315TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316 addConfigurationProperty("touch.deviceType", "touchScreen");
8317 prepareDisplay(DISPLAY_ORIENTATION_0);
8318 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008319 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008320
8321 NotifyMotionArgs motionArgs;
8322 NotifyKeyArgs keyArgs;
8323
8324 processId(mapper, 1);
8325 processPosition(mapper, 100, 200);
8326 processSync(mapper);
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8328 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8329 ASSERT_EQ(0, motionArgs.buttonState);
8330
8331 // press BTN_LEFT, release BTN_LEFT
8332 processKey(mapper, BTN_LEFT, 1);
8333 processSync(mapper);
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8335 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8336 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8337
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8340 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8341
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342 processKey(mapper, BTN_LEFT, 0);
8343 processSync(mapper);
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008345 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008346 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008347
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008350 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008351
8352 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8353 processKey(mapper, BTN_RIGHT, 1);
8354 processKey(mapper, BTN_MIDDLE, 1);
8355 processSync(mapper);
8356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8357 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8358 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8359 motionArgs.buttonState);
8360
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8363 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8364
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8367 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8368 motionArgs.buttonState);
8369
Michael Wrightd02c5b62014-02-10 15:10:22 -08008370 processKey(mapper, BTN_RIGHT, 0);
8371 processSync(mapper);
8372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008373 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008374 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008375
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008378 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379
8380 processKey(mapper, BTN_MIDDLE, 0);
8381 processSync(mapper);
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008383 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008384 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008385
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008388 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389
8390 // press BTN_BACK, release BTN_BACK
8391 processKey(mapper, BTN_BACK, 1);
8392 processSync(mapper);
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8394 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8395 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008396
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008399 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8400
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8403 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404
8405 processKey(mapper, BTN_BACK, 0);
8406 processSync(mapper);
8407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008409 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008410
8411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008413 ASSERT_EQ(0, motionArgs.buttonState);
8414
Michael Wrightd02c5b62014-02-10 15:10:22 -08008415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8416 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8417 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8418
8419 // press BTN_SIDE, release BTN_SIDE
8420 processKey(mapper, BTN_SIDE, 1);
8421 processSync(mapper);
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8423 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8424 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008425
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008428 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8429
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8432 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433
8434 processKey(mapper, BTN_SIDE, 0);
8435 processSync(mapper);
8436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008438 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008439
8440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008442 ASSERT_EQ(0, motionArgs.buttonState);
8443
Michael Wrightd02c5b62014-02-10 15:10:22 -08008444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8445 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8446 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8447
8448 // press BTN_FORWARD, release BTN_FORWARD
8449 processKey(mapper, BTN_FORWARD, 1);
8450 processSync(mapper);
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8452 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8453 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008454
Michael Wrightd02c5b62014-02-10 15:10:22 -08008455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008457 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8458
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8460 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8461 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462
8463 processKey(mapper, BTN_FORWARD, 0);
8464 processSync(mapper);
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008467 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008468
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008471 ASSERT_EQ(0, motionArgs.buttonState);
8472
Michael Wrightd02c5b62014-02-10 15:10:22 -08008473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8474 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8475 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8476
8477 // press BTN_EXTRA, release BTN_EXTRA
8478 processKey(mapper, BTN_EXTRA, 1);
8479 processSync(mapper);
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8481 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8482 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008483
Michael Wrightd02c5b62014-02-10 15:10:22 -08008484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008486 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8487
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8490 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008491
8492 processKey(mapper, BTN_EXTRA, 0);
8493 processSync(mapper);
8494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008497
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008500 ASSERT_EQ(0, motionArgs.buttonState);
8501
Michael Wrightd02c5b62014-02-10 15:10:22 -08008502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8503 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8504 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8505
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8507
Michael Wrightd02c5b62014-02-10 15:10:22 -08008508 // press BTN_STYLUS, release BTN_STYLUS
8509 processKey(mapper, BTN_STYLUS, 1);
8510 processSync(mapper);
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008513 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8514
8515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8516 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8517 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008518
8519 processKey(mapper, BTN_STYLUS, 0);
8520 processSync(mapper);
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008522 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008523 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008524
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008527 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008528
8529 // press BTN_STYLUS2, release BTN_STYLUS2
8530 processKey(mapper, BTN_STYLUS2, 1);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008534 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8535
8536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8538 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539
8540 processKey(mapper, BTN_STYLUS2, 0);
8541 processSync(mapper);
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008543 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008544 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008545
8546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008547 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008548 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008549
8550 // release touch
8551 processId(mapper, -1);
8552 processSync(mapper);
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8554 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8555 ASSERT_EQ(0, motionArgs.buttonState);
8556}
8557
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008558TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8559 addConfigurationProperty("touch.deviceType", "touchScreen");
8560 prepareDisplay(DISPLAY_ORIENTATION_0);
8561 prepareAxes(POSITION | ID | SLOT);
8562 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8563
8564 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8565 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8566
8567 // Touch down.
8568 processId(mapper, 1);
8569 processPosition(mapper, 100, 200);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8572 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8573
8574 // Press and release button mapped to the primary stylus button.
8575 processKey(mapper, BTN_A, 1);
8576 processSync(mapper);
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8578 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8579 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8581 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8582 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8583
8584 processKey(mapper, BTN_A, 0);
8585 processSync(mapper);
8586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8587 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8589 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8590
8591 // Press and release the HID usage mapped to the secondary stylus button.
8592 processHidUsage(mapper, 0xabcd, 1);
8593 processSync(mapper);
8594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8595 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8596 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8598 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8599 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8600
8601 processHidUsage(mapper, 0xabcd, 0);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8604 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8606 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8607
8608 // Release touch.
8609 processId(mapper, -1);
8610 processSync(mapper);
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8612 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8613}
8614
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008616 addConfigurationProperty("touch.deviceType", "touchScreen");
8617 prepareDisplay(DISPLAY_ORIENTATION_0);
8618 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008619 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008620
8621 NotifyMotionArgs motionArgs;
8622
8623 // default tool type is finger
8624 processId(mapper, 1);
8625 processPosition(mapper, 100, 200);
8626 processSync(mapper);
8627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8628 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8630
8631 // eraser
8632 processKey(mapper, BTN_TOOL_RUBBER, 1);
8633 processSync(mapper);
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8637
8638 // stylus
8639 processKey(mapper, BTN_TOOL_RUBBER, 0);
8640 processKey(mapper, BTN_TOOL_PEN, 1);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8645
8646 // brush
8647 processKey(mapper, BTN_TOOL_PEN, 0);
8648 processKey(mapper, BTN_TOOL_BRUSH, 1);
8649 processSync(mapper);
8650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8653
8654 // pencil
8655 processKey(mapper, BTN_TOOL_BRUSH, 0);
8656 processKey(mapper, BTN_TOOL_PENCIL, 1);
8657 processSync(mapper);
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8660 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8661
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008662 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008663 processKey(mapper, BTN_TOOL_PENCIL, 0);
8664 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8665 processSync(mapper);
8666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8668 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8669
8670 // mouse
8671 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8672 processKey(mapper, BTN_TOOL_MOUSE, 1);
8673 processSync(mapper);
8674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8676 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8677
8678 // lens
8679 processKey(mapper, BTN_TOOL_MOUSE, 0);
8680 processKey(mapper, BTN_TOOL_LENS, 1);
8681 processSync(mapper);
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8685
8686 // double-tap
8687 processKey(mapper, BTN_TOOL_LENS, 0);
8688 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8689 processSync(mapper);
8690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8693
8694 // triple-tap
8695 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8696 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8697 processSync(mapper);
8698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8701
8702 // quad-tap
8703 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8704 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8705 processSync(mapper);
8706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8709
8710 // finger
8711 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8712 processKey(mapper, BTN_TOOL_FINGER, 1);
8713 processSync(mapper);
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8717
8718 // stylus trumps finger
8719 processKey(mapper, BTN_TOOL_PEN, 1);
8720 processSync(mapper);
8721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8723 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8724
8725 // eraser trumps stylus
8726 processKey(mapper, BTN_TOOL_RUBBER, 1);
8727 processSync(mapper);
8728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8730 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8731
8732 // mouse trumps eraser
8733 processKey(mapper, BTN_TOOL_MOUSE, 1);
8734 processSync(mapper);
8735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8736 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8738
8739 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8740 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8741 processSync(mapper);
8742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8744 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8745
8746 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8747 processToolType(mapper, MT_TOOL_PEN);
8748 processSync(mapper);
8749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8751 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8752
8753 // back to default tool type
8754 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8755 processKey(mapper, BTN_TOOL_MOUSE, 0);
8756 processKey(mapper, BTN_TOOL_RUBBER, 0);
8757 processKey(mapper, BTN_TOOL_PEN, 0);
8758 processKey(mapper, BTN_TOOL_FINGER, 0);
8759 processSync(mapper);
8760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8761 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8763}
8764
8765TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008766 addConfigurationProperty("touch.deviceType", "touchScreen");
8767 prepareDisplay(DISPLAY_ORIENTATION_0);
8768 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008769 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008770 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008771
8772 NotifyMotionArgs motionArgs;
8773
8774 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8775 processId(mapper, 1);
8776 processPosition(mapper, 100, 200);
8777 processSync(mapper);
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8779 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8781 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8782
8783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8784 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8786 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8787
8788 // move a little
8789 processPosition(mapper, 150, 250);
8790 processSync(mapper);
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8792 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8794 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8795
8796 // down when BTN_TOUCH is pressed, pressure defaults to 1
8797 processKey(mapper, BTN_TOUCH, 1);
8798 processSync(mapper);
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8802 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8803
8804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8805 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8807 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8808
8809 // up when BTN_TOUCH is released, hover restored
8810 processKey(mapper, BTN_TOUCH, 0);
8811 processSync(mapper);
8812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8813 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8815 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8816
8817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8818 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8820 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8821
8822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8823 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8825 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8826
8827 // exit hover when pointer goes away
8828 processId(mapper, -1);
8829 processSync(mapper);
8830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8831 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8833 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8834}
8835
8836TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008837 addConfigurationProperty("touch.deviceType", "touchScreen");
8838 prepareDisplay(DISPLAY_ORIENTATION_0);
8839 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008840 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008841
8842 NotifyMotionArgs motionArgs;
8843
8844 // initially hovering because pressure is 0
8845 processId(mapper, 1);
8846 processPosition(mapper, 100, 200);
8847 processPressure(mapper, 0);
8848 processSync(mapper);
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8850 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8852 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8853
8854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8855 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8857 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8858
8859 // move a little
8860 processPosition(mapper, 150, 250);
8861 processSync(mapper);
8862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8863 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8865 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8866
8867 // down when pressure becomes non-zero
8868 processPressure(mapper, RAW_PRESSURE_MAX);
8869 processSync(mapper);
8870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8871 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8873 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8874
8875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8876 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8878 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8879
8880 // up when pressure becomes 0, hover restored
8881 processPressure(mapper, 0);
8882 processSync(mapper);
8883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8884 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8886 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8887
8888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8889 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8891 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8892
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8894 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8896 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8897
8898 // exit hover when pointer goes away
8899 processId(mapper, -1);
8900 processSync(mapper);
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8902 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8903 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8904 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8905}
8906
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008907/**
8908 * Set the input device port <--> display port associations, and check that the
8909 * events are routed to the display that matches the display port.
8910 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8911 */
8912TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008913 const std::string usb2 = "USB2";
8914 const uint8_t hdmi1 = 0;
8915 const uint8_t hdmi2 = 1;
8916 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008917 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008918
8919 addConfigurationProperty("touch.deviceType", "touchScreen");
8920 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008921 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008922
8923 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8924 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8925
8926 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8927 // for this input device is specified, and the matching viewport is not present,
8928 // the input device should be disabled (at the mapper level).
8929
8930 // Add viewport for display 2 on hdmi2
8931 prepareSecondaryDisplay(type, hdmi2);
8932 // Send a touch event
8933 processPosition(mapper, 100, 100);
8934 processSync(mapper);
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8936
8937 // Add viewport for display 1 on hdmi1
8938 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
8939 // Send a touch event again
8940 processPosition(mapper, 100, 100);
8941 processSync(mapper);
8942
8943 NotifyMotionArgs args;
8944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8945 ASSERT_EQ(DISPLAY_ID, args.displayId);
8946}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008947
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008948TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8949 addConfigurationProperty("touch.deviceType", "touchScreen");
8950 prepareAxes(POSITION);
8951 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8952
8953 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8954
8955 prepareDisplay(DISPLAY_ORIENTATION_0);
8956 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
8957
8958 // Send a touch event
8959 processPosition(mapper, 100, 100);
8960 processSync(mapper);
8961
8962 NotifyMotionArgs args;
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8964 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8965}
8966
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008967TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008968 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008969 std::shared_ptr<FakePointerController> fakePointerController =
8970 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008971 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008972 fakePointerController->setPosition(100, 200);
8973 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008974 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008975
Garfield Tan888a6a42020-01-09 11:39:16 -08008976 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008977 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008978
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008979 prepareDisplay(DISPLAY_ORIENTATION_0);
8980 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008981 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008982
Harry Cutts16a24cc2022-10-26 15:22:19 +00008983 // Check source is a touchpad that would obtain the PointerController.
8984 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008985
8986 NotifyMotionArgs motionArgs;
8987 processPosition(mapper, 100, 100);
8988 processSync(mapper);
8989
8990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8991 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8992 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8993}
8994
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008995/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008996 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8997 */
8998TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8999 addConfigurationProperty("touch.deviceType", "touchScreen");
9000 prepareAxes(POSITION);
9001 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9002
9003 prepareDisplay(DISPLAY_ORIENTATION_0);
9004 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
9005 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
9006 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
9007 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9008
9009 NotifyMotionArgs args;
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9011 ASSERT_EQ(26, args.readTime);
9012
9013 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
9014 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
9015 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
9016
9017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9018 ASSERT_EQ(33, args.readTime);
9019}
9020
9021/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009022 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9023 * events should not be delivered to the listener.
9024 */
9025TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9026 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009027 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009028 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9029 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9030 ViewportType::INTERNAL);
9031 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9032 prepareAxes(POSITION);
9033 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9034
9035 NotifyMotionArgs motionArgs;
9036 processPosition(mapper, 100, 100);
9037 processSync(mapper);
9038
9039 mFakeListener->assertNotifyMotionWasNotCalled();
9040}
9041
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009042/**
9043 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9044 * the touch mapper can process the events and the events can be delivered to the listener.
9045 */
9046TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9047 addConfigurationProperty("touch.deviceType", "touchScreen");
9048 addConfigurationProperty("touch.enableForInactiveViewport", "1");
9049 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9050 DISPLAY_ORIENTATION_0, false /*isActive*/, UNIQUE_ID, NO_PORT,
9051 ViewportType::INTERNAL);
9052 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9053 prepareAxes(POSITION);
9054 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9055
9056 NotifyMotionArgs motionArgs;
9057 processPosition(mapper, 100, 100);
9058 processSync(mapper);
9059
9060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9061 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9062}
9063
Garfield Tanc734e4f2021-01-15 20:01:39 -08009064TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9065 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009066 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Garfield Tanc734e4f2021-01-15 20:01:39 -08009067 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
9068 DISPLAY_ORIENTATION_0, true /*isActive*/, UNIQUE_ID, NO_PORT,
9069 ViewportType::INTERNAL);
9070 std::optional<DisplayViewport> optionalDisplayViewport =
9071 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9072 ASSERT_TRUE(optionalDisplayViewport.has_value());
9073 DisplayViewport displayViewport = *optionalDisplayViewport;
9074
9075 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9076 prepareAxes(POSITION);
9077 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9078
9079 // Finger down
9080 int32_t x = 100, y = 100;
9081 processPosition(mapper, x, y);
9082 processSync(mapper);
9083
9084 NotifyMotionArgs motionArgs;
9085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9086 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9087
9088 // Deactivate display viewport
9089 displayViewport.isActive = false;
9090 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9091 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9092
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009093 // The ongoing touch should be canceled immediately
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9095 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9096
9097 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009098 x += 10, y += 10;
9099 processPosition(mapper, x, y);
9100 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009102
9103 // Reactivate display viewport
9104 displayViewport.isActive = true;
9105 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9106 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9107
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009108 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009109 x += 10, y += 10;
9110 processPosition(mapper, x, y);
9111 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9113 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009114}
9115
Arthur Hung7c645402019-01-25 17:45:42 +08009116TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9117 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009118 prepareAxes(POSITION | ID | SLOT);
9119 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009120 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009121
9122 // Create the second touch screen device, and enable multi fingers.
9123 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009124 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009125 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009126 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009127 std::shared_ptr<InputDevice> device2 =
9128 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009129 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009130
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009131 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9132 0 /*flat*/, 0 /*fuzz*/);
9133 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9134 0 /*flat*/, 0 /*fuzz*/);
9135 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
9136 0 /*flat*/, 0 /*fuzz*/);
9137 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
9138 0 /*flat*/, 0 /*fuzz*/);
9139 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
9140 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9141 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009142
9143 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009144 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009145 std::list<NotifyArgs> unused =
9146 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9147 0 /*changes*/);
9148 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009149
9150 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009151 std::shared_ptr<FakePointerController> fakePointerController =
9152 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009153 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009154
9155 // Setup policy for associated displays and show touches.
9156 const uint8_t hdmi1 = 0;
9157 const uint8_t hdmi2 = 1;
9158 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9159 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9160 mFakePolicy->setShowTouches(true);
9161
9162 // Create displays.
9163 prepareDisplay(DISPLAY_ORIENTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009164 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009165
9166 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009167 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9168 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9169 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009170
9171 // Two fingers down at default display.
9172 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9173 processPosition(mapper, x1, y1);
9174 processId(mapper, 1);
9175 processSlot(mapper, 1);
9176 processPosition(mapper, x2, y2);
9177 processId(mapper, 2);
9178 processSync(mapper);
9179
9180 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9181 fakePointerController->getSpots().find(DISPLAY_ID);
9182 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9183 ASSERT_EQ(size_t(2), iter->second.size());
9184
9185 // Two fingers down at second display.
9186 processPosition(mapper2, x1, y1);
9187 processId(mapper2, 1);
9188 processSlot(mapper2, 1);
9189 processPosition(mapper2, x2, y2);
9190 processId(mapper2, 2);
9191 processSync(mapper2);
9192
9193 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9194 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9195 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009196
9197 // Disable the show touches configuration and ensure the spots are cleared.
9198 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009199 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9200 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009201
9202 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009203}
9204
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009205TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009206 prepareAxes(POSITION);
9207 addConfigurationProperty("touch.deviceType", "touchScreen");
9208 prepareDisplay(DISPLAY_ORIENTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009209 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009210
9211 NotifyMotionArgs motionArgs;
9212 // Unrotated video frame
9213 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9214 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009215 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009216 processPosition(mapper, 100, 200);
9217 processSync(mapper);
9218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9219 ASSERT_EQ(frames, motionArgs.videoFrames);
9220
9221 // Subsequent touch events should not have any videoframes
9222 // This is implemented separately in FakeEventHub,
9223 // but that should match the behaviour of TouchVideoDevice.
9224 processPosition(mapper, 200, 200);
9225 processSync(mapper);
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9227 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9228}
9229
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009230TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009231 prepareAxes(POSITION);
9232 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009233 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009234 // Unrotated video frame
9235 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9236 NotifyMotionArgs motionArgs;
9237
9238 // Test all 4 orientations
9239 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009240 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9241 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9242 clearViewports();
9243 prepareDisplay(orientation);
9244 std::vector<TouchVideoFrame> frames{frame};
9245 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9246 processPosition(mapper, 100, 200);
9247 processSync(mapper);
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9249 ASSERT_EQ(frames, motionArgs.videoFrames);
9250 }
9251}
9252
9253TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9254 prepareAxes(POSITION);
9255 addConfigurationProperty("touch.deviceType", "touchScreen");
9256 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9257 // orientation-aware are affected by display rotation.
9258 addConfigurationProperty("touch.orientationAware", "0");
9259 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9260 // Unrotated video frame
9261 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9262 NotifyMotionArgs motionArgs;
9263
9264 // Test all 4 orientations
9265 for (int32_t orientation : {DISPLAY_ORIENTATION_0, DISPLAY_ORIENTATION_90,
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009266 DISPLAY_ORIENTATION_180, DISPLAY_ORIENTATION_270}) {
9267 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9268 clearViewports();
9269 prepareDisplay(orientation);
9270 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009271 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009272 processPosition(mapper, 100, 200);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009275 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9276 // compared to the display. This is so that when the window transform (which contains the
9277 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9278 // window's coordinate space.
9279 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009280 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009281
9282 // Release finger.
9283 processSync(mapper);
9284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009285 }
9286}
9287
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009288TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009289 prepareAxes(POSITION);
9290 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009291 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009292 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9293 // so mix these.
9294 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9295 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9296 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9297 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9298 NotifyMotionArgs motionArgs;
9299
9300 prepareDisplay(DISPLAY_ORIENTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009301 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009302 processPosition(mapper, 100, 200);
9303 processSync(mapper);
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009305 ASSERT_EQ(frames, motionArgs.videoFrames);
9306}
9307
9308TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9309 prepareAxes(POSITION);
9310 addConfigurationProperty("touch.deviceType", "touchScreen");
9311 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9312 // orientation-aware are affected by display rotation.
9313 addConfigurationProperty("touch.orientationAware", "0");
9314 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9315 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9316 // so mix these.
9317 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9318 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9319 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9320 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9321 NotifyMotionArgs motionArgs;
9322
9323 prepareDisplay(DISPLAY_ORIENTATION_90);
9324 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9325 processPosition(mapper, 100, 200);
9326 processSync(mapper);
9327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9328 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9329 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9330 // compared to the display. This is so that when the window transform (which contains the
9331 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9332 // window's coordinate space.
9333 frame.rotate(getInverseRotation(DISPLAY_ORIENTATION_90));
9334 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009335 ASSERT_EQ(frames, motionArgs.videoFrames);
9336}
9337
Arthur Hung9da14732019-09-02 16:16:58 +08009338/**
9339 * If we had defined port associations, but the viewport is not ready, the touch device would be
9340 * expected to be disabled, and it should be enabled after the viewport has found.
9341 */
9342TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009343 constexpr uint8_t hdmi2 = 1;
9344 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009345 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009346
9347 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9348
9349 addConfigurationProperty("touch.deviceType", "touchScreen");
9350 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009351 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009352
9353 ASSERT_EQ(mDevice->isEnabled(), false);
9354
9355 // Add display on hdmi2, the device should be enabled and can receive touch event.
9356 prepareSecondaryDisplay(type, hdmi2);
9357 ASSERT_EQ(mDevice->isEnabled(), true);
9358
9359 // Send a touch event.
9360 processPosition(mapper, 100, 100);
9361 processSync(mapper);
9362
9363 NotifyMotionArgs args;
9364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9365 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9366}
9367
Arthur Hung421eb1c2020-01-16 00:09:42 +08009368TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009369 addConfigurationProperty("touch.deviceType", "touchScreen");
9370 prepareDisplay(DISPLAY_ORIENTATION_0);
9371 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009372 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009373
9374 NotifyMotionArgs motionArgs;
9375
9376 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9377 // finger down
9378 processId(mapper, 1);
9379 processPosition(mapper, x1, y1);
9380 processSync(mapper);
9381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9384
9385 // finger move
9386 processId(mapper, 1);
9387 processPosition(mapper, x2, y2);
9388 processSync(mapper);
9389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9392
9393 // finger up.
9394 processId(mapper, -1);
9395 processSync(mapper);
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9397 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9398 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9399
9400 // new finger down
9401 processId(mapper, 1);
9402 processPosition(mapper, x3, y3);
9403 processSync(mapper);
9404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9405 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9407}
9408
9409/**
arthurhungcc7f9802020-04-30 17:55:40 +08009410 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9411 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009412 */
arthurhungcc7f9802020-04-30 17:55:40 +08009413TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009414 addConfigurationProperty("touch.deviceType", "touchScreen");
9415 prepareDisplay(DISPLAY_ORIENTATION_0);
9416 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009417 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009418
9419 NotifyMotionArgs motionArgs;
9420
9421 // default tool type is finger
9422 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009423 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009424 processPosition(mapper, x1, y1);
9425 processSync(mapper);
9426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9427 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9428 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9429
9430 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9431 processToolType(mapper, MT_TOOL_PALM);
9432 processSync(mapper);
9433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9434 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9435
9436 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009437 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009438 processPosition(mapper, x2, y2);
9439 processSync(mapper);
9440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9441
9442 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009443 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009444 processSync(mapper);
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9446
9447 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009448 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009449 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009450 processPosition(mapper, x3, y3);
9451 processSync(mapper);
9452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9454 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9455}
9456
arthurhungbf89a482020-04-17 17:37:55 +08009457/**
arthurhungcc7f9802020-04-30 17:55:40 +08009458 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9459 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009460 */
arthurhungcc7f9802020-04-30 17:55:40 +08009461TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009462 addConfigurationProperty("touch.deviceType", "touchScreen");
9463 prepareDisplay(DISPLAY_ORIENTATION_0);
9464 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9465 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9466
9467 NotifyMotionArgs motionArgs;
9468
9469 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009470 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9471 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009472 processPosition(mapper, x1, y1);
9473 processSync(mapper);
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9476 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9477
9478 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009479 processSlot(mapper, SECOND_SLOT);
9480 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009481 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009482 processSync(mapper);
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009484 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9486
9487 // If the tool type of the first finger changes to MT_TOOL_PALM,
9488 // we expect to receive ACTION_POINTER_UP with cancel flag.
9489 processSlot(mapper, FIRST_SLOT);
9490 processId(mapper, FIRST_TRACKING_ID);
9491 processToolType(mapper, MT_TOOL_PALM);
9492 processSync(mapper);
9493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009494 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009495 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9496
9497 // The following MOVE events of second finger should be processed.
9498 processSlot(mapper, SECOND_SLOT);
9499 processId(mapper, SECOND_TRACKING_ID);
9500 processPosition(mapper, x2 + 1, y2 + 1);
9501 processSync(mapper);
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9504 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9505
9506 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9507 // it. Second finger receive move.
9508 processSlot(mapper, FIRST_SLOT);
9509 processId(mapper, INVALID_TRACKING_ID);
9510 processSync(mapper);
9511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9513 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9514
9515 // Second finger keeps moving.
9516 processSlot(mapper, SECOND_SLOT);
9517 processId(mapper, SECOND_TRACKING_ID);
9518 processPosition(mapper, x2 + 2, y2 + 2);
9519 processSync(mapper);
9520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9522 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9523
9524 // Second finger up.
9525 processId(mapper, INVALID_TRACKING_ID);
9526 processSync(mapper);
9527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9528 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9529 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9530}
9531
9532/**
9533 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9534 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9535 */
9536TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9537 addConfigurationProperty("touch.deviceType", "touchScreen");
9538 prepareDisplay(DISPLAY_ORIENTATION_0);
9539 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9540 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9541
9542 NotifyMotionArgs motionArgs;
9543
9544 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9545 // First finger down.
9546 processId(mapper, FIRST_TRACKING_ID);
9547 processPosition(mapper, x1, y1);
9548 processSync(mapper);
9549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9550 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9551 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9552
9553 // Second finger down.
9554 processSlot(mapper, SECOND_SLOT);
9555 processId(mapper, SECOND_TRACKING_ID);
9556 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009557 processSync(mapper);
9558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009559 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009560 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9561
arthurhungcc7f9802020-04-30 17:55:40 +08009562 // If the tool type of the first finger changes to MT_TOOL_PALM,
9563 // we expect to receive ACTION_POINTER_UP with cancel flag.
9564 processSlot(mapper, FIRST_SLOT);
9565 processId(mapper, FIRST_TRACKING_ID);
9566 processToolType(mapper, MT_TOOL_PALM);
9567 processSync(mapper);
9568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009569 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009570 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9571
9572 // Second finger keeps moving.
9573 processSlot(mapper, SECOND_SLOT);
9574 processId(mapper, SECOND_TRACKING_ID);
9575 processPosition(mapper, x2 + 1, y2 + 1);
9576 processSync(mapper);
9577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9579
9580 // second finger becomes palm, receive cancel due to only 1 finger is active.
9581 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009582 processToolType(mapper, MT_TOOL_PALM);
9583 processSync(mapper);
9584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9585 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9586
arthurhungcc7f9802020-04-30 17:55:40 +08009587 // third finger down.
9588 processSlot(mapper, THIRD_SLOT);
9589 processId(mapper, THIRD_TRACKING_ID);
9590 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009591 processPosition(mapper, x3, y3);
9592 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9594 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009596 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9597
9598 // third finger move
9599 processId(mapper, THIRD_TRACKING_ID);
9600 processPosition(mapper, x3 + 1, y3 + 1);
9601 processSync(mapper);
9602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9604
9605 // first finger up, third finger receive move.
9606 processSlot(mapper, FIRST_SLOT);
9607 processId(mapper, INVALID_TRACKING_ID);
9608 processSync(mapper);
9609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9611 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9612
9613 // second finger up, third finger receive move.
9614 processSlot(mapper, SECOND_SLOT);
9615 processId(mapper, INVALID_TRACKING_ID);
9616 processSync(mapper);
9617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9619 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9620
9621 // third finger up.
9622 processSlot(mapper, THIRD_SLOT);
9623 processId(mapper, INVALID_TRACKING_ID);
9624 processSync(mapper);
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9626 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9627 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9628}
9629
9630/**
9631 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9632 * and the active finger could still be allowed to receive the events
9633 */
9634TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9635 addConfigurationProperty("touch.deviceType", "touchScreen");
9636 prepareDisplay(DISPLAY_ORIENTATION_0);
9637 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9638 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9639
9640 NotifyMotionArgs motionArgs;
9641
9642 // default tool type is finger
9643 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9644 processId(mapper, FIRST_TRACKING_ID);
9645 processPosition(mapper, x1, y1);
9646 processSync(mapper);
9647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9648 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9649 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9650
9651 // Second finger down.
9652 processSlot(mapper, SECOND_SLOT);
9653 processId(mapper, SECOND_TRACKING_ID);
9654 processPosition(mapper, x2, y2);
9655 processSync(mapper);
9656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009657 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9659
9660 // If the tool type of the second finger changes to MT_TOOL_PALM,
9661 // we expect to receive ACTION_POINTER_UP with cancel flag.
9662 processId(mapper, SECOND_TRACKING_ID);
9663 processToolType(mapper, MT_TOOL_PALM);
9664 processSync(mapper);
9665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009666 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009667 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9668
9669 // The following MOVE event should be processed.
9670 processSlot(mapper, FIRST_SLOT);
9671 processId(mapper, FIRST_TRACKING_ID);
9672 processPosition(mapper, x1 + 1, y1 + 1);
9673 processSync(mapper);
9674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9676 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9677
9678 // second finger up.
9679 processSlot(mapper, SECOND_SLOT);
9680 processId(mapper, INVALID_TRACKING_ID);
9681 processSync(mapper);
9682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9684
9685 // first finger keep moving
9686 processSlot(mapper, FIRST_SLOT);
9687 processId(mapper, FIRST_TRACKING_ID);
9688 processPosition(mapper, x1 + 2, y1 + 2);
9689 processSync(mapper);
9690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9691 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9692
9693 // first finger up.
9694 processId(mapper, INVALID_TRACKING_ID);
9695 processSync(mapper);
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9697 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9698 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009699}
9700
Arthur Hung9ad18942021-06-19 02:04:46 +00009701/**
9702 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9703 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9704 * cause slot be valid again.
9705 */
9706TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9707 addConfigurationProperty("touch.deviceType", "touchScreen");
9708 prepareDisplay(DISPLAY_ORIENTATION_0);
9709 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9710 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9711
9712 NotifyMotionArgs motionArgs;
9713
9714 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9715 // First finger down.
9716 processId(mapper, FIRST_TRACKING_ID);
9717 processPosition(mapper, x1, y1);
9718 processPressure(mapper, RAW_PRESSURE_MAX);
9719 processSync(mapper);
9720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9721 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9722 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9723
9724 // First finger move.
9725 processId(mapper, FIRST_TRACKING_ID);
9726 processPosition(mapper, x1 + 1, y1 + 1);
9727 processPressure(mapper, RAW_PRESSURE_MAX);
9728 processSync(mapper);
9729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9731 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9732
9733 // Second finger down.
9734 processSlot(mapper, SECOND_SLOT);
9735 processId(mapper, SECOND_TRACKING_ID);
9736 processPosition(mapper, x2, y2);
9737 processPressure(mapper, RAW_PRESSURE_MAX);
9738 processSync(mapper);
9739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009740 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009741 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9742
9743 // second finger up with some unexpected data.
9744 processSlot(mapper, SECOND_SLOT);
9745 processId(mapper, INVALID_TRACKING_ID);
9746 processPosition(mapper, x2, y2);
9747 processSync(mapper);
9748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009749 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009750 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9751
9752 // first finger up with some unexpected data.
9753 processSlot(mapper, FIRST_SLOT);
9754 processId(mapper, INVALID_TRACKING_ID);
9755 processPosition(mapper, x2, y2);
9756 processPressure(mapper, RAW_PRESSURE_MAX);
9757 processSync(mapper);
9758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9759 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9760 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9761}
9762
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009763TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9764 addConfigurationProperty("touch.deviceType", "touchScreen");
9765 prepareDisplay(DISPLAY_ORIENTATION_0);
9766 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9767 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9768
9769 // First finger down.
9770 processId(mapper, FIRST_TRACKING_ID);
9771 processPosition(mapper, 100, 200);
9772 processPressure(mapper, RAW_PRESSURE_MAX);
9773 processSync(mapper);
9774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9775 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9776
9777 // Second finger down.
9778 processSlot(mapper, SECOND_SLOT);
9779 processId(mapper, SECOND_TRACKING_ID);
9780 processPosition(mapper, 300, 400);
9781 processPressure(mapper, RAW_PRESSURE_MAX);
9782 processSync(mapper);
9783 ASSERT_NO_FATAL_FAILURE(
9784 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9785
9786 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009787 // preserved. Resetting should cancel the ongoing gesture.
9788 resetMapper(mapper, ARBITRARY_TIME);
9789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9790 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009791
9792 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9793 // the existing touch state to generate a down event.
9794 processPosition(mapper, 301, 302);
9795 processSync(mapper);
9796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9797 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9799 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9800
9801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9802}
9803
9804TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9805 addConfigurationProperty("touch.deviceType", "touchScreen");
9806 prepareDisplay(DISPLAY_ORIENTATION_0);
9807 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9808 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9809
9810 // First finger touches down and releases.
9811 processId(mapper, FIRST_TRACKING_ID);
9812 processPosition(mapper, 100, 200);
9813 processPressure(mapper, RAW_PRESSURE_MAX);
9814 processSync(mapper);
9815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9816 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9817 processId(mapper, INVALID_TRACKING_ID);
9818 processSync(mapper);
9819 ASSERT_NO_FATAL_FAILURE(
9820 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9821
9822 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9823 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009824 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9826
9827 // Send an empty sync frame. Since there are no pointers, no events are generated.
9828 processSync(mapper);
9829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9830}
9831
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009832TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009833 addConfigurationProperty("touch.deviceType", "touchScreen");
9834 prepareDisplay(DISPLAY_ORIENTATION_0);
9835 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9836 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009838
9839 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9840 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9841 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9842 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9843 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9844
9845 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009846 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009847 processId(mapper, FIRST_TRACKING_ID);
9848 processToolType(mapper, MT_TOOL_PEN);
9849 processPosition(mapper, 100, 200);
9850 processPressure(mapper, RAW_PRESSURE_MAX);
9851 processSync(mapper);
9852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9853 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9854 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9855 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9856
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009857 // Now that we know the device supports styluses, ensure that the device is re-configured with
9858 // the stylus source.
9859 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9860 {
9861 const auto& devices = mReader->getInputDevices();
9862 auto deviceInfo =
9863 std::find_if(devices.begin(), devices.end(),
9864 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9865 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9866 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9867 }
9868
9869 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9871
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009872 processId(mapper, INVALID_TRACKING_ID);
9873 processSync(mapper);
9874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9875 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9876 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9877 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009878}
9879
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009880// --- MultiTouchInputMapperTest_ExternalDevice ---
9881
9882class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9883protected:
Chris Yea52ade12020-08-27 16:49:20 -07009884 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009885};
9886
9887/**
9888 * Expect fallback to internal viewport if device is external and external viewport is not present.
9889 */
9890TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9891 prepareAxes(POSITION);
9892 addConfigurationProperty("touch.deviceType", "touchScreen");
9893 prepareDisplay(DISPLAY_ORIENTATION_0);
9894 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9895
9896 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9897
9898 NotifyMotionArgs motionArgs;
9899
9900 // Expect the event to be sent to the internal viewport,
9901 // because an external viewport is not present.
9902 processPosition(mapper, 100, 100);
9903 processSync(mapper);
9904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9905 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9906
9907 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009908 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009909 processPosition(mapper, 100, 100);
9910 processSync(mapper);
9911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9912 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9913}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009914
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009915TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9916 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9917 std::shared_ptr<FakePointerController> fakePointerController =
9918 std::make_shared<FakePointerController>();
9919 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9920 fakePointerController->setPosition(0, 0);
9921 fakePointerController->setButtonState(0);
9922
9923 // prepare device and capture
9924 prepareDisplay(DISPLAY_ORIENTATION_0);
9925 prepareAxes(POSITION | ID | SLOT);
9926 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9927 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9928 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009929 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009930 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9931
9932 // captured touchpad should be a touchpad source
9933 NotifyDeviceResetArgs resetArgs;
9934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9935 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9936
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009937 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009938
9939 const InputDeviceInfo::MotionRange* relRangeX =
9940 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9941 ASSERT_NE(relRangeX, nullptr);
9942 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9943 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9944 const InputDeviceInfo::MotionRange* relRangeY =
9945 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9946 ASSERT_NE(relRangeY, nullptr);
9947 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9948 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9949
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009950 // run captured pointer tests - note that this is unscaled, so input listener events should be
9951 // identical to what the hardware sends (accounting for any
9952 // calibration).
9953 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009954 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009955 processId(mapper, 1);
9956 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9957 processKey(mapper, BTN_TOUCH, 1);
9958 processSync(mapper);
9959
9960 // expect coord[0] to contain initial location of touch 0
9961 NotifyMotionArgs args;
9962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9963 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9964 ASSERT_EQ(1U, args.pointerCount);
9965 ASSERT_EQ(0, args.pointerProperties[0].id);
9966 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9967 ASSERT_NO_FATAL_FAILURE(
9968 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9969
9970 // FINGER 1 DOWN
9971 processSlot(mapper, 1);
9972 processId(mapper, 2);
9973 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9974 processSync(mapper);
9975
9976 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009978 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009979 ASSERT_EQ(2U, args.pointerCount);
9980 ASSERT_EQ(0, args.pointerProperties[0].id);
9981 ASSERT_EQ(1, args.pointerProperties[1].id);
9982 ASSERT_NO_FATAL_FAILURE(
9983 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9984 ASSERT_NO_FATAL_FAILURE(
9985 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9986
9987 // FINGER 1 MOVE
9988 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9989 processSync(mapper);
9990
9991 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9992 // from move
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9994 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9995 ASSERT_NO_FATAL_FAILURE(
9996 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9997 ASSERT_NO_FATAL_FAILURE(
9998 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9999
10000 // FINGER 0 MOVE
10001 processSlot(mapper, 0);
10002 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10003 processSync(mapper);
10004
10005 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10008 ASSERT_NO_FATAL_FAILURE(
10009 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10010 ASSERT_NO_FATAL_FAILURE(
10011 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10012
10013 // BUTTON DOWN
10014 processKey(mapper, BTN_LEFT, 1);
10015 processSync(mapper);
10016
10017 // touchinputmapper design sends a move before button press
10018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10021 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10022
10023 // BUTTON UP
10024 processKey(mapper, BTN_LEFT, 0);
10025 processSync(mapper);
10026
10027 // touchinputmapper design sends a move after button release
10028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10029 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10032
10033 // FINGER 0 UP
10034 processId(mapper, -1);
10035 processSync(mapper);
10036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10037 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10038
10039 // FINGER 1 MOVE
10040 processSlot(mapper, 1);
10041 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10042 processSync(mapper);
10043
10044 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10046 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10047 ASSERT_EQ(1U, args.pointerCount);
10048 ASSERT_EQ(1, args.pointerProperties[0].id);
10049 ASSERT_NO_FATAL_FAILURE(
10050 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10051
10052 // FINGER 1 UP
10053 processId(mapper, -1);
10054 processKey(mapper, BTN_TOUCH, 0);
10055 processSync(mapper);
10056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10057 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10058
Harry Cutts16a24cc2022-10-26 15:22:19 +000010059 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010060 mFakePolicy->setPointerCapture(false);
10061 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +000010063 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010064}
10065
10066TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10067 std::shared_ptr<FakePointerController> fakePointerController =
10068 std::make_shared<FakePointerController>();
10069 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10070 fakePointerController->setPosition(0, 0);
10071 fakePointerController->setButtonState(0);
10072
10073 // prepare device and capture
10074 prepareDisplay(DISPLAY_ORIENTATION_0);
10075 prepareAxes(POSITION | ID | SLOT);
10076 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10077 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010078 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010079 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10080 // run uncaptured pointer tests - pushes out generic events
10081 // FINGER 0 DOWN
10082 processId(mapper, 3);
10083 processPosition(mapper, 100, 100);
10084 processKey(mapper, BTN_TOUCH, 1);
10085 processSync(mapper);
10086
10087 // start at (100,100), cursor should be at (0,0) * scale
10088 NotifyMotionArgs args;
10089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10090 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10091 ASSERT_NO_FATAL_FAILURE(
10092 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10093
10094 // FINGER 0 MOVE
10095 processPosition(mapper, 200, 200);
10096 processSync(mapper);
10097
10098 // compute scaling to help with touch position checking
10099 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10100 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10101 float scale =
10102 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10103
10104 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10106 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10107 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10108 0, 0, 0, 0, 0, 0, 0));
10109}
10110
10111TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10112 std::shared_ptr<FakePointerController> fakePointerController =
10113 std::make_shared<FakePointerController>();
10114
10115 prepareDisplay(DISPLAY_ORIENTATION_0);
10116 prepareAxes(POSITION | ID | SLOT);
10117 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010118 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010119 mFakePolicy->setPointerCapture(false);
10120 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10121
Harry Cutts16a24cc2022-10-26 15:22:19 +000010122 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
10123 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010124
Harry Cutts16a24cc2022-10-26 15:22:19 +000010125 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010126 mFakePolicy->setPointerCapture(true);
10127 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10128 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10129}
10130
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010131// --- BluetoothMultiTouchInputMapperTest ---
10132
10133class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10134protected:
10135 void SetUp() override {
10136 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10137 }
10138};
10139
10140TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10141 addConfigurationProperty("touch.deviceType", "touchScreen");
10142 prepareDisplay(DISPLAY_ORIENTATION_0);
10143 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10144 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10145
10146 nsecs_t kernelEventTime = ARBITRARY_TIME;
10147 nsecs_t expectedEventTime = ARBITRARY_TIME;
10148 // Touch down.
10149 processId(mapper, FIRST_TRACKING_ID);
10150 processPosition(mapper, 100, 200);
10151 processPressure(mapper, RAW_PRESSURE_MAX);
10152 processSync(mapper, ARBITRARY_TIME);
10153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10154 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10155
10156 // Process several events that come in quick succession, according to their timestamps.
10157 for (int i = 0; i < 3; i++) {
10158 constexpr static nsecs_t delta = ms2ns(1);
10159 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10160 kernelEventTime += delta;
10161 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10162
10163 processPosition(mapper, 101 + i, 201 + i);
10164 processSync(mapper, kernelEventTime);
10165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10166 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10167 WithEventTime(expectedEventTime))));
10168 }
10169
10170 // Release the touch.
10171 processId(mapper, INVALID_TRACKING_ID);
10172 processPressure(mapper, RAW_PRESSURE_MIN);
10173 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10175 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10176 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10177}
10178
10179// --- MultiTouchPointerModeTest ---
10180
HQ Liue6983c72022-04-19 22:14:56 +000010181class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10182protected:
10183 float mPointerMovementScale;
10184 float mPointerXZoomScale;
10185 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10186 addConfigurationProperty("touch.deviceType", "pointer");
10187 std::shared_ptr<FakePointerController> fakePointerController =
10188 std::make_shared<FakePointerController>();
10189 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10190 fakePointerController->setPosition(0, 0);
10191 fakePointerController->setButtonState(0);
10192 prepareDisplay(DISPLAY_ORIENTATION_0);
10193
10194 prepareAxes(POSITION);
10195 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10196 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10197 // needs to be disabled, and the pointer gesture needs to be enabled.
10198 mFakePolicy->setPointerCapture(false);
10199 mFakePolicy->setPointerGestureEnabled(true);
10200 mFakePolicy->setPointerController(fakePointerController);
10201
10202 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10203 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10204 mPointerMovementScale =
10205 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10206 mPointerXZoomScale =
10207 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10208 }
10209
10210 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10211 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10212 /*flat*/ 0,
10213 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10214 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10215 /*flat*/ 0,
10216 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10217 }
10218};
10219
10220/**
10221 * Two fingers down on a pointer mode touch pad. The width
10222 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10223 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10224 * be greater than the both value to be freeform gesture, so that after two
10225 * fingers start to move downwards, the gesture should be swipe.
10226 */
10227TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10228 // The min freeform gesture width is 25units/mm x 30mm = 750
10229 // which is greater than fraction of the diagnal length of the touchpad (349).
10230 // Thus, MaxSwipWidth is 750.
10231 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10232 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10233 NotifyMotionArgs motionArgs;
10234
10235 // Two fingers down at once.
10236 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10237 // Pointer's initial position is used the [0,0] coordinate.
10238 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10239
10240 processId(mapper, FIRST_TRACKING_ID);
10241 processPosition(mapper, x1, y1);
10242 processMTSync(mapper);
10243 processId(mapper, SECOND_TRACKING_ID);
10244 processPosition(mapper, x2, y2);
10245 processMTSync(mapper);
10246 processSync(mapper);
10247
10248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10249 ASSERT_EQ(1U, motionArgs.pointerCount);
10250 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10251 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010252 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010253 ASSERT_NO_FATAL_FAILURE(
10254 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10255
10256 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10257 // that there should be 1 pointer.
10258 int32_t movingDistance = 200;
10259 y1 += movingDistance;
10260 y2 += movingDistance;
10261
10262 processId(mapper, FIRST_TRACKING_ID);
10263 processPosition(mapper, x1, y1);
10264 processMTSync(mapper);
10265 processId(mapper, SECOND_TRACKING_ID);
10266 processPosition(mapper, x2, y2);
10267 processMTSync(mapper);
10268 processSync(mapper);
10269
10270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10271 ASSERT_EQ(1U, motionArgs.pointerCount);
10272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010274 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10276 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10277 0, 0, 0, 0));
10278}
10279
10280/**
10281 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10282 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10283 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10284 * value to be freeform gesture, so that after two fingers start to move downwards,
10285 * the gesture should be swipe.
10286 */
10287TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10288 // The min freeform gesture width is 5units/mm x 30mm = 150
10289 // which is greater than fraction of the diagnal length of the touchpad (349).
10290 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10291 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10292 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10293 NotifyMotionArgs motionArgs;
10294
10295 // Two fingers down at once.
10296 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10297 // Pointer's initial position is used the [0,0] coordinate.
10298 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10299
10300 processId(mapper, FIRST_TRACKING_ID);
10301 processPosition(mapper, x1, y1);
10302 processMTSync(mapper);
10303 processId(mapper, SECOND_TRACKING_ID);
10304 processPosition(mapper, x2, y2);
10305 processMTSync(mapper);
10306 processSync(mapper);
10307
10308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10309 ASSERT_EQ(1U, motionArgs.pointerCount);
10310 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010312 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010313 ASSERT_NO_FATAL_FAILURE(
10314 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10315
10316 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10317 // and there should be 1 pointer.
10318 int32_t movingDistance = 200;
10319 y1 += movingDistance;
10320 y2 += movingDistance;
10321
10322 processId(mapper, FIRST_TRACKING_ID);
10323 processPosition(mapper, x1, y1);
10324 processMTSync(mapper);
10325 processId(mapper, SECOND_TRACKING_ID);
10326 processPosition(mapper, x2, y2);
10327 processMTSync(mapper);
10328 processSync(mapper);
10329
10330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10331 ASSERT_EQ(1U, motionArgs.pointerCount);
10332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10333 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010334 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010335 // New coordinate is the scaled relative coordinate from the initial coordinate.
10336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10337 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10338 0, 0, 0, 0));
10339}
10340
10341/**
10342 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10343 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10344 * freeform gestures after two fingers start to move downwards.
10345 */
10346TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10347 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10348 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10349
10350 NotifyMotionArgs motionArgs;
10351
10352 // Two fingers down at once. Wider than the max swipe width.
10353 // The gesture is expected to be PRESS, then transformed to FREEFORM
10354 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10355
10356 processId(mapper, FIRST_TRACKING_ID);
10357 processPosition(mapper, x1, y1);
10358 processMTSync(mapper);
10359 processId(mapper, SECOND_TRACKING_ID);
10360 processPosition(mapper, x2, y2);
10361 processMTSync(mapper);
10362 processSync(mapper);
10363
10364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10365 ASSERT_EQ(1U, motionArgs.pointerCount);
10366 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010368 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010369 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10370 ASSERT_NO_FATAL_FAILURE(
10371 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10372
10373 int32_t movingDistance = 200;
10374
10375 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10376 // then two down events for two pointers.
10377 y1 += movingDistance;
10378 y2 += movingDistance;
10379
10380 processId(mapper, FIRST_TRACKING_ID);
10381 processPosition(mapper, x1, y1);
10382 processMTSync(mapper);
10383 processId(mapper, SECOND_TRACKING_ID);
10384 processPosition(mapper, x2, y2);
10385 processMTSync(mapper);
10386 processSync(mapper);
10387
10388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10389 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10390 ASSERT_EQ(1U, motionArgs.pointerCount);
10391 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10394 ASSERT_EQ(1U, motionArgs.pointerCount);
10395 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10397 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010398 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010399 ASSERT_EQ(2U, motionArgs.pointerCount);
10400 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010402 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010403 // Two pointers' scaled relative coordinates from their initial centroid.
10404 // Initial y coordinates are 0 as y1 and y2 have the same value.
10405 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10406 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10407 // When pointers move, the new coordinates equal to the initial coordinates plus
10408 // scaled moving distance.
10409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10410 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10411 0, 0, 0, 0));
10412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10413 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10414 0, 0, 0, 0));
10415
10416 // Move two fingers down again, expect one MOVE motion event.
10417 y1 += movingDistance;
10418 y2 += movingDistance;
10419
10420 processId(mapper, FIRST_TRACKING_ID);
10421 processPosition(mapper, x1, y1);
10422 processMTSync(mapper);
10423 processId(mapper, SECOND_TRACKING_ID);
10424 processPosition(mapper, x2, y2);
10425 processMTSync(mapper);
10426 processSync(mapper);
10427
10428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10429 ASSERT_EQ(2U, motionArgs.pointerCount);
10430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010432 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10434 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10435 0, 0, 0, 0, 0));
10436 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10437 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10438 0, 0, 0, 0, 0));
10439}
10440
Harry Cutts39b7ca22022-10-05 15:55:48 +000010441TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10442 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10443 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10444 NotifyMotionArgs motionArgs;
10445
10446 // Place two fingers down.
10447 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10448
10449 processId(mapper, FIRST_TRACKING_ID);
10450 processPosition(mapper, x1, y1);
10451 processMTSync(mapper);
10452 processId(mapper, SECOND_TRACKING_ID);
10453 processPosition(mapper, x2, y2);
10454 processMTSync(mapper);
10455 processSync(mapper);
10456
10457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10458 ASSERT_EQ(1U, motionArgs.pointerCount);
10459 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10460 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10461 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10462 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10463
10464 // Move the two fingers down and to the left.
10465 int32_t movingDistance = 200;
10466 x1 -= movingDistance;
10467 y1 += movingDistance;
10468 x2 -= movingDistance;
10469 y2 += movingDistance;
10470
10471 processId(mapper, FIRST_TRACKING_ID);
10472 processPosition(mapper, x1, y1);
10473 processMTSync(mapper);
10474 processId(mapper, SECOND_TRACKING_ID);
10475 processPosition(mapper, x2, y2);
10476 processMTSync(mapper);
10477 processSync(mapper);
10478
10479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10480 ASSERT_EQ(1U, motionArgs.pointerCount);
10481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10482 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10483 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10484 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10485}
10486
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010487TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
10488 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10489 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10490 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10492
10493 // Start a stylus gesture.
10494 processKey(mapper, BTN_TOOL_PEN, 1);
10495 processId(mapper, FIRST_TRACKING_ID);
10496 processPosition(mapper, 100, 200);
10497 processSync(mapper);
10498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10499 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10500 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10501 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10502 // TODO(b/257078296): Pointer mode generates extra event.
10503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10504 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10505 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10506 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10508
10509 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10510 // gesture should be disabled.
10511 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10512 viewport->isActive = false;
10513 mFakePolicy->updateViewport(*viewport);
10514 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10516 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10517 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10518 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10519 // TODO(b/257078296): Pointer mode generates extra event.
10520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10521 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10522 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10523 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10525}
10526
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010527// --- JoystickInputMapperTest ---
10528
10529class JoystickInputMapperTest : public InputMapperTest {
10530protected:
10531 static const int32_t RAW_X_MIN;
10532 static const int32_t RAW_X_MAX;
10533 static const int32_t RAW_Y_MIN;
10534 static const int32_t RAW_Y_MAX;
10535
10536 void SetUp() override {
10537 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10538 }
10539 void prepareAxes() {
10540 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10541 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10542 }
10543
10544 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10546 }
10547
10548 void processSync(JoystickInputMapper& mapper) {
10549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10550 }
10551
10552 void prepareVirtualDisplay(int32_t orientation) {
10553 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10554 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10555 NO_PORT, ViewportType::VIRTUAL);
10556 }
10557};
10558
10559const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10560const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10561const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10562const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10563
10564TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10565 prepareAxes();
10566 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10567
10568 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10569
10570 prepareVirtualDisplay(DISPLAY_ORIENTATION_0);
10571
10572 // Send an axis event
10573 processAxis(mapper, ABS_X, 100);
10574 processSync(mapper);
10575
10576 NotifyMotionArgs args;
10577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10578 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10579
10580 // Send another axis event
10581 processAxis(mapper, ABS_Y, 100);
10582 processSync(mapper);
10583
10584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10585 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10586}
10587
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010588// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010589
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010590class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010591protected:
10592 static const char* DEVICE_NAME;
10593 static const char* DEVICE_LOCATION;
10594 static const int32_t DEVICE_ID;
10595 static const int32_t DEVICE_GENERATION;
10596 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010597 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010598 static const int32_t EVENTHUB_ID;
10599
10600 std::shared_ptr<FakeEventHub> mFakeEventHub;
10601 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010602 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010603 std::unique_ptr<InstrumentedInputReader> mReader;
10604 std::shared_ptr<InputDevice> mDevice;
10605
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010606 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010607 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010608 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010609 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010610 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010611 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010612 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10613 }
10614
10615 void SetUp() override { SetUp(DEVICE_CLASSES); }
10616
10617 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010618 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010619 mFakePolicy.clear();
10620 }
10621
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010622 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010623 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10624 mReader->requestRefreshConfiguration(changes);
10625 mReader->loopOnce();
10626 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010627 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010628 }
10629
10630 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10631 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010632 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010633 InputDeviceIdentifier identifier;
10634 identifier.name = name;
10635 identifier.location = location;
10636 std::shared_ptr<InputDevice> device =
10637 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10638 identifier);
10639 mReader->pushNextDevice(device);
10640 mFakeEventHub->addDevice(eventHubId, name, classes);
10641 mReader->loopOnce();
10642 return device;
10643 }
10644
10645 template <class T, typename... Args>
10646 T& addControllerAndConfigure(Args... args) {
10647 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10648
10649 return controller;
10650 }
10651};
10652
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010653const char* PeripheralControllerTest::DEVICE_NAME = "device";
10654const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10655const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10656const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10657const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010658const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10659 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010660const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010661
10662// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010663class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010664protected:
10665 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010666 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010667 }
10668};
10669
10670TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010671 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010672
Harry Cuttsa5b71292022-11-28 12:56:17 +000010673 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10674 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10675 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010676}
10677
10678TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010679 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010680
Harry Cuttsa5b71292022-11-28 12:56:17 +000010681 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10682 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10683 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010684}
10685
10686// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010687class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010688protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010689 void SetUp() override {
10690 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10691 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010692};
10693
Chris Ye85758332021-05-16 23:05:17 -070010694TEST_F(LightControllerTest, MonoLight) {
10695 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010696 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010697 .maxBrightness = 255,
10698 .flags = InputLightClass::BRIGHTNESS,
10699 .path = ""};
10700 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010701
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010702 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010703 InputDeviceInfo info;
10704 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010705 std::vector<InputDeviceLightInfo> lights = info.getLights();
10706 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010707 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10708 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10709
10710 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10711 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10712}
10713
10714TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10715 RawLightInfo infoMono = {.id = 1,
10716 .name = "mono_keyboard_backlight",
10717 .maxBrightness = 255,
10718 .flags = InputLightClass::BRIGHTNESS |
10719 InputLightClass::KEYBOARD_BACKLIGHT,
10720 .path = ""};
10721 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10722
10723 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10724 InputDeviceInfo info;
10725 controller.populateDeviceInfo(&info);
10726 std::vector<InputDeviceLightInfo> lights = info.getLights();
10727 ASSERT_EQ(1U, lights.size());
10728 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10729 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010730
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010731 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10732 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010733}
10734
10735TEST_F(LightControllerTest, RGBLight) {
10736 RawLightInfo infoRed = {.id = 1,
10737 .name = "red",
10738 .maxBrightness = 255,
10739 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10740 .path = ""};
10741 RawLightInfo infoGreen = {.id = 2,
10742 .name = "green",
10743 .maxBrightness = 255,
10744 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10745 .path = ""};
10746 RawLightInfo infoBlue = {.id = 3,
10747 .name = "blue",
10748 .maxBrightness = 255,
10749 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10750 .path = ""};
10751 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10752 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10753 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10754
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010755 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010756 InputDeviceInfo info;
10757 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010758 std::vector<InputDeviceLightInfo> lights = info.getLights();
10759 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010760 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10761 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10762 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10763
10764 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10765 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10766}
10767
10768TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10769 RawLightInfo infoRed = {.id = 1,
10770 .name = "red_keyboard_backlight",
10771 .maxBrightness = 255,
10772 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10773 InputLightClass::KEYBOARD_BACKLIGHT,
10774 .path = ""};
10775 RawLightInfo infoGreen = {.id = 2,
10776 .name = "green_keyboard_backlight",
10777 .maxBrightness = 255,
10778 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10779 InputLightClass::KEYBOARD_BACKLIGHT,
10780 .path = ""};
10781 RawLightInfo infoBlue = {.id = 3,
10782 .name = "blue_keyboard_backlight",
10783 .maxBrightness = 255,
10784 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10785 InputLightClass::KEYBOARD_BACKLIGHT,
10786 .path = ""};
10787 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10788 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10789 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10790
10791 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10792 InputDeviceInfo info;
10793 controller.populateDeviceInfo(&info);
10794 std::vector<InputDeviceLightInfo> lights = info.getLights();
10795 ASSERT_EQ(1U, lights.size());
10796 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10797 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10798 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10799
10800 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10801 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10802}
10803
10804TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10805 RawLightInfo infoRed = {.id = 1,
10806 .name = "red",
10807 .maxBrightness = 255,
10808 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10809 .path = ""};
10810 RawLightInfo infoGreen = {.id = 2,
10811 .name = "green",
10812 .maxBrightness = 255,
10813 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10814 .path = ""};
10815 RawLightInfo infoBlue = {.id = 3,
10816 .name = "blue",
10817 .maxBrightness = 255,
10818 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10819 .path = ""};
10820 RawLightInfo infoGlobal = {.id = 3,
10821 .name = "global_keyboard_backlight",
10822 .maxBrightness = 255,
10823 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10824 InputLightClass::KEYBOARD_BACKLIGHT,
10825 .path = ""};
10826 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10827 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10828 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10829 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10830
10831 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10832 InputDeviceInfo info;
10833 controller.populateDeviceInfo(&info);
10834 std::vector<InputDeviceLightInfo> lights = info.getLights();
10835 ASSERT_EQ(1U, lights.size());
10836 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10837 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10838 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010839
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010840 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10841 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010842}
10843
10844TEST_F(LightControllerTest, MultiColorRGBLight) {
10845 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010846 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010847 .maxBrightness = 255,
10848 .flags = InputLightClass::BRIGHTNESS |
10849 InputLightClass::MULTI_INTENSITY |
10850 InputLightClass::MULTI_INDEX,
10851 .path = ""};
10852
10853 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10854
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010855 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010856 InputDeviceInfo info;
10857 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010858 std::vector<InputDeviceLightInfo> lights = info.getLights();
10859 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010860 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10861 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10862 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10863
10864 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10865 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10866}
10867
10868TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10869 RawLightInfo infoColor = {.id = 1,
10870 .name = "multi_color_keyboard_backlight",
10871 .maxBrightness = 255,
10872 .flags = InputLightClass::BRIGHTNESS |
10873 InputLightClass::MULTI_INTENSITY |
10874 InputLightClass::MULTI_INDEX |
10875 InputLightClass::KEYBOARD_BACKLIGHT,
10876 .path = ""};
10877
10878 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10879
10880 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10881 InputDeviceInfo info;
10882 controller.populateDeviceInfo(&info);
10883 std::vector<InputDeviceLightInfo> lights = info.getLights();
10884 ASSERT_EQ(1U, lights.size());
10885 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10886 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10887 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010888
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010889 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10890 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010891}
10892
10893TEST_F(LightControllerTest, PlayerIdLight) {
10894 RawLightInfo info1 = {.id = 1,
10895 .name = "player1",
10896 .maxBrightness = 255,
10897 .flags = InputLightClass::BRIGHTNESS,
10898 .path = ""};
10899 RawLightInfo info2 = {.id = 2,
10900 .name = "player2",
10901 .maxBrightness = 255,
10902 .flags = InputLightClass::BRIGHTNESS,
10903 .path = ""};
10904 RawLightInfo info3 = {.id = 3,
10905 .name = "player3",
10906 .maxBrightness = 255,
10907 .flags = InputLightClass::BRIGHTNESS,
10908 .path = ""};
10909 RawLightInfo info4 = {.id = 4,
10910 .name = "player4",
10911 .maxBrightness = 255,
10912 .flags = InputLightClass::BRIGHTNESS,
10913 .path = ""};
10914 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10915 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10916 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10917 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10918
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010919 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010920 InputDeviceInfo info;
10921 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010922 std::vector<InputDeviceLightInfo> lights = info.getLights();
10923 ASSERT_EQ(1U, lights.size());
10924 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010925 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10926 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010927
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010928 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10929 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10930 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010931}
10932
Michael Wrightd02c5b62014-02-10 15:10:22 -080010933} // namespace android