blob: e1b46faac6c741bd6bc2efd9c349d6bf222e26b1 [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>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000034#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TestInputListener.h>
36#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090040#include <com_android_input_flags.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000041#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050043#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000044#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070046#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000047#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000048#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000049#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000050#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000051#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000052#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000053#include "input/DisplayViewport.h"
54#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010055
Michael Wrightd02c5b62014-02-10 15:10:22 -080056namespace android {
57
Dominik Laskowski2f01d772022-03-23 16:01:29 -070058using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000059using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070060using std::chrono_literals::operator""ms;
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -080061using std::chrono_literals::operator""s;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070062
Michael Wrightd02c5b62014-02-10 15:10:22 -080063// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000067static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t DISPLAY_WIDTH = 480;
69static constexpr int32_t DISPLAY_HEIGHT = 800;
70static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
71static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
72static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070073static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070074static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080075
arthurhungcc7f9802020-04-30 17:55:40 +080076static constexpr int32_t FIRST_SLOT = 0;
77static constexpr int32_t SECOND_SLOT = 1;
78static constexpr int32_t THIRD_SLOT = 2;
79static constexpr int32_t INVALID_TRACKING_ID = -1;
80static constexpr int32_t FIRST_TRACKING_ID = 0;
81static constexpr int32_t SECOND_TRACKING_ID = 1;
82static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080083static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
84static constexpr int32_t LIGHT_COLOR = 0x7F448866;
85static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080086
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080087static constexpr int32_t ACTION_POINTER_0_DOWN =
88 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_0_UP =
90 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_DOWN =
92 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93static constexpr int32_t ACTION_POINTER_1_UP =
94 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
95
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000096static constexpr uint32_t STYLUS_FUSION_SOURCE =
97 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
98
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000099// Minimum timestamp separation between subsequent input events from a Bluetooth device.
100static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000101
Byoungho Jungda10dd32023-10-06 17:03:45 +0900102namespace input_flags = com::android::input::flags;
103
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104template<typename T>
105static inline T min(T a, T b) {
106 return a < b ? a : b;
107}
108
109static inline float avg(float x, float y) {
110 return (x + y) / 2;
111}
112
Chris Ye3fdbfef2021-01-06 18:45:18 -0800113// Mapping for light color name and the light color
114const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
115 {"green", LightColor::GREEN},
116 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
Michael Wrighta9cf4192022-12-01 23:46:39 +0000118static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700119 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000120 case ui::ROTATION_90:
121 return ui::ROTATION_270;
122 case ui::ROTATION_270:
123 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700124 default:
125 return orientation;
126 }
127}
128
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800129static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
130 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000131 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800132
133 const InputDeviceInfo::MotionRange* motionRange =
134 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
135 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
136}
137
138static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
139 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000140 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800141
142 const InputDeviceInfo::MotionRange* motionRange =
143 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
144 ASSERT_EQ(nullptr, motionRange);
145}
146
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700147[[maybe_unused]] static void dumpReader(InputReader& reader) {
148 std::string dump;
149 reader.dump(dump);
150 std::istringstream iss(dump);
151 for (std::string line; std::getline(iss, line);) {
152 ALOGE("%s", line.c_str());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -0800153 std::this_thread::sleep_for(1ms);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700154 }
155}
156
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157// --- FakeInputMapper ---
158
159class FakeInputMapper : public InputMapper {
160 uint32_t mSources;
161 int32_t mKeyboardType;
162 int32_t mMetaState;
163 KeyedVector<int32_t, int32_t> mKeyCodeStates;
164 KeyedVector<int32_t, int32_t> mScanCodeStates;
165 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100166 // fake mapping which would normally come from keyCharacterMap
167 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800168 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700169 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700171 std::mutex mLock;
172 std::condition_variable mStateChangedCondition;
173 bool mConfigureWasCalled GUARDED_BY(mLock);
174 bool mResetWasCalled GUARDED_BY(mLock);
175 bool mProcessWasCalled GUARDED_BY(mLock);
176 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177
Arthur Hungc23540e2018-11-29 20:42:11 +0800178 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000180 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
181 uint32_t sources)
182 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800183 mSources(sources),
184 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800186 mConfigureWasCalled(false),
187 mResetWasCalled(false),
188 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189
Chris Yea52ade12020-08-27 16:49:20 -0700190 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191
192 void setKeyboardType(int32_t keyboardType) {
193 mKeyboardType = keyboardType;
194 }
195
196 void setMetaState(int32_t metaState) {
197 mMetaState = metaState;
198 }
199
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700200 // Sets the return value for the `process` call.
201 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
202 mProcessResult.clear();
203 for (auto notifyArg : notifyArgs) {
204 mProcessResult.push_back(notifyArg);
205 }
206 }
207
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 std::unique_lock<std::mutex> lock(mLock);
210 base::ScopedLockAssertion assumeLocked(mLock);
211 const bool configureCalled =
212 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
213 return mConfigureWasCalled;
214 });
215 if (!configureCalled) {
216 FAIL() << "Expected configure() to have been called.";
217 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 mConfigureWasCalled = false;
219 }
220
221 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700222 std::unique_lock<std::mutex> lock(mLock);
223 base::ScopedLockAssertion assumeLocked(mLock);
224 const bool resetCalled =
225 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
226 return mResetWasCalled;
227 });
228 if (!resetCalled) {
229 FAIL() << "Expected reset() to have been called.";
230 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 mResetWasCalled = false;
232 }
233
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000234 void assertResetWasNotCalled() {
235 std::scoped_lock lock(mLock);
236 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
237 }
238
Yi Kong9b14ac62018-07-17 13:48:38 -0700239 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700240 std::unique_lock<std::mutex> lock(mLock);
241 base::ScopedLockAssertion assumeLocked(mLock);
242 const bool processCalled =
243 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
244 return mProcessWasCalled;
245 });
246 if (!processCalled) {
247 FAIL() << "Expected process() to have been called.";
248 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 if (outLastEvent) {
250 *outLastEvent = mLastEvent;
251 }
252 mProcessWasCalled = false;
253 }
254
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000255 void assertProcessWasNotCalled() {
256 std::scoped_lock lock(mLock);
257 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
258 }
259
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 void setKeyCodeState(int32_t keyCode, int32_t state) {
261 mKeyCodeStates.replaceValueFor(keyCode, state);
262 }
263
264 void setScanCodeState(int32_t scanCode, int32_t state) {
265 mScanCodeStates.replaceValueFor(scanCode, state);
266 }
267
268 void setSwitchState(int32_t switchCode, int32_t state) {
269 mSwitchStates.replaceValueFor(switchCode, state);
270 }
271
272 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800273 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274 }
275
Philip Junker4af3b3d2021-12-14 10:36:55 +0100276 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
277 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
278 }
279
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100281 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282
Harry Cuttsd02ea102023-03-17 18:21:30 +0000283 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 InputMapper::populateDeviceInfo(deviceInfo);
285
286 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000287 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288 }
289 }
290
Arpit Singhed6c3de2023-04-05 19:24:37 +0000291 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000292 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700293 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800295
296 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800297 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000298 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000299 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800300 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700301
302 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700303 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304 }
305
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700306 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700307 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700309 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700310 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311 }
312
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700313 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700314 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800315 mLastEvent = *rawEvent;
316 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700317 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700318 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319 }
320
Chris Yea52ade12020-08-27 16:49:20 -0700321 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
323 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
324 }
325
Philip Junker4af3b3d2021-12-14 10:36:55 +0100326 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
327 auto it = mKeyCodeMapping.find(locationKeyCode);
328 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
329 }
330
Chris Yea52ade12020-08-27 16:49:20 -0700331 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
333 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
334 }
335
Chris Yea52ade12020-08-27 16:49:20 -0700336 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 ssize_t index = mSwitchStates.indexOfKey(switchCode);
338 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
339 }
340
Chris Yea52ade12020-08-27 16:49:20 -0700341 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700342 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700343 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700344 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
346 if (keyCodes[i] == mSupportedKeyCodes[j]) {
347 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348 }
349 }
350 }
Chris Yea52ade12020-08-27 16:49:20 -0700351 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352 return result;
353 }
354
355 virtual int32_t getMetaState() {
356 return mMetaState;
357 }
358
359 virtual void fadePointer() {
360 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800361
362 virtual std::optional<int32_t> getAssociatedDisplay() {
363 if (mViewport) {
364 return std::make_optional(mViewport->displayId);
365 }
366 return std::nullopt;
367 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368};
369
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700370// --- InputReaderPolicyTest ---
371class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700372protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700373 sp<FakeInputReaderPolicy> mFakePolicy;
374
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700375 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700376 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700377};
378
379/**
380 * Check that empty set of viewports is an acceptable configuration.
381 * Also try to get internal viewport two different ways - by type and by uniqueId.
382 *
383 * There will be confusion if two viewports with empty uniqueId and identical type are present.
384 * Such configuration is not currently allowed.
385 */
386TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700387 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700388
389 // We didn't add any viewports yet, so there shouldn't be any.
390 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100391 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700392 ASSERT_FALSE(internalViewport);
393
394 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000395 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000396 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700397
398 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700399 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700400 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100401 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700402
403 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100404 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700405 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700406 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700407
408 mFakePolicy->clearViewports();
409 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700410 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700411 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100412 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700413 ASSERT_FALSE(internalViewport);
414}
415
416TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
417 const std::string internalUniqueId = "local:0";
418 const std::string externalUniqueId = "local:1";
419 const std::string virtualUniqueId1 = "virtual:2";
420 const std::string virtualUniqueId2 = "virtual:3";
421 constexpr int32_t virtualDisplayId1 = 2;
422 constexpr int32_t virtualDisplayId2 = 3;
423
424 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000425 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000426 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000427 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700428 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000429 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000430 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000431 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 // Add an virtual viewport
433 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000434 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000435 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700436 // Add another virtual viewport
437 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000438 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000439 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700440
441 // Check matching by type for internal
442 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100443 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700444 ASSERT_TRUE(internalViewport);
445 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
446
447 // Check matching by type for external
448 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100449 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700450 ASSERT_TRUE(externalViewport);
451 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
452
453 // Check matching by uniqueId for virtual viewport #1
454 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700455 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700456 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100457 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700458 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
459 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
460
461 // Check matching by uniqueId for virtual viewport #2
462 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700463 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700464 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100465 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700466 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
467 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
468}
469
470
471/**
472 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
473 * that lookup works by checking display id.
474 * Check that 2 viewports of each kind is possible, for all existing viewport types.
475 */
476TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
477 const std::string uniqueId1 = "uniqueId1";
478 const std::string uniqueId2 = "uniqueId2";
479 constexpr int32_t displayId1 = 2;
480 constexpr int32_t displayId2 = 3;
481
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100482 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
483 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700484 for (const ViewportType& type : types) {
485 mFakePolicy->clearViewports();
486 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000487 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000488 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700489 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000490 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000491 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700492
493 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700494 std::optional<DisplayViewport> viewport1 =
495 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700496 ASSERT_TRUE(viewport1);
497 ASSERT_EQ(displayId1, viewport1->displayId);
498 ASSERT_EQ(type, viewport1->type);
499
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700500 std::optional<DisplayViewport> viewport2 =
501 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700502 ASSERT_TRUE(viewport2);
503 ASSERT_EQ(displayId2, viewport2->displayId);
504 ASSERT_EQ(type, viewport2->type);
505
506 // When there are multiple viewports of the same kind, and uniqueId is not specified
507 // in the call to getDisplayViewport, then that situation is not supported.
508 // The viewports can be stored in any order, so we cannot rely on the order, since that
509 // is just implementation detail.
510 // However, we can check that it still returns *a* viewport, we just cannot assert
511 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700512 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700513 ASSERT_TRUE(someViewport);
514 }
515}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700517/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000518 * When we have multiple internal displays make sure we always return the default display when
519 * querying by type.
520 */
521TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
522 const std::string uniqueId1 = "uniqueId1";
523 const std::string uniqueId2 = "uniqueId2";
524 constexpr int32_t nonDefaultDisplayId = 2;
525 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
526 "Test display ID should not be ADISPLAY_ID_DEFAULT");
527
528 // Add the default display first and ensure it gets returned.
529 mFakePolicy->clearViewports();
530 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000531 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000532 ViewportType::INTERNAL);
533 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000534 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000535 ViewportType::INTERNAL);
536
537 std::optional<DisplayViewport> viewport =
538 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
539 ASSERT_TRUE(viewport);
540 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
541 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
542
543 // Add the default display second to make sure order doesn't matter.
544 mFakePolicy->clearViewports();
545 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000546 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000547 ViewportType::INTERNAL);
548 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000549 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000550 ViewportType::INTERNAL);
551
552 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
553 ASSERT_TRUE(viewport);
554 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
555 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
556}
557
558/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700559 * Check getDisplayViewportByPort
560 */
561TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100562 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700563 const std::string uniqueId1 = "uniqueId1";
564 const std::string uniqueId2 = "uniqueId2";
565 constexpr int32_t displayId1 = 1;
566 constexpr int32_t displayId2 = 2;
567 const uint8_t hdmi1 = 0;
568 const uint8_t hdmi2 = 1;
569 const uint8_t hdmi3 = 2;
570
571 mFakePolicy->clearViewports();
572 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000573 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000574 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700575 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000576 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000577 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700578
579 // Check that correct display viewport was returned by comparing the display ports.
580 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
581 ASSERT_TRUE(hdmi1Viewport);
582 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
583 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
584
585 // Check that we can still get the same viewport using the uniqueId
586 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
587 ASSERT_TRUE(hdmi1Viewport);
588 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
589 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
590 ASSERT_EQ(type, hdmi1Viewport->type);
591
592 // Check that we cannot find a port with "HDMI2", because we never added one
593 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
594 ASSERT_FALSE(hdmi2Viewport);
595}
596
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597// --- InputReaderTest ---
598
599class InputReaderTest : public testing::Test {
600protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700601 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700603 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000604 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605
Chris Yea52ade12020-08-27 16:49:20 -0700606 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700607 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700608 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700609 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610
Prabir Pradhan28efc192019-11-05 01:10:04 +0000611 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700612 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 }
614
Chris Yea52ade12020-08-27 16:49:20 -0700615 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700616 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 }
619
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700620 void addDevice(int32_t eventHubId, const std::string& name,
621 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800622 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623
624 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800625 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800626 }
627 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000628 mReader->loopOnce();
629 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700630 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700632 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633 }
634
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800635 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700636 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000637 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700638 }
639
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800640 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700641 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000642 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700643 }
644
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800645 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700646 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700647 ftl::Flags<InputDeviceClass> classes,
648 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800649 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800650 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000651 FakeInputMapper& mapper =
652 device->addMapper<FakeInputMapper>(eventHubId,
653 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800654 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800655 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 return mapper;
657 }
658};
659
Chris Ye98d3f532020-10-01 21:48:59 -0700660TEST_F(InputReaderTest, PolicyGetInputDevices) {
661 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700662 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700663 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664
665 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700666 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800668 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100669 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
671 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000672 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800673}
674
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000675TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
676 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
677 mFakeEventHub->setSysfsRootPath(1, "xyz");
678
679 // Should also have received a notification describing the new input device.
680 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
681 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
682 ASSERT_EQ(0U, inputDevice.getLights().size());
683
684 RawLightInfo infoMonolight = {.id = 123,
685 .name = "mono_keyboard_backlight",
686 .maxBrightness = 255,
687 .flags = InputLightClass::BRIGHTNESS,
688 .path = ""};
689 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
690 mReader->sysfsNodeChanged("xyz");
691 mReader->loopOnce();
692
693 // Should also have received a notification describing the new recreated input device.
694 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
695 inputDevice = mFakePolicy->getInputDevices()[0];
696 ASSERT_EQ(1U, inputDevice.getLights().size());
697}
698
Chris Yee7310032020-09-22 15:36:28 -0700699TEST_F(InputReaderTest, GetMergedInputDevices) {
700 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
701 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
702 // Add two subdevices to device
703 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
704 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000705 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
706 AINPUT_SOURCE_KEYBOARD);
707 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
708 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700709
710 // Push same device instance for next device to be added, so they'll have same identifier.
711 mReader->pushNextDevice(device);
712 mReader->pushNextDevice(device);
713 ASSERT_NO_FATAL_FAILURE(
714 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
715 ASSERT_NO_FATAL_FAILURE(
716 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
717
718 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000719 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700720}
721
Chris Yee14523a2020-12-19 13:46:00 -0800722TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
723 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
724 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
725 // Add two subdevices to device
726 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
727 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000728 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
729 AINPUT_SOURCE_KEYBOARD);
730 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
731 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800732
733 // Push same device instance for next device to be added, so they'll have same identifier.
734 mReader->pushNextDevice(device);
735 mReader->pushNextDevice(device);
736 // Sensor device is initially disabled
737 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
738 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
739 nullptr));
740 // Device is disabled because the only sub device is a sensor device and disabled initially.
741 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
742 ASSERT_FALSE(device->isEnabled());
743 ASSERT_NO_FATAL_FAILURE(
744 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
745 // The merged device is enabled if any sub device is enabled
746 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
747 ASSERT_TRUE(device->isEnabled());
748}
749
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700750TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800751 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700752 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800753 constexpr int32_t eventHubId = 1;
754 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000756 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
757 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800758 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800759 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760
Yi Kong9b14ac62018-07-17 13:48:38 -0700761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700762
763 NotifyDeviceResetArgs resetArgs;
764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700765 ASSERT_EQ(deviceId, resetArgs.deviceId);
766
767 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800768 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000769 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700770
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700772 ASSERT_EQ(deviceId, resetArgs.deviceId);
773 ASSERT_EQ(device->isEnabled(), false);
774
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800775 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000776 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700779 ASSERT_EQ(device->isEnabled(), false);
780
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800781 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000782 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700784 ASSERT_EQ(deviceId, resetArgs.deviceId);
785 ASSERT_EQ(device->isEnabled(), true);
786}
787
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800789 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700790 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800791 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800792 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800793 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800794 AINPUT_SOURCE_KEYBOARD, nullptr);
795 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796
797 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
798 AINPUT_SOURCE_ANY, AKEYCODE_A))
799 << "Should return unknown when the device id is >= 0 but unknown.";
800
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800801 ASSERT_EQ(AKEY_STATE_UNKNOWN,
802 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
803 << "Should return unknown when the device id is valid but the sources are not "
804 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800806 ASSERT_EQ(AKEY_STATE_DOWN,
807 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
808 AKEYCODE_A))
809 << "Should return value provided by mapper when device id is valid and the device "
810 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811
812 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
813 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
814 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
815
816 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
817 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
818 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
819}
820
Philip Junker4af3b3d2021-12-14 10:36:55 +0100821TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
822 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
823 constexpr int32_t eventHubId = 1;
824 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
825 InputDeviceClass::KEYBOARD,
826 AINPUT_SOURCE_KEYBOARD, nullptr);
827 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
828
829 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
830 << "Should return unknown when the device with the specified id is not found.";
831
832 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
833 << "Should return correct mapping when device id is valid and mapping exists.";
834
835 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
836 << "Should return the location key code when device id is valid and there's no "
837 "mapping.";
838}
839
840TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
841 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
842 constexpr int32_t eventHubId = 1;
843 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
844 InputDeviceClass::JOYSTICK,
845 AINPUT_SOURCE_GAMEPAD, nullptr);
846 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
847
848 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
849 << "Should return unknown when the device id is valid but there is no keyboard mapper";
850}
851
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800853 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700854 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800855 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800856 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800857 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800858 AINPUT_SOURCE_KEYBOARD, nullptr);
859 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
861 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
862 AINPUT_SOURCE_ANY, KEY_A))
863 << "Should return unknown when the device id is >= 0 but unknown.";
864
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800865 ASSERT_EQ(AKEY_STATE_UNKNOWN,
866 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
867 << "Should return unknown when the device id is valid but the sources are not "
868 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800870 ASSERT_EQ(AKEY_STATE_DOWN,
871 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
872 KEY_A))
873 << "Should return value provided by mapper when device id is valid and the device "
874 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875
876 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
877 AINPUT_SOURCE_TRACKBALL, KEY_A))
878 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
879
880 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
881 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
882 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
883}
884
885TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800886 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700887 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800888 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800889 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800890 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800891 AINPUT_SOURCE_KEYBOARD, nullptr);
892 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893
894 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
895 AINPUT_SOURCE_ANY, SW_LID))
896 << "Should return unknown when the device id is >= 0 but unknown.";
897
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800898 ASSERT_EQ(AKEY_STATE_UNKNOWN,
899 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
900 << "Should return unknown when the device id is valid but the sources are not "
901 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800903 ASSERT_EQ(AKEY_STATE_DOWN,
904 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
905 SW_LID))
906 << "Should return value provided by mapper when device id is valid and the device "
907 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908
909 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
910 AINPUT_SOURCE_TRACKBALL, SW_LID))
911 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
912
913 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
914 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
915 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
916}
917
918TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800919 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700920 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800921 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800922 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800923 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800924 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800926 mapper.addSupportedKeyCode(AKEYCODE_A);
927 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700929 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 uint8_t flags[4] = { 0, 0, 0, 1 };
931
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 << "Should return false when device id is >= 0 but unknown.";
934 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
935
936 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700937 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800938 << "Should return false when device id is valid but the sources are not supported by "
939 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
941
942 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700943 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800944 keyCodes, flags))
945 << "Should return value provided by mapper when device id is valid and the device "
946 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
948
949 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700950 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
951 << "Should return false when the device id is < 0 but the sources are not supported by "
952 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
954
955 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700956 ASSERT_TRUE(
957 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
958 << "Should return value provided by mapper when device id is < 0 and one of the "
959 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
961}
962
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000963TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800964 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700965 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966
967 NotifyConfigurationChangedArgs args;
968
969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
970 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
971}
972
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000973TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000976 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800977 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000978 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800979 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800980 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800981 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000983 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000984 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
986
987 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800988 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000989 ASSERT_EQ(when, event.when);
990 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800991 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992 ASSERT_EQ(EV_KEY, event.type);
993 ASSERT_EQ(KEY_A, event.code);
994 ASSERT_EQ(1, event.value);
995}
996
Garfield Tan1c7bc862020-01-28 13:24:04 -0800997TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800998 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700999 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001000 constexpr int32_t eventHubId = 1;
1001 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001002 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001003 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1004 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001005 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001006 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001007
1008 NotifyDeviceResetArgs resetArgs;
1009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001010 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001011
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001012 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001013 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001015 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001016 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001017
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001018 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001019 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001021 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001022 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001023
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001024 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001025 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001027 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001028 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001029}
1030
Garfield Tan1c7bc862020-01-28 13:24:04 -08001031TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1032 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001033 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001034 constexpr int32_t eventHubId = 1;
1035 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1036 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001037 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1038 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001039 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001040 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1041
1042 NotifyDeviceResetArgs resetArgs;
1043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1044 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1045}
1046
Arthur Hungc23540e2018-11-29 20:42:11 +08001047TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001048 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001049 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001050 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001051 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001052 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1053 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001054 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1055 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001056 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001057
1058 const uint8_t hdmi1 = 1;
1059
1060 // Associated touch screen with second display.
1061 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1062
1063 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001064 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001065 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001066 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001067 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001068 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001069 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001070 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001071 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001072
1073 // Add the device, and make sure all of the callbacks are triggered.
1074 // The device is added after the input port associations are processed since
1075 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001076 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001079 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001080
Arthur Hung2c9a3342019-07-23 14:18:59 +08001081 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001082 ASSERT_EQ(deviceId, device->getId());
1083 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1084 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001085
1086 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001087 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001088 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001089 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001090}
1091
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001092TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1093 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001094 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001095 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1096 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1097 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001098 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1099 AINPUT_SOURCE_KEYBOARD);
1100 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1101 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001102 mReader->pushNextDevice(device);
1103 mReader->pushNextDevice(device);
1104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1105 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1106
1107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1108
1109 NotifyDeviceResetArgs resetArgs;
1110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1111 ASSERT_EQ(deviceId, resetArgs.deviceId);
1112 ASSERT_TRUE(device->isEnabled());
1113 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1114 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1115
1116 disableDevice(deviceId);
1117 mReader->loopOnce();
1118
1119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1120 ASSERT_EQ(deviceId, resetArgs.deviceId);
1121 ASSERT_FALSE(device->isEnabled());
1122 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1123 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1124
1125 enableDevice(deviceId);
1126 mReader->loopOnce();
1127
1128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1129 ASSERT_EQ(deviceId, resetArgs.deviceId);
1130 ASSERT_TRUE(device->isEnabled());
1131 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1132 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1133}
1134
1135TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1136 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001137 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001138 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1139 // Add two subdevices to device
1140 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1141 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001142 device->addMapper<FakeInputMapper>(eventHubIds[0],
1143 mFakePolicy->getReaderConfiguration(),
1144 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001145 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001146 device->addMapper<FakeInputMapper>(eventHubIds[1],
1147 mFakePolicy->getReaderConfiguration(),
1148 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001149 mReader->pushNextDevice(device);
1150 mReader->pushNextDevice(device);
1151 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1152 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1153
1154 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1155 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1156
1157 ASSERT_EQ(AKEY_STATE_DOWN,
1158 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1159 ASSERT_EQ(AKEY_STATE_DOWN,
1160 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1161 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1162 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1163}
1164
Prabir Pradhan7e186182020-11-10 13:56:45 -08001165TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1166 NotifyPointerCaptureChangedArgs args;
1167
Hiroki Sato25040232024-02-22 17:21:22 +09001168 auto request = mFakePolicy->setPointerCapture(/*window=*/sp<BBinder>::make());
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001169 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001170 mReader->loopOnce();
1171 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001172 ASSERT_TRUE(args.request.isEnable()) << "Pointer Capture should be enabled.";
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001173 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001174
Hiroki Sato25040232024-02-22 17:21:22 +09001175 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001176 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001177 mReader->loopOnce();
1178 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001179 ASSERT_FALSE(args.request.isEnable()) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001180
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001181 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001182 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001183 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001184 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001185 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001186}
1187
Chris Ye87143712020-11-10 05:05:58 +00001188class FakeVibratorInputMapper : public FakeInputMapper {
1189public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001190 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1191 const InputReaderConfiguration& readerConfig, uint32_t sources)
1192 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001193
1194 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1195};
1196
1197TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1198 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001199 ftl::Flags<InputDeviceClass> deviceClass =
1200 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001201 constexpr int32_t eventHubId = 1;
1202 const char* DEVICE_LOCATION = "BLUETOOTH";
1203 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1204 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001205 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1206 mFakePolicy->getReaderConfiguration(),
1207 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001208 mReader->pushNextDevice(device);
1209
1210 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1211 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1212
1213 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1214 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1215}
1216
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001217// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001218
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001219class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001220public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001221 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001222
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001223 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001224
Andy Chenf9f1a022022-08-29 20:07:10 -04001225 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1226
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1228
1229 void dump(std::string& dump) override {}
1230
1231 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1232 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001233 }
1234
Chris Yee2b1e5c2021-03-10 22:45:12 -08001235 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1236 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001237 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001238
1239 bool setLightColor(int32_t lightId, int32_t color) override {
1240 getDeviceContext().setLightBrightness(lightId, color >> 24);
1241 return true;
1242 }
1243
1244 std::optional<int32_t> getLightColor(int32_t lightId) override {
1245 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1246 if (!result.has_value()) {
1247 return std::nullopt;
1248 }
1249 return result.value() << 24;
1250 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001251
1252 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1253
1254 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1255
1256private:
1257 InputDeviceContext& mDeviceContext;
1258 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1259 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001260 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001261};
1262
Chris Yee2b1e5c2021-03-10 22:45:12 -08001263TEST_F(InputReaderTest, BatteryGetCapacity) {
1264 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001265 ftl::Flags<InputDeviceClass> deviceClass =
1266 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001267 constexpr int32_t eventHubId = 1;
1268 const char* DEVICE_LOCATION = "BLUETOOTH";
1269 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001270 FakePeripheralController& controller =
1271 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001272 mReader->pushNextDevice(device);
1273
1274 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1275
Harry Cuttsa5b71292022-11-28 12:56:17 +00001276 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1277 FakeEventHub::BATTERY_CAPACITY);
1278 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001279}
1280
1281TEST_F(InputReaderTest, BatteryGetStatus) {
1282 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001283 ftl::Flags<InputDeviceClass> deviceClass =
1284 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001285 constexpr int32_t eventHubId = 1;
1286 const char* DEVICE_LOCATION = "BLUETOOTH";
1287 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001288 FakePeripheralController& controller =
1289 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001290 mReader->pushNextDevice(device);
1291
1292 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1293
Harry Cuttsa5b71292022-11-28 12:56:17 +00001294 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1295 FakeEventHub::BATTERY_STATUS);
1296 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001297}
1298
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001299TEST_F(InputReaderTest, BatteryGetDevicePath) {
1300 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1301 ftl::Flags<InputDeviceClass> deviceClass =
1302 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1303 constexpr int32_t eventHubId = 1;
1304 const char* DEVICE_LOCATION = "BLUETOOTH";
1305 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1306 device->addController<FakePeripheralController>(eventHubId);
1307 mReader->pushNextDevice(device);
1308
1309 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1310
Harry Cuttsa5b71292022-11-28 12:56:17 +00001311 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001312}
1313
Chris Ye3fdbfef2021-01-06 18:45:18 -08001314TEST_F(InputReaderTest, LightGetColor) {
1315 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001316 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001317 constexpr int32_t eventHubId = 1;
1318 const char* DEVICE_LOCATION = "BLUETOOTH";
1319 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001320 FakePeripheralController& controller =
1321 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001322 mReader->pushNextDevice(device);
1323 RawLightInfo info = {.id = 1,
1324 .name = "Mono",
1325 .maxBrightness = 255,
1326 .flags = InputLightClass::BRIGHTNESS,
1327 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001328 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1329 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001330
1331 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001332
Harry Cutts33476232023-01-30 19:57:29 +00001333 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1334 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1335 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1336 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001337}
1338
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339// --- InputReaderIntegrationTest ---
1340
1341// These tests create and interact with the InputReader only through its interface.
1342// The InputReader is started during SetUp(), which starts its processing in its own
1343// thread. The tests use linux uinput to emulate input devices.
1344// NOTE: Interacting with the physical device while these tests are running may cause
1345// the tests to fail.
1346class InputReaderIntegrationTest : public testing::Test {
1347protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001348 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001349 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001350 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001351
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001352 std::shared_ptr<FakePointerController> mFakePointerController;
1353
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001354 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1355 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1356
Chris Yea52ade12020-08-27 16:49:20 -07001357 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001358#if !defined(__ANDROID__)
1359 GTEST_SKIP();
1360#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001361 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001362 mFakePointerController = std::make_shared<FakePointerController>();
1363 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001364
Arpit Singh440bf652023-08-09 09:23:43 +00001365 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001366 }
1367
Chris Yea52ade12020-08-27 16:49:20 -07001368 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001369#if !defined(__ANDROID__)
1370 return;
1371#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001372 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001373 mReader.reset();
1374 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001375 mFakePolicy.clear();
1376 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001377
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001378 std::optional<InputDeviceInfo> waitForDevice(const std::string& deviceName) {
1379 std::chrono::time_point start = std::chrono::steady_clock::now();
1380 while (true) {
1381 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1382 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1383 [&deviceName](const InputDeviceInfo& info) {
1384 return info.getIdentifier().name == deviceName;
1385 });
1386 if (it != inputDevices.end()) {
1387 return std::make_optional(*it);
1388 }
1389 std::this_thread::sleep_for(1ms);
1390 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
1391 if (elapsed > 5s) {
1392 return {};
1393 }
1394 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001395 }
Arpit Singh440bf652023-08-09 09:23:43 +00001396
1397 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001398 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1399 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001400
1401 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1402 *mTestListener);
1403 ASSERT_EQ(mReader->start(), OK);
1404
1405 // Since this test is run on a real device, all the input devices connected
1406 // to the test device will show up in mReader. We wait for those input devices to
1407 // show up before beginning the tests.
1408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1410 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1411 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001412};
1413
1414TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1415 // An invalid input device that is only used for this test.
1416 class InvalidUinputDevice : public UinputDevice {
1417 public:
Harry Cutts33476232023-01-30 19:57:29 +00001418 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001419
1420 private:
1421 void configureDevice(int fd, uinput_user_dev* device) override {}
1422 };
1423
1424 const size_t numDevices = mFakePolicy->getInputDevices().size();
1425
1426 // UinputDevice does not set any event or key bits, so InputReader should not
1427 // consider it as a valid device.
1428 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1429 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1431 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1432
1433 invalidDevice.reset();
1434 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1435 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1436 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1437}
1438
1439TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1440 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1441
1442 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1443 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1445 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1446
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001447 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001448 ASSERT_TRUE(device.has_value());
1449 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1450 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1451 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001452
1453 keyboard.reset();
1454 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1455 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1456 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1457}
1458
1459TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1460 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1461 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1462
1463 NotifyConfigurationChangedArgs configChangedArgs;
1464 ASSERT_NO_FATAL_FAILURE(
1465 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001466 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001467 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1468
1469 NotifyKeyArgs keyArgs;
1470 keyboard->pressAndReleaseHomeKey();
1471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1472 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001473 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001474 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001475 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001476 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001477 prevTimestamp = keyArgs.eventTime;
1478
1479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1480 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001481 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001482 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001483 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001484}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001485
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001486TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1487 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1488 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1489
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001490 const auto device = waitForDevice(stylus->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001491 ASSERT_TRUE(device.has_value());
1492
Prabir Pradhana3621852022-10-14 18:57:23 +00001493 // An external stylus with buttons should also be recognized as a keyboard.
1494 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001495 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1496 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1497
1498 const auto DOWN =
1499 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1500 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1501
1502 stylus->pressAndReleaseKey(BTN_STYLUS);
1503 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1504 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1505 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1506 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1507
1508 stylus->pressAndReleaseKey(BTN_STYLUS2);
1509 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1510 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1511 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1512 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1513
1514 stylus->pressAndReleaseKey(BTN_STYLUS3);
1515 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1516 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1518 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1519}
1520
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001521TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1522 std::unique_ptr<UinputKeyboard> keyboard =
1523 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1524 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1525 KEY_R, KEY_T, KEY_Y,
1526 BTN_STYLUS, BTN_STYLUS2,
1527 BTN_STYLUS3});
1528 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1529
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001530 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001531 ASSERT_TRUE(device.has_value());
1532
1533 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1534 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1535 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1536 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1537}
1538
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001539TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1540 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1541 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1542 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1543 createUinputDevice<UinputKeyboardWithHidUsage>(
1544 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1545 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1546
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001547 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001548 ASSERT_TRUE(device.has_value());
1549
1550 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1551 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1552
1553 // If a device supports reporting HID usage codes, it shouldn't automatically support
1554 // stylus keys.
1555 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1556 uint8_t outFlags[] = {0};
1557 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1558 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1559}
1560
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001561/**
1562 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1563 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1564 * are passed to the listener.
1565 */
1566static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1567TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1568 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1569 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1570 NotifyKeyArgs keyArgs;
1571
1572 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1573 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1574 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1575 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1576
1577 controller->pressAndReleaseKey(BTN_GEAR_UP);
1578 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1579 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1580 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1581}
1582
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001583// --- TouchIntegrationTest ---
1584
Arpit Singh440bf652023-08-09 09:23:43 +00001585class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001586protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001587 const std::string UNIQUE_ID = "local:0";
1588
Chris Yea52ade12020-08-27 16:49:20 -07001589 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001590#if !defined(__ANDROID__)
1591 GTEST_SKIP();
1592#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001593 InputReaderIntegrationTest::SetUp();
1594 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001595 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1596 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001597
1598 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1599 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1600 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001601 const auto info = waitForDevice(mDevice->getName());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001602 ASSERT_TRUE(info);
1603 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001604 }
1605
1606 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001607 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001608 std::optional<uint8_t> physicalPort,
1609 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001610 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001611 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001612 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001613 }
1614
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001615 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1616 NotifyMotionArgs args;
1617 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1618 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001619 ASSERT_EQ(points.size(), args.getPointerCount());
1620 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001621 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1622 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1623 }
1624 }
1625
Arthur Hungaab25622020-01-16 11:22:11 +08001626 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001627 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001628};
1629
Arpit Singh440bf652023-08-09 09:23:43 +00001630enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1631
1632class TouchIntegrationTest : public BaseTouchIntegrationTest,
1633 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1634protected:
1635 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1636 const std::string INPUT_PORT = "uinput_touch/input0";
1637
1638 void SetUp() override {
1639#if !defined(__ANDROID__)
1640 GTEST_SKIP();
1641#endif
1642 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1643 BaseTouchIntegrationTest::SetUp();
1644 return;
1645 }
1646
1647 // setup policy with a input-port or UniqueId association to the display
1648 bool isInputPortAssociation =
1649 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1650
1651 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1652 if (isInputPortAssociation) {
1653 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1654 } else {
1655 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1656 }
1657 mFakePointerController = std::make_shared<FakePointerController>();
1658 mFakePolicy->setPointerController(mFakePointerController);
1659
1660 InputReaderIntegrationTest::setupInputReader();
1661
1662 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1663 INPUT_PORT);
1664 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1665
1666 // Add a display linked to a physical port or UniqueId.
1667 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1668 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1669 ViewportType::INTERNAL);
1670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1671 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001672 const auto info = waitForDevice(mDevice->getName());
Arpit Singh440bf652023-08-09 09:23:43 +00001673 ASSERT_TRUE(info);
1674 mDeviceInfo = *info;
1675 }
1676};
1677
1678TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001679 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1680 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1681 // presses).
1682 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1683 mDeviceInfo.getSources());
1684}
1685
Arpit Singh440bf652023-08-09 09:23:43 +00001686TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001687 NotifyMotionArgs args;
1688 const Point centerPoint = mDevice->getCenterPoint();
1689
1690 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001691 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001692 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001693 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001694 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1695 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1696
1697 // ACTION_MOVE
1698 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001699 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001700 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1702
1703 // ACTION_UP
1704 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001705 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001706 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1707 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1708}
1709
Arpit Singh440bf652023-08-09 09:23:43 +00001710TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001711 NotifyMotionArgs args;
1712 const Point centerPoint = mDevice->getCenterPoint();
1713
1714 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001715 mDevice->sendSlot(FIRST_SLOT);
1716 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001717 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001718 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001719 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1720 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1721
1722 // ACTION_POINTER_DOWN (Second slot)
1723 const Point secondPoint = centerPoint + Point(100, 100);
1724 mDevice->sendSlot(SECOND_SLOT);
1725 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001726 mDevice->sendDown(secondPoint);
1727 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001728 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001729 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001730
1731 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001732 mDevice->sendMove(secondPoint + Point(1, 1));
1733 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001734 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1736
1737 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001738 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001739 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001740 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001741 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001742
1743 // ACTION_UP
1744 mDevice->sendSlot(FIRST_SLOT);
1745 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001746 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001747 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1748 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1749}
1750
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001751/**
1752 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1753 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1754 * data?
1755 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1756 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1757 * for Pointer 0 only is generated after.
1758 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1759 * events, we will not miss any information.
1760 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1761 * event generated afterwards that contains the newest movement of pointer 0.
1762 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1763 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1764 * losing information about non-palm pointers.
1765 */
Arpit Singh440bf652023-08-09 09:23:43 +00001766TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001767 NotifyMotionArgs args;
1768 const Point centerPoint = mDevice->getCenterPoint();
1769
1770 // ACTION_DOWN
1771 mDevice->sendSlot(FIRST_SLOT);
1772 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1773 mDevice->sendDown(centerPoint);
1774 mDevice->sendSync();
1775 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1776
1777 // ACTION_POINTER_DOWN (Second slot)
1778 const Point secondPoint = centerPoint + Point(100, 100);
1779 mDevice->sendSlot(SECOND_SLOT);
1780 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1781 mDevice->sendDown(secondPoint);
1782 mDevice->sendSync();
1783 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1784
1785 // ACTION_MOVE (First slot)
1786 mDevice->sendSlot(FIRST_SLOT);
1787 mDevice->sendMove(centerPoint + Point(5, 5));
1788 // ACTION_POINTER_UP (Second slot)
1789 mDevice->sendSlot(SECOND_SLOT);
1790 mDevice->sendPointerUp();
1791 // Send a single sync for the above 2 pointer updates
1792 mDevice->sendSync();
1793
1794 // First, we should get POINTER_UP for the second pointer
1795 assertReceivedMotion(ACTION_POINTER_1_UP,
1796 {/*first pointer */ centerPoint + Point(5, 5),
1797 /*second pointer*/ secondPoint});
1798
1799 // Next, the MOVE event for the first pointer
1800 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1801}
1802
1803/**
1804 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1805 * move, and then it will go up, all in the same frame.
1806 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1807 * gets sent to the listener.
1808 */
Arpit Singh440bf652023-08-09 09:23:43 +00001809TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001810 NotifyMotionArgs args;
1811 const Point centerPoint = mDevice->getCenterPoint();
1812
1813 // ACTION_DOWN
1814 mDevice->sendSlot(FIRST_SLOT);
1815 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1816 mDevice->sendDown(centerPoint);
1817 mDevice->sendSync();
1818 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1819
1820 // ACTION_POINTER_DOWN (Second slot)
1821 const Point secondPoint = centerPoint + Point(100, 100);
1822 mDevice->sendSlot(SECOND_SLOT);
1823 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1824 mDevice->sendDown(secondPoint);
1825 mDevice->sendSync();
1826 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1827
1828 // ACTION_MOVE (First slot)
1829 mDevice->sendSlot(FIRST_SLOT);
1830 mDevice->sendMove(centerPoint + Point(5, 5));
1831 // ACTION_POINTER_UP (Second slot)
1832 mDevice->sendSlot(SECOND_SLOT);
1833 mDevice->sendMove(secondPoint + Point(6, 6));
1834 mDevice->sendPointerUp();
1835 // Send a single sync for the above 2 pointer updates
1836 mDevice->sendSync();
1837
1838 // First, we should get POINTER_UP for the second pointer
1839 // The movement of the second pointer during the liftoff frame is ignored.
1840 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1841 assertReceivedMotion(ACTION_POINTER_1_UP,
1842 {/*first pointer */ centerPoint + Point(5, 5),
1843 /*second pointer*/ secondPoint});
1844
1845 // Next, the MOVE event for the first pointer
1846 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1847}
1848
Arpit Singh440bf652023-08-09 09:23:43 +00001849TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001850 NotifyMotionArgs args;
1851 const Point centerPoint = mDevice->getCenterPoint();
1852
1853 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001854 mDevice->sendSlot(FIRST_SLOT);
1855 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001856 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001857 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001858 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1859 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1860
arthurhungcc7f9802020-04-30 17:55:40 +08001861 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001862 const Point secondPoint = centerPoint + Point(100, 100);
1863 mDevice->sendSlot(SECOND_SLOT);
1864 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1865 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001866 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001867 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001868 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001869
arthurhungcc7f9802020-04-30 17:55:40 +08001870 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001871 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001872 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001873 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1875
arthurhungcc7f9802020-04-30 17:55:40 +08001876 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1877 // a palm event.
1878 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001879 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001880 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001881 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001882 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001883 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001884
arthurhungcc7f9802020-04-30 17:55:40 +08001885 // Send up to second slot, expect first slot send moving.
1886 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001887 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001888 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001890
arthurhungcc7f9802020-04-30 17:55:40 +08001891 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001892 mDevice->sendSlot(FIRST_SLOT);
1893 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001894 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001895
arthurhungcc7f9802020-04-30 17:55:40 +08001896 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1897 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001898}
1899
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001900/**
1901 * Some drivers historically have reported axis values outside of the range specified in the
1902 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1903 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1904 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1905 * and no units for pressure (resolution) is specified by the evdev documentation.
1906 */
1907TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1908 const Point centerPoint = mDevice->getCenterPoint();
1909
1910 // Down with pressure outside the reported range
1911 mDevice->sendSlot(FIRST_SLOT);
1912 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1913 mDevice->sendDown(centerPoint);
1914 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1915 mDevice->sendSync();
1916 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1917 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1918
1919 // Move to a point outside the reported range
1920 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1921 mDevice->sendSync();
1922 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1923 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1924
1925 // Up
1926 mDevice->sendUp();
1927 mDevice->sendSync();
1928 ASSERT_NO_FATAL_FAILURE(
1929 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1930}
1931
Arpit Singh440bf652023-08-09 09:23:43 +00001932TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001933 const Point centerPoint = mDevice->getCenterPoint();
1934
1935 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1936 mDevice->sendSlot(FIRST_SLOT);
1937 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1938 mDevice->sendToolType(MT_TOOL_PEN);
1939 mDevice->sendDown(centerPoint);
1940 mDevice->sendSync();
1941 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1942 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001943 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001944
1945 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1946
1947 // Release the stylus touch.
1948 mDevice->sendUp();
1949 mDevice->sendSync();
1950 ASSERT_NO_FATAL_FAILURE(
1951 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1952
1953 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1954
1955 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1956 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1957 mDevice->sendToolType(MT_TOOL_FINGER);
1958 mDevice->sendDown(centerPoint);
1959 mDevice->sendSync();
1960 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1961 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001962 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001963
1964 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1965
1966 mDevice->sendUp();
1967 mDevice->sendSync();
1968 ASSERT_NO_FATAL_FAILURE(
1969 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1970
1971 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1972 // The policy should be notified of the stylus presence.
1973 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1974 mDevice->sendToolType(MT_TOOL_PEN);
1975 mDevice->sendMove(centerPoint);
1976 mDevice->sendSync();
1977 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1978 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001979 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001980
1981 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1982}
1983
Arpit Singh440bf652023-08-09 09:23:43 +00001984TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001985 const Point centerPoint = mDevice->getCenterPoint();
1986
1987 // Down
1988 mDevice->sendSlot(FIRST_SLOT);
1989 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1990 mDevice->sendDown(centerPoint);
1991 mDevice->sendSync();
1992 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1993 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1994
1995 // Move
1996 mDevice->sendMove(centerPoint + Point(1, 1));
1997 mDevice->sendSync();
1998 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1999 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2000
2001 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2002 auto externalStylus = createUinputDevice<UinputExternalStylus>();
2003 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2004 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002005 const auto stylusInfo = waitForDevice(externalStylus->getName());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002006 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002007
2008 // Move
2009 mDevice->sendMove(centerPoint + Point(2, 2));
2010 mDevice->sendSync();
2011 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2012 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2013
2014 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2015 externalStylus.reset();
2016 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2017 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2018 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2019
2020 // Up
2021 mDevice->sendUp();
2022 mDevice->sendSync();
2023 ASSERT_NO_FATAL_FAILURE(
2024 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2025
2026 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2027}
2028
Arpit Singh440bf652023-08-09 09:23:43 +00002029INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2030 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2031 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2032 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2033
Prabir Pradhan124ea442022-10-28 20:27:44 +00002034// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002035
Prabir Pradhan124ea442022-10-28 20:27:44 +00002036// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2037// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2038// stylus.
2039template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002040class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002041protected:
2042 void SetUp() override {
2043#if !defined(__ANDROID__)
2044 GTEST_SKIP();
2045#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002046 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002047 mTouchscreen = mDevice.get();
2048 mTouchscreenInfo = mDeviceInfo;
2049
2050 setUpStylusDevice();
2051 }
2052
2053 UinputStylusDevice* mStylus{nullptr};
2054 InputDeviceInfo mStylusInfo{};
2055
2056 UinputTouchScreen* mTouchscreen{nullptr};
2057 InputDeviceInfo mTouchscreenInfo{};
2058
2059private:
2060 // When we are attempting to test stylus button events that are sent from the touchscreen,
2061 // use the same Uinput device for the touchscreen and the stylus.
2062 template <typename T = UinputStylusDevice>
2063 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2064 mStylus = mDevice.get();
2065 mStylusInfo = mDeviceInfo;
2066 }
2067
2068 // When we are attempting to stylus buttons from an external stylus being merged with touches
2069 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2070 template <typename T = UinputStylusDevice>
2071 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2072 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2073 mStylus = mStylusDeviceLifecycleTracker.get();
2074 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002076 const auto info = waitForDevice(mStylus->getName());
Prabir Pradhan124ea442022-10-28 20:27:44 +00002077 ASSERT_TRUE(info);
2078 mStylusInfo = *info;
2079 }
2080
2081 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2082
2083 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002084 using BaseTouchIntegrationTest::mDevice;
2085 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002086};
2087
2088using StylusButtonIntegrationTestTypes =
2089 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2090TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2091
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002092TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002093 const auto stylusId = TestFixture::mStylusInfo.getId();
2094
2095 TestFixture::mStylus->pressKey(BTN_STYLUS);
2096 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2097 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2098 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2099
2100 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2101 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002102 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002103 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002104}
2105
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002106TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002107 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2108 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2109 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002110
2111 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002112 TestFixture::mStylus->pressKey(BTN_STYLUS);
2113 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002114 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002115 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002116
2117 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002118 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2119 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2120 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2121 TestFixture::mTouchscreen->sendDown(centerPoint);
2122 TestFixture::mTouchscreen->sendSync();
2123 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002124 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002125 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002126 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2127 WithDeviceId(touchscreenId))));
2128 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002129 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002130 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002131 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2132 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002133
Prabir Pradhan124ea442022-10-28 20:27:44 +00002134 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2135 TestFixture::mTouchscreen->sendSync();
2136 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002137 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002138 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002139 WithDeviceId(touchscreenId))));
2140 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002141 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002142 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002143 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002144
2145 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002146 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2147 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002148 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002149 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002150}
2151
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002152TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002153 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2154 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2155 const auto stylusId = TestFixture::mStylusInfo.getId();
2156 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002157 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002158
2159 // Press the stylus button.
2160 TestFixture::mStylus->pressKey(BTN_STYLUS);
2161 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2162 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2163 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2164
2165 // Start hovering with the stylus.
2166 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2167 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2168 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2169 TestFixture::mTouchscreen->sendMove(centerPoint);
2170 TestFixture::mTouchscreen->sendSync();
2171 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2172 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2173 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2174 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2175 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2176 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2177 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2178 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2179 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2180
2181 // Touch down with the stylus.
2182 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2183 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2184 TestFixture::mTouchscreen->sendDown(centerPoint);
2185 TestFixture::mTouchscreen->sendSync();
2186 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2187 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2188 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2189
2190 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2191 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2192 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2193
2194 // Stop touching with the stylus, and start hovering.
2195 TestFixture::mTouchscreen->sendUp();
2196 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2197 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2198 TestFixture::mTouchscreen->sendMove(centerPoint);
2199 TestFixture::mTouchscreen->sendSync();
2200 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2201 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2202 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2203 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2204 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2205 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2206 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2207 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2208 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2209
2210 // Stop hovering.
2211 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2212 TestFixture::mTouchscreen->sendSync();
2213 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2214 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2215 WithButtonState(0))));
2216 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2217 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2218 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2219 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2220
2221 // Release the stylus button.
2222 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2223 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2224 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2225 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2226}
2227
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002228TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002229 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2230 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2231 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002232
2233 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002234 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2235 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2236 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2237 TestFixture::mTouchscreen->sendDown(centerPoint);
2238 TestFixture::mTouchscreen->sendSync();
2239 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002240 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002241 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002242 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002243
2244 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002245 TestFixture::mStylus->pressKey(BTN_STYLUS);
2246 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002247 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002248 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2249 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002250 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002251 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002252 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2253 WithDeviceId(touchscreenId))));
2254 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002255 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002256 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002257 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2258 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002259
Prabir Pradhan124ea442022-10-28 20:27:44 +00002260 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2261 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002262 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002263 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2264 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002265 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002266 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002267 WithDeviceId(touchscreenId))));
2268 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002269 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002270 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002271 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002272
2273 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002274 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2275 TestFixture::mTouchscreen->sendSync();
2276 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002277 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002278 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002279 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002280}
2281
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002282TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002283 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2284 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002285 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002286
2287 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2288 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2289 const auto stylusId = TestFixture::mStylusInfo.getId();
2290
2291 // Start a stylus gesture. By the time this event is processed, the configuration change that
2292 // was requested is guaranteed to be completed.
2293 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2294 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2295 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2296 TestFixture::mTouchscreen->sendDown(centerPoint);
2297 TestFixture::mTouchscreen->sendSync();
2298 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2299 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002300 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002301 WithDeviceId(touchscreenId))));
2302
2303 // Press and release a stylus button. Each change only generates a MOVE motion event.
2304 // Key events are unaffected.
2305 TestFixture::mStylus->pressKey(BTN_STYLUS);
2306 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2307 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2308 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2309 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2310 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002311 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002312 WithDeviceId(touchscreenId))));
2313
2314 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2315 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2316 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2317 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2318 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2319 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002320 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002321 WithDeviceId(touchscreenId))));
2322
2323 // Finish the stylus gesture.
2324 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2325 TestFixture::mTouchscreen->sendSync();
2326 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2327 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002328 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002329 WithDeviceId(touchscreenId))));
2330}
2331
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002332// --- ExternalStylusIntegrationTest ---
2333
2334// Verify the behavior of an external stylus. An external stylus can report pressure or button
2335// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2336// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002337using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002338
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002339TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2340 // Create an external stylus capable of reporting pressure data that
2341 // should be fused with a touch pointer.
2342 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2343 createUinputDevice<UinputExternalStylusWithPressure>();
2344 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2345 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002346 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002347 ASSERT_TRUE(stylusInfo);
2348
2349 // Connecting an external stylus changes the source of the touchscreen.
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002350 const auto deviceInfo = waitForDevice(mDevice->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002351 ASSERT_TRUE(deviceInfo);
2352 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2353}
2354
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002355TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002356 const Point centerPoint = mDevice->getCenterPoint();
2357
2358 // Create an external stylus capable of reporting pressure data that
2359 // should be fused with a touch pointer.
2360 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2361 createUinputDevice<UinputExternalStylusWithPressure>();
2362 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2363 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002364 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002365 ASSERT_TRUE(stylusInfo);
2366
2367 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2368
2369 const auto touchscreenId = mDeviceInfo.getId();
2370
2371 // Set a pressure value on the stylus. It doesn't generate any events.
2372 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2373 stylus->setPressure(100);
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2375
2376 // Start a finger gesture, and ensure it shows up as stylus gesture
2377 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002378 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002379 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002380 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002381 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382 mDevice->sendSync();
2383 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002384 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2385 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2386 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002387
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002388 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2389 // event with the updated pressure.
2390 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002392 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2393 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2394 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002395
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002396 // The external stylus did not generate any events.
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2399}
2400
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002401TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002402 const Point centerPoint = mDevice->getCenterPoint();
2403
2404 // Create an external stylus capable of reporting pressure data that
2405 // should be fused with a touch pointer.
2406 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2407 createUinputDevice<UinputExternalStylusWithPressure>();
2408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002410 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002411 ASSERT_TRUE(stylusInfo);
2412
2413 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2414
2415 const auto touchscreenId = mDeviceInfo.getId();
2416
2417 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2418 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002419 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2420 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002421 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002423
2424 // Start a finger gesture. The touch device will withhold generating any touches for
2425 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2426 mDevice->sendSlot(FIRST_SLOT);
2427 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2428 mDevice->sendToolType(MT_TOOL_FINGER);
2429 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002430 const auto syncTime = std::chrono::system_clock::now();
2431 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2432 // is generated in that period, there will be a race condition between the event being generated
2433 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2434 // will reduce the liklihood of the race condition occurring.
2435 const auto waitUntilTimeForNoEvent =
2436 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002437 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002438 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002440 // Since the external stylus did not report a pressure value within the timeout,
2441 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002442 const auto waitUntilTimeForEvent = syncTime +
2443 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2444 ASSERT_NO_FATAL_FAILURE(
2445 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2446 AMOTION_EVENT_ACTION_DOWN),
2447 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2448 AINPUT_SOURCE_STYLUS),
2449 WithToolType(ToolType::FINGER),
2450 WithDeviceId(touchscreenId),
2451 WithPressure(1.f)),
2452 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002453
2454 // Change the pressure on the external stylus. Since the pressure was not present at the start
2455 // of the gesture, it is ignored for now.
2456 stylus->setPressure(200);
2457 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2458
2459 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002460 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2461 mDevice->sendSync();
2462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2463 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002464 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002465 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002466
2467 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2468 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2469 mDevice->sendToolType(MT_TOOL_FINGER);
2470 mDevice->sendDown(centerPoint);
2471 mDevice->sendSync();
2472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002473 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2474 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2475 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002476
2477 // The external stylus did not generate any events.
2478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002481
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002482TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002483 const Point centerPoint = mDevice->getCenterPoint();
2484
2485 // Create an external stylus device that does not support pressure. It should not affect any
2486 // touch pointers.
2487 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2488 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002490 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002491 ASSERT_TRUE(stylusInfo);
2492
2493 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2494
2495 const auto touchscreenId = mDeviceInfo.getId();
2496
2497 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2498 // pressure data from the external stylus.
2499 mDevice->sendSlot(FIRST_SLOT);
2500 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2501 mDevice->sendToolType(MT_TOOL_FINGER);
2502 mDevice->sendDown(centerPoint);
2503 auto waitUntil = std::chrono::system_clock::now() +
2504 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2505 mDevice->sendSync();
2506 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002507 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2508 AMOTION_EVENT_ACTION_DOWN),
2509 WithToolType(ToolType::FINGER),
2510 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2511 AINPUT_SOURCE_STYLUS),
2512 WithButtonState(0),
2513 WithDeviceId(touchscreenId),
2514 WithPressure(1.f)),
2515 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002516
2517 // The external stylus did not generate any events.
2518 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2519 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2520}
2521
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522// --- InputDeviceTest ---
2523class InputDeviceTest : public testing::Test {
2524protected:
2525 static const char* DEVICE_NAME;
2526 static const char* DEVICE_LOCATION;
2527 static const int32_t DEVICE_ID;
2528 static const int32_t DEVICE_GENERATION;
2529 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002530 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531 static const int32_t EVENTHUB_ID;
2532 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2533
2534 std::shared_ptr<FakeEventHub> mFakeEventHub;
2535 sp<FakeInputReaderPolicy> mFakePolicy;
2536 std::unique_ptr<TestInputListener> mFakeListener;
2537 std::unique_ptr<InstrumentedInputReader> mReader;
2538 std::shared_ptr<InputDevice> mDevice;
2539
2540 void SetUp() override {
2541 mFakeEventHub = std::make_unique<FakeEventHub>();
2542 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2543 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002544 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002546 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002547 identifier.name = DEVICE_NAME;
2548 identifier.location = DEVICE_LOCATION;
2549 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2550 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2551 identifier);
2552 mReader->pushNextDevice(mDevice);
2553 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002554 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002556
2557 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002558 mFakeListener.reset();
2559 mFakePolicy.clear();
2560 }
2561};
2562
2563const char* InputDeviceTest::DEVICE_NAME = "device";
2564const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2565const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2566const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002568const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2569 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002570const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2572
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002573TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002575 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2576 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002578
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2580 ASSERT_EQ(mDevice->isEnabled(), false);
2581}
2582
2583TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2584 // Configuration.
2585 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002586 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587
2588 // Reset.
2589 unused += mDevice->reset(ARBITRARY_TIME);
2590
2591 NotifyDeviceResetArgs resetArgs;
2592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2593 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2594 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2595
2596 // Metadata.
2597 ASSERT_TRUE(mDevice->isIgnored());
2598 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2599
2600 InputDeviceInfo info = mDevice->getDeviceInfo();
2601 ASSERT_EQ(DEVICE_ID, info.getId());
2602 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2603 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2604 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2605
2606 // State queries.
2607 ASSERT_EQ(0, mDevice->getMetaState());
2608
2609 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2610 << "Ignored device should return unknown key code state.";
2611 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2612 << "Ignored device should return unknown scan code state.";
2613 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2614 << "Ignored device should return unknown switch state.";
2615
2616 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2617 uint8_t flags[2] = { 0, 1 };
2618 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2619 << "Ignored device should never mark any key codes.";
2620 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2621 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2622}
2623
2624TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2625 // Configuration.
2626 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2627
2628 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002629 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2630 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2632 mapper1.setMetaState(AMETA_ALT_ON);
2633 mapper1.addSupportedKeyCode(AKEYCODE_A);
2634 mapper1.addSupportedKeyCode(AKEYCODE_B);
2635 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2636 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2637 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2638 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2639 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2640
2641 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002642 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2643 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 mapper2.setMetaState(AMETA_SHIFT_ON);
2645
2646 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002647 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648
Harry Cuttsf13161a2023-03-08 14:15:49 +00002649 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2650 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002652 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002654 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2655 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656
2657 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002658 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002659 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2660 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661
2662 NotifyDeviceResetArgs resetArgs;
2663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2664 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2665 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2666
2667 // Metadata.
2668 ASSERT_FALSE(mDevice->isIgnored());
2669 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2670
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002671 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002673 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2675 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2676
2677 // State queries.
2678 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2679 << "Should query mappers and combine meta states.";
2680
2681 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2682 << "Should return unknown key code state when source not supported.";
2683 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2684 << "Should return unknown scan code state when source not supported.";
2685 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2686 << "Should return unknown switch state when source not supported.";
2687
2688 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2689 << "Should query mapper when source is supported.";
2690 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2691 << "Should query mapper when source is supported.";
2692 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2693 << "Should query mapper when source is supported.";
2694
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002695 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002697 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698 << "Should do nothing when source is unsupported.";
2699 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2700 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2701 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2702 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2703
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002704 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705 << "Should query mapper when source is supported.";
2706 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2707 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2708 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2709 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2710
2711 // Event handling.
2712 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002713 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002714 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002716 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2717 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002718}
2719
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002720TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2721 // Set some behavior to force the configuration to be update.
2722 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2723 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2724 AINPUT_SOURCE_KEYBOARD);
2725
2726 std::list<NotifyArgs> unused =
2727 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2728 /*changes=*/{});
2729
2730 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2731}
2732
2733TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2734 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2735 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2736 AINPUT_SOURCE_KEYBOARD);
2737
2738 std::list<NotifyArgs> unused =
2739 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2740 /*changes=*/{});
2741
2742 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2743}
2744
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002745TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2746 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2747 FakeInputMapper& mapper =
2748 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2749 AINPUT_SOURCE_KEYBOARD);
2750 NotifyMotionArgs args1;
2751 NotifySwitchArgs args2;
2752 NotifyKeyArgs args3;
2753 mapper.setProcessResult({args1, args2, args3});
2754
2755 InputReaderConfiguration config;
2756 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2757
2758 RawEvent event;
2759 event.deviceId = EVENTHUB_ID;
2760 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2761
2762 for (auto& arg : notifyArgs) {
2763 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2764 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2765 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2766 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2767 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2768 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2769 }
2770 }
2771}
2772
2773TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2774 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2775 FakeInputMapper& mapper =
2776 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2777 AINPUT_SOURCE_KEYBOARD);
2778 NotifyMotionArgs args;
2779 mapper.setProcessResult({args});
2780
2781 InputReaderConfiguration config;
2782 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2783
2784 RawEvent event;
2785 event.deviceId = EVENTHUB_ID;
2786 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2787
2788 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2789 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2790}
2791
2792TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2793 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2794 FakeInputMapper& mapper =
2795 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2796 AINPUT_SOURCE_KEYBOARD);
2797 NotifyMotionArgs args;
2798 args.policyFlags = POLICY_FLAG_WAKE;
2799 mapper.setProcessResult({args});
2800
2801 InputReaderConfiguration config;
2802 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2803
2804 RawEvent event;
2805 event.deviceId = EVENTHUB_ID;
2806 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2807
2808 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2809 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2810}
2811
Arthur Hung2c9a3342019-07-23 14:18:59 +08002812// A single input device is associated with a specific display. Check that:
2813// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002814// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002815TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002816 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2817 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002818
2819 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002820 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002821 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2822 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002823
2824 // Device should be enabled by default.
2825 ASSERT_TRUE(mDevice->isEnabled());
2826
2827 // Prepare associated info.
2828 constexpr uint8_t hdmi = 1;
2829 const std::string UNIQUE_ID = "local:1";
2830
2831 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002832 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002833 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002834 // Device should be disabled because it is associated with a specific display via
2835 // input port <-> display port association, but the corresponding display is not found
2836 ASSERT_FALSE(mDevice->isEnabled());
2837
2838 // Prepare displays.
2839 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002840 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002841 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002842 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002843 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002844 ASSERT_TRUE(mDevice->isEnabled());
2845
2846 // Device should be disabled after set disable.
2847 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002848 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002849 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002850 ASSERT_FALSE(mDevice->isEnabled());
2851
2852 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002853 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002854 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002855 ASSERT_FALSE(mDevice->isEnabled());
2856}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857
Christine Franks1ba71cc2021-04-07 14:37:42 -07002858TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2859 // Device should be enabled by default.
2860 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002861 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2862 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002863 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002864 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2865 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002866 ASSERT_TRUE(mDevice->isEnabled());
2867
2868 // Device should be disabled because it is associated with a specific display, but the
2869 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002870 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002871 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002872 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002873 ASSERT_FALSE(mDevice->isEnabled());
2874
2875 // Device should be enabled when a display is found.
2876 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002877 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002878 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002879 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002880 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002881 ASSERT_TRUE(mDevice->isEnabled());
2882
2883 // Device should be disabled after set disable.
2884 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002885 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002886 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002887 ASSERT_FALSE(mDevice->isEnabled());
2888
2889 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002890 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002891 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002892 ASSERT_FALSE(mDevice->isEnabled());
2893}
2894
Christine Franks2a2293c2022-01-18 11:51:16 -08002895TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2896 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002897 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2898 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002899 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002900 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2901 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002902
Christine Franks2a2293c2022-01-18 11:51:16 -08002903 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2904 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002905 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002906 NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002907 const auto initialGeneration = mDevice->getGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002908 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002909 InputReaderConfiguration::Change::DISPLAY_INFO);
Antonio Kantek0ac5e092024-04-22 17:10:27 +00002910 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueIdByPort());
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002911 ASSERT_GT(mDevice->getGeneration(), initialGeneration);
2912 ASSERT_EQ(mDevice->getDeviceInfo().getAssociatedDisplayId(), SECONDARY_DISPLAY_ID);
Christine Franks2a2293c2022-01-18 11:51:16 -08002913}
2914
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002915/**
2916 * This test reproduces a crash caused by a dangling reference that remains after device is added
2917 * and removed. The reference is accessed in InputDevice::dump(..);
2918 */
2919TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2920 constexpr int32_t TEST_EVENTHUB_ID = 10;
2921 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2922
Harry Cutts33476232023-01-30 19:57:29 +00002923 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002924 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2925 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002926 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2927 std::string dumpStr, eventHubDevStr;
2928 device.dump(dumpStr, eventHubDevStr);
2929}
2930
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002931TEST_F(InputDeviceTest, GetBluetoothAddress) {
2932 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2933 ASSERT_TRUE(address);
2934 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2935}
2936
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002937TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2938 mFakePolicy->clearViewports();
2939 FakeInputMapper& mapper =
2940 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2941 AINPUT_SOURCE_KEYBOARD);
2942 std::list<NotifyArgs> unused =
2943 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2944 /*changes=*/{});
2945
2946 mapper.assertConfigureWasCalled();
2947 mapper.assertResetWasNotCalled();
2948
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002949 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002950 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002951 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002952 .type = EV_SYN,
2953 .code = SYN_REPORT,
2954 .value = 0};
2955
2956 // Events are processed normally.
2957 unused = mDevice->process(&event, /*count=*/1);
2958 mapper.assertProcessWasCalled();
2959
2960 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002961 event.type = EV_SYN;
2962 event.code = SYN_DROPPED;
2963 event.value = 0;
2964 unused = mDevice->process(&event, /*count=*/1);
2965 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002966
2967 // All events until the next SYN_REPORT should be dropped.
2968 event.type = EV_KEY;
2969 event.code = KEY_A;
2970 event.value = 1;
2971 unused = mDevice->process(&event, /*count=*/1);
2972 mapper.assertProcessWasNotCalled();
2973
2974 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00002975 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002976 event.type = EV_SYN;
2977 event.code = SYN_REPORT;
2978 event.value = 0;
2979 unused = mDevice->process(&event, /*count=*/1);
2980 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00002981 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002982
2983 // The mapper receives events normally now.
2984 event.type = EV_KEY;
2985 event.code = KEY_B;
2986 event.value = 1;
2987 unused = mDevice->process(&event, /*count=*/1);
2988 mapper.assertProcessWasCalled();
2989}
2990
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991// --- SwitchInputMapperTest ---
2992
2993class SwitchInputMapperTest : public InputMapperTest {
2994protected:
2995};
2996
2997TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002998 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003000 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001}
3002
3003TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003004 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003006 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003007 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003009 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003010 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011}
3012
3013TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003014 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003015 std::list<NotifyArgs> out;
3016 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3017 ASSERT_TRUE(out.empty());
3018 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3019 ASSERT_TRUE(out.empty());
3020 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3021 ASSERT_TRUE(out.empty());
3022 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003023
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003024 ASSERT_EQ(1u, out.size());
3025 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003027 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3028 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029 args.switchMask);
3030 ASSERT_EQ(uint32_t(0), args.policyFlags);
3031}
3032
Chris Ye87143712020-11-10 05:05:58 +00003033// --- VibratorInputMapperTest ---
3034class VibratorInputMapperTest : public InputMapperTest {
3035protected:
3036 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3037};
3038
3039TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003040 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003041
3042 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3043}
3044
3045TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003046 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003047
3048 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3049}
3050
3051TEST_F(VibratorInputMapperTest, Vibrate) {
3052 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003053 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003054 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003055
3056 VibrationElement pattern(2);
3057 VibrationSequence sequence(2);
3058 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003059 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3060 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003061 sequence.addElement(pattern);
3062 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003063 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3064 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003065 sequence.addElement(pattern);
3066
3067 std::vector<int64_t> timings = {0, 1};
3068 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3069
3070 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003071 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003072 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003073 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003074 // Verify vibrator state listener was notified.
3075 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003076 ASSERT_EQ(1u, out.size());
3077 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3078 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3079 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003080 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003081 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003082 ASSERT_FALSE(mapper.isVibrating());
3083 // Verify vibrator state listener was notified.
3084 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003085 ASSERT_EQ(1u, out.size());
3086 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3087 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3088 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003089}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090
Chris Yef59a2f42020-10-16 12:55:26 -07003091// --- SensorInputMapperTest ---
3092
3093class SensorInputMapperTest : public InputMapperTest {
3094protected:
3095 static const int32_t ACCEL_RAW_MIN;
3096 static const int32_t ACCEL_RAW_MAX;
3097 static const int32_t ACCEL_RAW_FUZZ;
3098 static const int32_t ACCEL_RAW_FLAT;
3099 static const int32_t ACCEL_RAW_RESOLUTION;
3100
3101 static const int32_t GYRO_RAW_MIN;
3102 static const int32_t GYRO_RAW_MAX;
3103 static const int32_t GYRO_RAW_FUZZ;
3104 static const int32_t GYRO_RAW_FLAT;
3105 static const int32_t GYRO_RAW_RESOLUTION;
3106
3107 static const float GRAVITY_MS2_UNIT;
3108 static const float DEGREE_RADIAN_UNIT;
3109
3110 void prepareAccelAxes();
3111 void prepareGyroAxes();
3112 void setAccelProperties();
3113 void setGyroProperties();
3114 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3115};
3116
3117const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3118const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3119const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3120const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3121const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3122
3123const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3124const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3125const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3126const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3127const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3128
3129const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3130const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3131
3132void SensorInputMapperTest::prepareAccelAxes() {
3133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3134 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3136 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3138 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3139}
3140
3141void SensorInputMapperTest::prepareGyroAxes() {
3142 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3143 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3145 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3147 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3148}
3149
3150void SensorInputMapperTest::setAccelProperties() {
3151 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3152 /* sensorDataIndex */ 0);
3153 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3154 /* sensorDataIndex */ 1);
3155 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3156 /* sensorDataIndex */ 2);
3157 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3158 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3159 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3160 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3161 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3162}
3163
3164void SensorInputMapperTest::setGyroProperties() {
3165 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3166 /* sensorDataIndex */ 0);
3167 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3168 /* sensorDataIndex */ 1);
3169 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3170 /* sensorDataIndex */ 2);
3171 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3172 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3173 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3174 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3175 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3176}
3177
3178TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003179 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003180
3181 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3182}
3183
3184TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3185 setAccelProperties();
3186 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003187 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003188
3189 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3190 std::chrono::microseconds(10000),
3191 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003192 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003198
3199 NotifySensorArgs args;
3200 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3201 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3202 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3203
3204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3205 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3206 ASSERT_EQ(args.deviceId, DEVICE_ID);
3207 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3208 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3209 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3210 ASSERT_EQ(args.values, values);
3211 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3212}
3213
3214TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3215 setGyroProperties();
3216 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003217 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003218
3219 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3220 std::chrono::microseconds(10000),
3221 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003222 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003228
3229 NotifySensorArgs args;
3230 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3231 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3232 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3233
3234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3235 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3236 ASSERT_EQ(args.deviceId, DEVICE_ID);
3237 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3238 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3239 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3240 ASSERT_EQ(args.values, values);
3241 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3242}
3243
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244// --- KeyboardInputMapperTest ---
3245
3246class KeyboardInputMapperTest : public InputMapperTest {
3247protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003248 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003249 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003250 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003251
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003253 int32_t originalKeyCode, int32_t rotatedKeyCode,
3254 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255};
3256
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003257/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3258 * orientation.
3259 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003260void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003261 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3262 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003263}
3264
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003266 int32_t originalScanCode, int32_t originalKeyCode,
3267 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268 NotifyKeyArgs args;
3269
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3272 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3273 ASSERT_EQ(originalScanCode, args.scanCode);
3274 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003275 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3280 ASSERT_EQ(originalScanCode, args.scanCode);
3281 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003282 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283}
3284
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003286 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003287 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291}
3292
3293TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3294 const int32_t USAGE_A = 0x070004;
3295 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003296 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3297 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003298 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3299 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3300 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003302 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003303 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003304 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003305 // Initial metastate is AMETA_NONE.
3306 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307
3308 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310 NotifyKeyArgs args;
3311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3312 ASSERT_EQ(DEVICE_ID, args.deviceId);
3313 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3314 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3316 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3317 ASSERT_EQ(KEY_HOME, args.scanCode);
3318 ASSERT_EQ(AMETA_NONE, args.metaState);
3319 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3320 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3321 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3322
3323 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003324 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3326 ASSERT_EQ(DEVICE_ID, args.deviceId);
3327 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3328 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3329 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3330 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3331 ASSERT_EQ(KEY_HOME, args.scanCode);
3332 ASSERT_EQ(AMETA_NONE, args.metaState);
3333 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3334 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3335 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3336
3337 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3341 ASSERT_EQ(DEVICE_ID, args.deviceId);
3342 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3343 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3345 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3346 ASSERT_EQ(0, args.scanCode);
3347 ASSERT_EQ(AMETA_NONE, args.metaState);
3348 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3349 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3350 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3351
3352 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3354 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3356 ASSERT_EQ(DEVICE_ID, args.deviceId);
3357 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3358 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3359 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3360 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3361 ASSERT_EQ(0, args.scanCode);
3362 ASSERT_EQ(AMETA_NONE, args.metaState);
3363 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3364 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3365 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3366
3367 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3371 ASSERT_EQ(DEVICE_ID, args.deviceId);
3372 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3373 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3374 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3375 ASSERT_EQ(0, args.keyCode);
3376 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3377 ASSERT_EQ(AMETA_NONE, args.metaState);
3378 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3379 ASSERT_EQ(0U, args.policyFlags);
3380 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3381
3382 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3384 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3386 ASSERT_EQ(DEVICE_ID, args.deviceId);
3387 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3388 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3389 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3390 ASSERT_EQ(0, args.keyCode);
3391 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3392 ASSERT_EQ(AMETA_NONE, args.metaState);
3393 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3394 ASSERT_EQ(0U, args.policyFlags);
3395 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3396}
3397
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003398TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3399 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3400 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3401 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3402
3403 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003404 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003405 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3406
3407 // Key down by scan code.
3408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3409 NotifyKeyArgs args;
3410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3411 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3412
3413 // Key up by scan code.
3414 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3416 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3417}
3418
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003419/**
3420 * Ensure that the readTime is set to the time when the EV_KEY is received.
3421 */
3422TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3423 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3424
3425 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003426 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003427 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3428 NotifyKeyArgs args;
3429
3430 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003431 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3433 ASSERT_EQ(12, args.readTime);
3434
3435 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003436 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3438 ASSERT_EQ(15, args.readTime);
3439}
3440
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003442 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3443 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003444 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3445 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3446 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003448 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003449 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003450 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451
Arthur Hung95f68612022-04-07 14:08:22 +08003452 // Initial metastate is AMETA_NONE.
3453 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454
3455 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457 NotifyKeyArgs args;
3458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3459 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003460 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003461 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462
3463 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003464 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3466 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003467 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468
3469 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003470 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3472 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003473 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474
3475 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003476 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3478 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003479 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003480 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481}
3482
3483TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3486 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3487 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003489 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003490 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003491 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492
Michael Wrighta9cf4192022-12-01 23:46:39 +00003493 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3495 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3496 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3497 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3498 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3499 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3500 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3501 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3502}
3503
3504TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003505 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3506 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3507 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3508 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003511 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003512 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003513 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
Michael Wrighta9cf4192022-12-01 23:46:39 +00003515 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003516 ASSERT_NO_FATAL_FAILURE(
3517 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3518 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3519 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3520 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3521 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3522 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3523 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003525 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003526 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003527 ASSERT_NO_FATAL_FAILURE(
3528 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3529 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3530 AKEYCODE_DPAD_UP, DISPLAY_ID));
3531 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3532 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3533 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3534 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003536 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003537 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003538 ASSERT_NO_FATAL_FAILURE(
3539 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3540 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3541 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3542 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3543 AKEYCODE_DPAD_UP, DISPLAY_ID));
3544 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3545 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003547 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003548 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003549 ASSERT_NO_FATAL_FAILURE(
3550 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3551 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3552 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3553 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3554 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3555 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3556 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557
3558 // Special case: if orientation changes while key is down, we still emit the same keycode
3559 // in the key up as we did in the key down.
3560 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003561 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003562 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3565 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3566 ASSERT_EQ(KEY_UP, args.scanCode);
3567 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3568
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003569 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003570 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3573 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3574 ASSERT_EQ(KEY_UP, args.scanCode);
3575 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3576}
3577
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003578TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3579 // If the keyboard is not orientation aware,
3580 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003581 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003583 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003584 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003585 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003586 NotifyKeyArgs args;
3587
3588 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3593 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3594
Michael Wrighta9cf4192022-12-01 23:46:39 +00003595 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3600 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3601}
3602
3603TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3604 // If the keyboard is orientation aware,
3605 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003606 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003607
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003608 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003609 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003610 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003611 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003612 NotifyKeyArgs args;
3613
3614 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3615 // ^--- already checked by the previous test
3616
Michael Wrighta9cf4192022-12-01 23:46:39 +00003617 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003618 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3623 ASSERT_EQ(DISPLAY_ID, args.displayId);
3624
3625 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003626 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003627 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003628 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3633 ASSERT_EQ(newDisplayId, args.displayId);
3634}
3635
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003637 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003638 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003639 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003641 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003642 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003644 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003645 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646}
3647
Philip Junker4af3b3d2021-12-14 10:36:55 +01003648TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3649 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003650 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003651 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3652
3653 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3654 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3655 << "If a mapping is available, the result is equal to the mapping";
3656
3657 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3658 << "If no mapping is available, the result is the key location";
3659}
3660
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003662 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003663 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003664 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003666 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003667 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003669 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003670 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671}
3672
3673TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003674 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003675 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003676 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003678 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679
Michael Wrightd02c5b62014-02-10 15:10:22 -08003680 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003681 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682 ASSERT_TRUE(flags[0]);
3683 ASSERT_FALSE(flags[1]);
3684}
3685
3686TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003687 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3688 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3689 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3691 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3692 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003694 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003695 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003697 // Initial metastate is AMETA_NONE.
3698 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699
3700 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003701 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3702 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3703 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704
3705 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003708 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3709 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3710 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003711 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712
3713 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003716 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3717 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3718 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003719 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720
3721 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003724 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3725 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3726 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
3729 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3733 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3734 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003735 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736
3737 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003740 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3741 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3742 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003743 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
3745 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3749 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3750 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752}
3753
Chris Yea52ade12020-08-27 16:49:20 -07003754TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3755 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3756 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3757 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3758 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3759
3760 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003761 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003762 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3763
Chris Yea52ade12020-08-27 16:49:20 -07003764 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003765 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003766 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3767 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3768 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3769 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3770
3771 NotifyKeyArgs args;
3772 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3775 ASSERT_EQ(AMETA_NONE, args.metaState);
3776 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3777 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3778 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3779
3780 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003781 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3783 ASSERT_EQ(AMETA_NONE, args.metaState);
3784 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3785 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3786 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3787}
3788
Arthur Hung2c9a3342019-07-23 14:18:59 +08003789TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3790 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003791 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3792 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3793 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3794 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003795
3796 // keyboard 2.
3797 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003798 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003799 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003800 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003801 std::shared_ptr<InputDevice> device2 =
3802 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003803 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003805 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3806 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3807 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3808 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003809
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003810 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003811 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003812 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003813
Arpit Singh67ca6842023-04-26 14:43:16 +00003814 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003815 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003816 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3817 mFakePolicy
3818 ->getReaderConfiguration(),
3819 AINPUT_SOURCE_KEYBOARD,
3820 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003821 std::list<NotifyArgs> unused =
3822 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003823 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003824 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003825
3826 // Prepared displays and associated info.
3827 constexpr uint8_t hdmi1 = 0;
3828 constexpr uint8_t hdmi2 = 1;
3829 const std::string SECONDARY_UNIQUE_ID = "local:1";
3830
3831 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3832 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3833
3834 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003835 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003836 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003837 ASSERT_FALSE(device2->isEnabled());
3838
3839 // Prepare second display.
3840 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003841 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003842 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003843 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003844 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003845 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003846 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003847 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003848
3849 // Device should be enabled after the associated display is found.
3850 ASSERT_TRUE(mDevice->isEnabled());
3851 ASSERT_TRUE(device2->isEnabled());
3852
3853 // Test pad key events
3854 ASSERT_NO_FATAL_FAILURE(
3855 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3856 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3857 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3858 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3859 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3860 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3861 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3862
3863 ASSERT_NO_FATAL_FAILURE(
3864 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3865 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3866 AKEYCODE_DPAD_RIGHT, newDisplayId));
3867 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3868 AKEYCODE_DPAD_DOWN, newDisplayId));
3869 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3870 AKEYCODE_DPAD_LEFT, newDisplayId));
3871}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872
arthurhungc903df12020-08-11 15:08:42 +08003873TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3874 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3875 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3876 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3877 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3878 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3879 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3880
3881 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003882 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003883 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003884 // Initial metastate is AMETA_NONE.
3885 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003886
3887 // Initialization should have turned all of the lights off.
3888 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3889 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3890 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3891
3892 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003895 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3896 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3897
3898 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003901 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3902 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3903
3904 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003907 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3908 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3909
3910 mFakeEventHub->removeDevice(EVENTHUB_ID);
3911 mReader->loopOnce();
3912
3913 // keyboard 2 should default toggle keys.
3914 const std::string USB2 = "USB2";
3915 const std::string DEVICE_NAME2 = "KEYBOARD2";
3916 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3917 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3918 std::shared_ptr<InputDevice> device2 =
3919 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003920 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003921 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3922 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3923 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3924 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3925 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3926 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3927
Arpit Singh67ca6842023-04-26 14:43:16 +00003928 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003929 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003930 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3931 mFakePolicy
3932 ->getReaderConfiguration(),
3933 AINPUT_SOURCE_KEYBOARD,
3934 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003935 std::list<NotifyArgs> unused =
3936 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003937 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003938 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003939
3940 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3941 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3942 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003943 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3944 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003945}
3946
Arthur Hungcb40a002021-08-03 14:31:01 +00003947TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3948 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3949 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3950 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3951
3952 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003953 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003954 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3955 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003956 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003957 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003958 // Initial metastate is AMETA_NONE.
3959 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003960
3961 mReader->toggleCapsLockState(DEVICE_ID);
3962 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3963}
3964
Arthur Hungfb3cc112022-04-13 07:39:50 +00003965TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3966 // keyboard 1.
3967 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3968 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3969 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3970 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3971 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3972 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3973
3974 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003975 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003976 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3977
3978 // keyboard 2.
3979 const std::string USB2 = "USB2";
3980 const std::string DEVICE_NAME2 = "KEYBOARD2";
3981 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3982 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3983 std::shared_ptr<InputDevice> device2 =
3984 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3985 ftl::Flags<InputDeviceClass>(0));
3986 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3987 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3988 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3989 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3990 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3991 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3992
Arpit Singh67ca6842023-04-26 14:43:16 +00003993 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003994 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003995 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3996 mFakePolicy
3997 ->getReaderConfiguration(),
3998 AINPUT_SOURCE_KEYBOARD,
3999 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004000 std::list<NotifyArgs> unused =
4001 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004002 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004003 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004004
Arthur Hung95f68612022-04-07 14:08:22 +08004005 // Initial metastate is AMETA_NONE.
4006 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4007 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4008
4009 // Toggle num lock on and off.
4010 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4011 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004012 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4013 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4014 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4015
4016 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4017 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4018 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4019 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4020 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4021
4022 // Toggle caps lock on and off.
4023 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4024 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4025 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4026 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4027 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4028
4029 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4030 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4031 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4032 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4033 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4034
4035 // Toggle scroll lock on and off.
4036 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4037 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4038 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4039 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4040 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4041
4042 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4043 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4044 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4045 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4046 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4047}
4048
Arthur Hung2141d542022-08-23 07:45:21 +00004049TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4050 const int32_t USAGE_A = 0x070004;
4051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4052 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4053
4054 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004055 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004056 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4057 // Key down by scan code.
4058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4059 NotifyKeyArgs args;
4060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4061 ASSERT_EQ(DEVICE_ID, args.deviceId);
4062 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4063 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4064 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4065 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4066 ASSERT_EQ(KEY_HOME, args.scanCode);
4067 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4068
4069 // Disable device, it should synthesize cancellation events for down events.
4070 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004071 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004072
4073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4074 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4075 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4076 ASSERT_EQ(KEY_HOME, args.scanCode);
4077 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4078}
4079
Zixuan Qufecb6062022-11-12 04:44:31 +00004080TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004081 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4082 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004083 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004084 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4085 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004086
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004087 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004088 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4089
4090 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004091 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004092
4093 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4094 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4095 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4096 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4097 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004098 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4099
4100 // Call change layout association with the same values: Generation shouldn't change
4101 generation = mReader->getContext()->getGeneration();
4102 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4103 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4104 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4105 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004106}
4107
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004108TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4109 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4110 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4111
4112 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004113 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004114 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4115 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004116 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004117
4118 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4119 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4120}
4121
Justin Chung71ddb432023-03-27 04:29:07 +00004122TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4124 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004125 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004126 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4127 NotifyKeyArgs args;
4128
4129 // Key down
4130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4132 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4133}
4134
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004135// --- KeyboardInputMapperTest_ExternalDevice ---
4136
4137class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4138protected:
Chris Yea52ade12020-08-27 16:49:20 -07004139 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004140};
4141
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004142TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004143 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4144 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004145
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004146 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4147 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4148 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4149 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004151 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004152 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004153 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004154
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004156 NotifyKeyArgs args;
4157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4158 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4159
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004160 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4162 ASSERT_EQ(uint32_t(0), args.policyFlags);
4163
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004166 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004167
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004168 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4170 ASSERT_EQ(uint32_t(0), args.policyFlags);
4171
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4174 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4175
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004176 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4178 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4179}
4180
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004181TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_NoneAlphabeticKeyboard) {
4182 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4183 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4184
4185 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4186 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4187 POLICY_FLAG_WAKE);
4188
4189 KeyboardInputMapper& mapper =
4190 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4191 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4192
4193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4194 NotifyKeyArgs args;
4195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4196 ASSERT_EQ(uint32_t(0), args.policyFlags);
4197
4198 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4200 ASSERT_EQ(uint32_t(0), args.policyFlags);
4201
4202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4204 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4205
4206 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4208 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4209}
4210
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004211TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004212 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004213
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004214 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4215 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4216 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004217
Powei Fengd041c5d2019-05-03 17:11:33 -07004218 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004219 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004220 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004222
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004224 NotifyKeyArgs args;
4225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4226 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4227
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004228 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4230 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4231
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4234 ASSERT_EQ(uint32_t(0), args.policyFlags);
4235
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004236 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4238 ASSERT_EQ(uint32_t(0), args.policyFlags);
4239
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4242 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4243
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004244 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4246 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4247}
4248
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249// --- TouchInputMapperTest ---
4250
4251class TouchInputMapperTest : public InputMapperTest {
4252protected:
4253 static const int32_t RAW_X_MIN;
4254 static const int32_t RAW_X_MAX;
4255 static const int32_t RAW_Y_MIN;
4256 static const int32_t RAW_Y_MAX;
4257 static const int32_t RAW_TOUCH_MIN;
4258 static const int32_t RAW_TOUCH_MAX;
4259 static const int32_t RAW_TOOL_MIN;
4260 static const int32_t RAW_TOOL_MAX;
4261 static const int32_t RAW_PRESSURE_MIN;
4262 static const int32_t RAW_PRESSURE_MAX;
4263 static const int32_t RAW_ORIENTATION_MIN;
4264 static const int32_t RAW_ORIENTATION_MAX;
4265 static const int32_t RAW_DISTANCE_MIN;
4266 static const int32_t RAW_DISTANCE_MAX;
4267 static const int32_t RAW_TILT_MIN;
4268 static const int32_t RAW_TILT_MAX;
4269 static const int32_t RAW_ID_MIN;
4270 static const int32_t RAW_ID_MAX;
4271 static const int32_t RAW_SLOT_MIN;
4272 static const int32_t RAW_SLOT_MAX;
4273 static const float X_PRECISION;
4274 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004275 static const float X_PRECISION_VIRTUAL;
4276 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277
4278 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004279 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
4281 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4282
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004283 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004284 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004285
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 enum Axes {
4287 POSITION = 1 << 0,
4288 TOUCH = 1 << 1,
4289 TOOL = 1 << 2,
4290 PRESSURE = 1 << 3,
4291 ORIENTATION = 1 << 4,
4292 MINOR = 1 << 5,
4293 ID = 1 << 6,
4294 DISTANCE = 1 << 7,
4295 TILT = 1 << 8,
4296 SLOT = 1 << 9,
4297 TOOL_TYPE = 1 << 10,
4298 };
4299
Michael Wrighta9cf4192022-12-01 23:46:39 +00004300 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004301 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004302 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004304 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 int32_t toRawX(float displayX);
4306 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004307 int32_t toRotatedRawX(float displayX);
4308 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004309 float toCookedX(float rawX, float rawY);
4310 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004312 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004314 float toDisplayY(int32_t rawY, int32_t displayHeight);
4315
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316};
4317
4318const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4319const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4320const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4321const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4322const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4323const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4324const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4325const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004326const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4327const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4329const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4330const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4331const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4332const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4333const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4334const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4335const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4336const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4337const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4338const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4339const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004340const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4341 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4342const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4343 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004344const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4345 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346
4347const float TouchInputMapperTest::GEOMETRIC_SCALE =
4348 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4349 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4350
4351const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4352 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4353 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4354};
4355
Michael Wrighta9cf4192022-12-01 23:46:39 +00004356void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004357 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4358 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004359}
4360
4361void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4362 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004363 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364}
4365
Michael Wrighta9cf4192022-12-01 23:46:39 +00004366void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004367 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4368 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4369 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004370}
4371
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004373 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4374 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4375 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4376 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377}
4378
Jason Gerecke489fda82012-09-07 17:19:40 -07004379void TouchInputMapperTest::prepareLocationCalibration() {
4380 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4381}
4382
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383int32_t TouchInputMapperTest::toRawX(float displayX) {
4384 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4385}
4386
4387int32_t TouchInputMapperTest::toRawY(float displayY) {
4388 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4389}
4390
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004391int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4392 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4393}
4394
4395int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4396 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4397}
4398
Jason Gerecke489fda82012-09-07 17:19:40 -07004399float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4400 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4401 return rawX;
4402}
4403
4404float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4405 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4406 return rawY;
4407}
4408
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004410 return toDisplayX(rawX, DISPLAY_WIDTH);
4411}
4412
4413float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4414 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415}
4416
4417float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004418 return toDisplayY(rawY, DISPLAY_HEIGHT);
4419}
4420
4421float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4422 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423}
4424
4425
4426// --- SingleTouchInputMapperTest ---
4427
4428class SingleTouchInputMapperTest : public TouchInputMapperTest {
4429protected:
4430 void prepareButtons();
4431 void prepareAxes(int axes);
4432
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004433 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4434 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4435 void processUp(SingleTouchInputMapper& mappery);
4436 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4437 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4438 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4439 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4440 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4441 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442};
4443
4444void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004445 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446}
4447
4448void SingleTouchInputMapperTest::prepareAxes(int axes) {
4449 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004450 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4451 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 }
4453 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004454 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4455 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 }
4457 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004458 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4459 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 }
4461 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004462 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4463 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 }
4465 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004466 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4467 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468 }
4469}
4470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004471void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475}
4476
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004477void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480}
4481
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004482void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484}
4485
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004486void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488}
4489
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004490void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4491 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493}
4494
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004495void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497}
4498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004499void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4500 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503}
4504
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004505void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4506 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508}
4509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004510void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512}
4513
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515 prepareButtons();
4516 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004517 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518
Josep del Río2d8c79a2023-01-23 19:33:50 +00004519 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520}
4521
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 prepareButtons();
4524 prepareAxes(POSITION);
4525 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004526 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004528 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529}
4530
4531TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004533 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 prepareButtons();
4535 prepareAxes(POSITION);
4536 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004537 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538
4539 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004540 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541
4542 // Virtual key is down.
4543 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4544 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4545 processDown(mapper, x, y);
4546 processSync(mapper);
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004549 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550
4551 // Virtual key is up.
4552 processUp(mapper);
4553 processSync(mapper);
4554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4555
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004556 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557}
4558
4559TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004561 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 prepareButtons();
4563 prepareAxes(POSITION);
4564 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004565 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566
4567 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004568 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569
4570 // Virtual key is down.
4571 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4572 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4573 processDown(mapper, x, y);
4574 processSync(mapper);
4575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004577 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578
4579 // Virtual key is up.
4580 processUp(mapper);
4581 processSync(mapper);
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4583
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004584 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585}
4586
4587TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004589 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 prepareButtons();
4591 prepareAxes(POSITION);
4592 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004593 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004596 ASSERT_TRUE(
4597 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 ASSERT_TRUE(flags[0]);
4599 ASSERT_FALSE(flags[1]);
4600}
4601
4602TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004604 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 prepareButtons();
4606 prepareAxes(POSITION);
4607 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004608 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609
arthurhungdcef2dc2020-08-11 14:47:50 +08004610 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611
4612 NotifyKeyArgs args;
4613
4614 // Press virtual key.
4615 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4616 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4617 processDown(mapper, x, y);
4618 processSync(mapper);
4619
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4621 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4622 ASSERT_EQ(DEVICE_ID, args.deviceId);
4623 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4624 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4625 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4626 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4627 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4628 ASSERT_EQ(KEY_HOME, args.scanCode);
4629 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4630 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4631
4632 // Release virtual key.
4633 processUp(mapper);
4634 processSync(mapper);
4635
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4637 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4638 ASSERT_EQ(DEVICE_ID, args.deviceId);
4639 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4640 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4641 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4642 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4643 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4644 ASSERT_EQ(KEY_HOME, args.scanCode);
4645 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4646 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4647
4648 // Should not have sent any motions.
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4650}
4651
4652TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004654 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655 prepareButtons();
4656 prepareAxes(POSITION);
4657 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004658 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659
arthurhungdcef2dc2020-08-11 14:47:50 +08004660 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661
4662 NotifyKeyArgs keyArgs;
4663
4664 // Press virtual key.
4665 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4666 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4667 processDown(mapper, x, y);
4668 processSync(mapper);
4669
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4671 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4672 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4673 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4674 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4675 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4676 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4677 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4678 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4679 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4680 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4681
4682 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4683 // into the display area.
4684 y -= 100;
4685 processMove(mapper, x, y);
4686 processSync(mapper);
4687
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4689 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4690 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4691 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4692 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4693 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4694 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4695 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4696 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4697 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4698 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4699 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4700
4701 NotifyMotionArgs motionArgs;
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4703 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4704 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4705 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4706 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4708 ASSERT_EQ(0, motionArgs.flags);
4709 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4710 ASSERT_EQ(0, motionArgs.buttonState);
4711 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004712 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004714 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4716 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4717 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4718 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4719 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4720
4721 // Keep moving out of bounds. Should generate a pointer move.
4722 y -= 50;
4723 processMove(mapper, x, y);
4724 processSync(mapper);
4725
4726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4728 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4729 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4730 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4732 ASSERT_EQ(0, motionArgs.flags);
4733 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4734 ASSERT_EQ(0, motionArgs.buttonState);
4735 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004736 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004738 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4740 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4741 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4742 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4744
4745 // Release out of bounds. Should generate a pointer up.
4746 processUp(mapper);
4747 processSync(mapper);
4748
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4751 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4752 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4753 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4754 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4755 ASSERT_EQ(0, motionArgs.flags);
4756 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4757 ASSERT_EQ(0, motionArgs.buttonState);
4758 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004759 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004761 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4763 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4764 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4765 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4766 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4767
4768 // Should not have sent any more keys or motions.
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4771}
4772
4773TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004775 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 prepareButtons();
4777 prepareAxes(POSITION);
4778 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004779 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780
arthurhungdcef2dc2020-08-11 14:47:50 +08004781 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782
4783 NotifyMotionArgs motionArgs;
4784
4785 // Initially go down out of bounds.
4786 int32_t x = -10;
4787 int32_t y = -10;
4788 processDown(mapper, x, y);
4789 processSync(mapper);
4790
4791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4792
4793 // Move into the display area. Should generate a pointer down.
4794 x = 50;
4795 y = 75;
4796 processMove(mapper, x, y);
4797 processSync(mapper);
4798
4799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4800 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4801 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4802 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4803 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4805 ASSERT_EQ(0, motionArgs.flags);
4806 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4807 ASSERT_EQ(0, motionArgs.buttonState);
4808 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004809 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004811 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4813 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4814 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4815 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4817
4818 // Release. Should generate a pointer up.
4819 processUp(mapper);
4820 processSync(mapper);
4821
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4823 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4824 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4825 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4826 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4827 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4828 ASSERT_EQ(0, motionArgs.flags);
4829 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4830 ASSERT_EQ(0, motionArgs.buttonState);
4831 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004832 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004834 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4836 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4837 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4838 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4839 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4840
4841 // Should not have sent any more keys or motions.
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4844}
4845
Santos Cordonfa5cf462017-04-05 10:37:00 -07004846TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004847 addConfigurationProperty("touch.deviceType", "touchScreen");
4848 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4849
Michael Wrighta9cf4192022-12-01 23:46:39 +00004850 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004851 prepareButtons();
4852 prepareAxes(POSITION);
4853 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004854 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004855
arthurhungdcef2dc2020-08-11 14:47:50 +08004856 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004857
4858 NotifyMotionArgs motionArgs;
4859
4860 // Down.
4861 int32_t x = 100;
4862 int32_t y = 125;
4863 processDown(mapper, x, y);
4864 processSync(mapper);
4865
4866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4867 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4868 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4869 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4870 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4871 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4872 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4873 ASSERT_EQ(0, motionArgs.flags);
4874 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4875 ASSERT_EQ(0, motionArgs.buttonState);
4876 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004877 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004878 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004879 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4881 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4882 1, 0, 0, 0, 0, 0, 0, 0));
4883 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4884 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4885 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4886
4887 // Move.
4888 x += 50;
4889 y += 75;
4890 processMove(mapper, x, y);
4891 processSync(mapper);
4892
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4894 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4895 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4896 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4897 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4898 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4900 ASSERT_EQ(0, motionArgs.flags);
4901 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4902 ASSERT_EQ(0, motionArgs.buttonState);
4903 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004904 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004905 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004906 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4908 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4909 1, 0, 0, 0, 0, 0, 0, 0));
4910 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4911 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4913
4914 // Up.
4915 processUp(mapper);
4916 processSync(mapper);
4917
4918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4919 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4920 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4921 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4922 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4923 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4924 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4925 ASSERT_EQ(0, motionArgs.flags);
4926 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4927 ASSERT_EQ(0, motionArgs.buttonState);
4928 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004929 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004930 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004931 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4933 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4934 1, 0, 0, 0, 0, 0, 0, 0));
4935 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4936 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4937 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4938
4939 // Should not have sent any more keys or motions.
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4942}
4943
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004946 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947 prepareButtons();
4948 prepareAxes(POSITION);
4949 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004950 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951
arthurhungdcef2dc2020-08-11 14:47:50 +08004952 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953
4954 NotifyMotionArgs motionArgs;
4955
4956 // Down.
4957 int32_t x = 100;
4958 int32_t y = 125;
4959 processDown(mapper, x, y);
4960 processSync(mapper);
4961
4962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4963 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4964 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4965 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4966 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4967 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4968 ASSERT_EQ(0, motionArgs.flags);
4969 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4970 ASSERT_EQ(0, motionArgs.buttonState);
4971 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004972 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004974 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4976 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4977 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4978 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4979 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4980
4981 // Move.
4982 x += 50;
4983 y += 75;
4984 processMove(mapper, x, y);
4985 processSync(mapper);
4986
4987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4988 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4989 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4990 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4991 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4993 ASSERT_EQ(0, motionArgs.flags);
4994 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4995 ASSERT_EQ(0, motionArgs.buttonState);
4996 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004997 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004999 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5001 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5002 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5003 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5004 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5005
5006 // Up.
5007 processUp(mapper);
5008 processSync(mapper);
5009
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5011 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5012 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5013 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5014 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5015 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5016 ASSERT_EQ(0, motionArgs.flags);
5017 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5018 ASSERT_EQ(0, motionArgs.buttonState);
5019 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005020 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005022 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5024 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5025 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5026 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5028
5029 // Should not have sent any more keys or motions.
5030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5032}
5033
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005034TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 addConfigurationProperty("touch.deviceType", "touchScreen");
5036 prepareButtons();
5037 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005038 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5039 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005040 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041
5042 NotifyMotionArgs args;
5043
5044 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005045 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046 processDown(mapper, toRawX(50), toRawY(75));
5047 processSync(mapper);
5048
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5050 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5051 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5052
5053 processUp(mapper);
5054 processSync(mapper);
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5056}
5057
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005058TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059 addConfigurationProperty("touch.deviceType", "touchScreen");
5060 prepareButtons();
5061 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005062 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5063 // orientation-aware are affected by display rotation.
5064 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005065 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066
5067 NotifyMotionArgs args;
5068
5069 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005070 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005071 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 processDown(mapper, toRawX(50), toRawY(75));
5073 processSync(mapper);
5074
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5076 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5077 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5078
5079 processUp(mapper);
5080 processSync(mapper);
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5082
5083 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005084 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005085 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005086 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 processSync(mapper);
5088
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5090 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5091 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5092
5093 processUp(mapper);
5094 processSync(mapper);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5096
5097 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005098 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005099 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5101 processSync(mapper);
5102
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5104 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5105 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5106
5107 processUp(mapper);
5108 processSync(mapper);
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5110
5111 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005112 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005113 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005114 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 processSync(mapper);
5116
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5118 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5119 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5120
5121 processUp(mapper);
5122 processSync(mapper);
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5124}
5125
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005126TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5127 addConfigurationProperty("touch.deviceType", "touchScreen");
5128 prepareButtons();
5129 prepareAxes(POSITION);
5130 addConfigurationProperty("touch.orientationAware", "1");
5131 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5132 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005133 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005134 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005135 NotifyMotionArgs args;
5136
5137 // Orientation 0.
5138 processDown(mapper, toRawX(50), toRawY(75));
5139 processSync(mapper);
5140
5141 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5142 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5143 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5144
5145 processUp(mapper);
5146 processSync(mapper);
5147 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5148}
5149
5150TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5151 addConfigurationProperty("touch.deviceType", "touchScreen");
5152 prepareButtons();
5153 prepareAxes(POSITION);
5154 addConfigurationProperty("touch.orientationAware", "1");
5155 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5156 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005157 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005158 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005159 NotifyMotionArgs args;
5160
5161 // Orientation 90.
5162 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5163 processSync(mapper);
5164
5165 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5166 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5167 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5168
5169 processUp(mapper);
5170 processSync(mapper);
5171 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5172}
5173
5174TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5175 addConfigurationProperty("touch.deviceType", "touchScreen");
5176 prepareButtons();
5177 prepareAxes(POSITION);
5178 addConfigurationProperty("touch.orientationAware", "1");
5179 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5180 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005181 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005182 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005183 NotifyMotionArgs args;
5184
5185 // Orientation 180.
5186 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5187 processSync(mapper);
5188
5189 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5190 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5191 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5192
5193 processUp(mapper);
5194 processSync(mapper);
5195 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5196}
5197
5198TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5199 addConfigurationProperty("touch.deviceType", "touchScreen");
5200 prepareButtons();
5201 prepareAxes(POSITION);
5202 addConfigurationProperty("touch.orientationAware", "1");
5203 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5204 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005205 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005206 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005207 NotifyMotionArgs args;
5208
5209 // Orientation 270.
5210 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5211 processSync(mapper);
5212
5213 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5214 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5215 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5216
5217 processUp(mapper);
5218 processSync(mapper);
5219 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5220}
5221
5222TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5223 addConfigurationProperty("touch.deviceType", "touchScreen");
5224 prepareButtons();
5225 prepareAxes(POSITION);
5226 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5227 // orientation-aware are affected by display rotation.
5228 addConfigurationProperty("touch.orientationAware", "0");
5229 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005230 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005231
5232 NotifyMotionArgs args;
5233
5234 // Orientation 90, Rotation 0.
5235 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005236 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005237 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5238 processSync(mapper);
5239
5240 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5241 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5242 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5243
5244 processUp(mapper);
5245 processSync(mapper);
5246 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5247
5248 // Orientation 90, Rotation 90.
5249 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005250 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005251 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005252 processSync(mapper);
5253
5254 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5255 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5256 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5257
5258 processUp(mapper);
5259 processSync(mapper);
5260 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5261
5262 // Orientation 90, Rotation 180.
5263 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005264 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005265 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5266 processSync(mapper);
5267
5268 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5269 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5270 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5271
5272 processUp(mapper);
5273 processSync(mapper);
5274 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5275
5276 // Orientation 90, Rotation 270.
5277 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005278 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005279 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005280 processSync(mapper);
5281
5282 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5283 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5284 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5285
5286 processUp(mapper);
5287 processSync(mapper);
5288 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5289}
5290
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005291TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5292 addConfigurationProperty("touch.deviceType", "touchScreen");
5293 prepareButtons();
5294 prepareAxes(POSITION);
5295 addConfigurationProperty("touch.orientationAware", "1");
5296 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005297 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005298
5299 // Set a physical frame in the display viewport.
5300 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5301 viewport->physicalLeft = 20;
5302 viewport->physicalTop = 600;
5303 viewport->physicalRight = 30;
5304 viewport->physicalBottom = 610;
5305 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005306 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005307
5308 // Start the touch.
5309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5310 processSync(mapper);
5311
5312 // Expect all input starting outside the physical frame to be ignored.
5313 const std::array<Point, 6> outsidePoints = {
5314 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5315 for (const auto& p : outsidePoints) {
5316 processMove(mapper, toRawX(p.x), toRawY(p.y));
5317 processSync(mapper);
5318 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5319 }
5320
5321 // Move the touch into the physical frame.
5322 processMove(mapper, toRawX(25), toRawY(605));
5323 processSync(mapper);
5324 NotifyMotionArgs args;
5325 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5326 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5327 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5328 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5329
5330 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5331 for (const auto& p : outsidePoints) {
5332 processMove(mapper, toRawX(p.x), toRawY(p.y));
5333 processSync(mapper);
5334 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5335 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5336 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5337 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5338 }
5339
5340 processUp(mapper);
5341 processSync(mapper);
5342 EXPECT_NO_FATAL_FAILURE(
5343 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5344}
5345
Harry Cutts1db43992023-06-19 17:05:07 +00005346TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
Harry Cutts1db43992023-06-19 17:05:07 +00005347 addConfigurationProperty("touch.deviceType", "pointer");
5348 prepareAxes(POSITION);
5349 prepareDisplay(ui::ROTATION_0);
5350 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5351
5352 // Set a physical frame in the display viewport.
5353 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5354 viewport->physicalLeft = 20;
5355 viewport->physicalTop = 600;
5356 viewport->physicalRight = 30;
5357 viewport->physicalBottom = 610;
5358 mFakePolicy->updateViewport(*viewport);
5359 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5360
5361 // Start the touch.
5362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5363 processSync(mapper);
5364
5365 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5366 // produced.
5367 const std::array<Point, 6> outsidePoints = {
5368 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5369 for (const auto& p : outsidePoints) {
5370 processMove(mapper, toRawX(p.x), toRawY(p.y));
5371 processSync(mapper);
5372 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5373 }
5374}
5375
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005377 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005378 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005379 prepareButtons();
5380 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005381 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005382
5383 // These calculations are based on the input device calibration documentation.
5384 int32_t rawX = 100;
5385 int32_t rawY = 200;
5386 int32_t rawPressure = 10;
5387 int32_t rawToolMajor = 12;
5388 int32_t rawDistance = 2;
5389 int32_t rawTiltX = 30;
5390 int32_t rawTiltY = 110;
5391
5392 float x = toDisplayX(rawX);
5393 float y = toDisplayY(rawY);
5394 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5395 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5396 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5397 float distance = float(rawDistance);
5398
5399 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5400 float tiltScale = M_PI / 180;
5401 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5402 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5403 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5404 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5405
5406 processDown(mapper, rawX, rawY);
5407 processPressure(mapper, rawPressure);
5408 processToolMajor(mapper, rawToolMajor);
5409 processDistance(mapper, rawDistance);
5410 processTilt(mapper, rawTiltX, rawTiltY);
5411 processSync(mapper);
5412
5413 NotifyMotionArgs args;
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5416 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5417 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5418}
5419
Jason Gerecke489fda82012-09-07 17:19:40 -07005420TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005421 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005422 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005423 prepareLocationCalibration();
5424 prepareButtons();
5425 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005426 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005427
5428 int32_t rawX = 100;
5429 int32_t rawY = 200;
5430
5431 float x = toDisplayX(toCookedX(rawX, rawY));
5432 float y = toDisplayY(toCookedY(rawX, rawY));
5433
5434 processDown(mapper, rawX, rawY);
5435 processSync(mapper);
5436
5437 NotifyMotionArgs args;
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5440 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5441}
5442
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005444 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005445 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446 prepareButtons();
5447 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005448 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449
5450 NotifyMotionArgs motionArgs;
5451 NotifyKeyArgs keyArgs;
5452
5453 processDown(mapper, 100, 200);
5454 processSync(mapper);
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5456 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5457 ASSERT_EQ(0, motionArgs.buttonState);
5458
5459 // press BTN_LEFT, release BTN_LEFT
5460 processKey(mapper, BTN_LEFT, 1);
5461 processSync(mapper);
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5464 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5465
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5468 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5469
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470 processKey(mapper, BTN_LEFT, 0);
5471 processSync(mapper);
5472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005473 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005475
5476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005478 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005479
5480 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5481 processKey(mapper, BTN_RIGHT, 1);
5482 processKey(mapper, BTN_MIDDLE, 1);
5483 processSync(mapper);
5484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5485 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5486 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5487 motionArgs.buttonState);
5488
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5490 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5491 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5492
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5495 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5496 motionArgs.buttonState);
5497
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 processKey(mapper, BTN_RIGHT, 0);
5499 processSync(mapper);
5500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005501 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005503
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005506 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005507
5508 processKey(mapper, BTN_MIDDLE, 0);
5509 processSync(mapper);
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005511 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005513
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005516 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517
5518 // press BTN_BACK, release BTN_BACK
5519 processKey(mapper, BTN_BACK, 1);
5520 processSync(mapper);
5521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5522 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5523 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005524
Michael Wrightd02c5b62014-02-10 15:10:22 -08005525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005527 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5528
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5531 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532
5533 processKey(mapper, BTN_BACK, 0);
5534 processSync(mapper);
5535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005538
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005541 ASSERT_EQ(0, motionArgs.buttonState);
5542
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5544 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5545 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5546
5547 // press BTN_SIDE, release BTN_SIDE
5548 processKey(mapper, BTN_SIDE, 1);
5549 processSync(mapper);
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5551 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5552 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005553
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005556 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5557
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5560 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561
5562 processKey(mapper, BTN_SIDE, 0);
5563 processSync(mapper);
5564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005565 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005567
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005570 ASSERT_EQ(0, motionArgs.buttonState);
5571
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5573 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5574 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5575
5576 // press BTN_FORWARD, release BTN_FORWARD
5577 processKey(mapper, BTN_FORWARD, 1);
5578 processSync(mapper);
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5580 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5581 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005582
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005585 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5589 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590
5591 processKey(mapper, BTN_FORWARD, 0);
5592 processSync(mapper);
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005596
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005599 ASSERT_EQ(0, motionArgs.buttonState);
5600
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5602 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5603 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5604
5605 // press BTN_EXTRA, release BTN_EXTRA
5606 processKey(mapper, BTN_EXTRA, 1);
5607 processSync(mapper);
5608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5609 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5610 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005611
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005614 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5615
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5618 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005619
5620 processKey(mapper, BTN_EXTRA, 0);
5621 processSync(mapper);
5622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005625
5626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005628 ASSERT_EQ(0, motionArgs.buttonState);
5629
Michael Wrightd02c5b62014-02-10 15:10:22 -08005630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5631 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5632 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5633
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5635
Michael Wrightd02c5b62014-02-10 15:10:22 -08005636 // press BTN_STYLUS, release BTN_STYLUS
5637 processKey(mapper, BTN_STYLUS, 1);
5638 processSync(mapper);
5639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005641 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5642
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5645 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005646
5647 processKey(mapper, BTN_STYLUS, 0);
5648 processSync(mapper);
5649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005650 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005652
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005655 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005656
5657 // press BTN_STYLUS2, release BTN_STYLUS2
5658 processKey(mapper, BTN_STYLUS2, 1);
5659 processSync(mapper);
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5661 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005662 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5663
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5666 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667
5668 processKey(mapper, BTN_STYLUS2, 0);
5669 processSync(mapper);
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005671 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005672 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005673
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005675 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005676 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005677
5678 // release touch
5679 processUp(mapper);
5680 processSync(mapper);
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5682 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5683 ASSERT_EQ(0, motionArgs.buttonState);
5684}
5685
5686TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005687 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005688 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005689 prepareButtons();
5690 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005691 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692
5693 NotifyMotionArgs motionArgs;
5694
5695 // default tool type is finger
5696 processDown(mapper, 100, 200);
5697 processSync(mapper);
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5699 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005700 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005701
5702 // eraser
5703 processKey(mapper, BTN_TOOL_RUBBER, 1);
5704 processSync(mapper);
5705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5706 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005707 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708
5709 // stylus
5710 processKey(mapper, BTN_TOOL_RUBBER, 0);
5711 processKey(mapper, BTN_TOOL_PEN, 1);
5712 processSync(mapper);
5713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005715 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005716
5717 // brush
5718 processKey(mapper, BTN_TOOL_PEN, 0);
5719 processKey(mapper, BTN_TOOL_BRUSH, 1);
5720 processSync(mapper);
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005723 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724
5725 // pencil
5726 processKey(mapper, BTN_TOOL_BRUSH, 0);
5727 processKey(mapper, BTN_TOOL_PENCIL, 1);
5728 processSync(mapper);
5729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005731 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005732
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005733 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734 processKey(mapper, BTN_TOOL_PENCIL, 0);
5735 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5736 processSync(mapper);
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5738 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005739 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005740
5741 // mouse
5742 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5743 processKey(mapper, BTN_TOOL_MOUSE, 1);
5744 processSync(mapper);
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005747 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005748
5749 // lens
5750 processKey(mapper, BTN_TOOL_MOUSE, 0);
5751 processKey(mapper, BTN_TOOL_LENS, 1);
5752 processSync(mapper);
5753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5754 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005755 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005756
5757 // double-tap
5758 processKey(mapper, BTN_TOOL_LENS, 0);
5759 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5760 processSync(mapper);
5761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005763 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005764
5765 // triple-tap
5766 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5767 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5768 processSync(mapper);
5769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005771 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772
5773 // quad-tap
5774 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5775 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5776 processSync(mapper);
5777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005779 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005780
5781 // finger
5782 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5783 processKey(mapper, BTN_TOOL_FINGER, 1);
5784 processSync(mapper);
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005787 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005788
5789 // stylus trumps finger
5790 processKey(mapper, BTN_TOOL_PEN, 1);
5791 processSync(mapper);
5792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005794 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795
5796 // eraser trumps stylus
5797 processKey(mapper, BTN_TOOL_RUBBER, 1);
5798 processSync(mapper);
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005801 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802
5803 // mouse trumps eraser
5804 processKey(mapper, BTN_TOOL_MOUSE, 1);
5805 processSync(mapper);
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005808 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005809
5810 // back to default tool type
5811 processKey(mapper, BTN_TOOL_MOUSE, 0);
5812 processKey(mapper, BTN_TOOL_RUBBER, 0);
5813 processKey(mapper, BTN_TOOL_PEN, 0);
5814 processKey(mapper, BTN_TOOL_FINGER, 0);
5815 processSync(mapper);
5816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005818 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819}
5820
5821TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005823 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005824 prepareButtons();
5825 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005826 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005827 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828
5829 NotifyMotionArgs motionArgs;
5830
5831 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5832 processKey(mapper, BTN_TOOL_FINGER, 1);
5833 processMove(mapper, 100, 200);
5834 processSync(mapper);
5835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5836 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5838 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5839
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5841 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5843 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5844
5845 // move a little
5846 processMove(mapper, 150, 250);
5847 processSync(mapper);
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5851 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5852
5853 // down when BTN_TOUCH is pressed, pressure defaults to 1
5854 processKey(mapper, BTN_TOUCH, 1);
5855 processSync(mapper);
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5859 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5863 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5864 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5865
5866 // up when BTN_TOUCH is released, hover restored
5867 processKey(mapper, BTN_TOUCH, 0);
5868 processSync(mapper);
5869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5870 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5872 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5873
5874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5875 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5877 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5878
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5880 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5882 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5883
5884 // exit hover when pointer goes away
5885 processKey(mapper, BTN_TOOL_FINGER, 0);
5886 processSync(mapper);
5887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5888 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5890 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5891}
5892
5893TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005895 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896 prepareButtons();
5897 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005898 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899
5900 NotifyMotionArgs motionArgs;
5901
5902 // initially hovering because pressure is 0
5903 processDown(mapper, 100, 200);
5904 processPressure(mapper, 0);
5905 processSync(mapper);
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5907 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5909 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5910
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5912 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5914 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5915
5916 // move a little
5917 processMove(mapper, 150, 250);
5918 processSync(mapper);
5919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5920 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5922 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5923
5924 // down when pressure is non-zero
5925 processPressure(mapper, RAW_PRESSURE_MAX);
5926 processSync(mapper);
5927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5928 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5930 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5931
5932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5933 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5935 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5936
5937 // up when pressure becomes 0, hover restored
5938 processPressure(mapper, 0);
5939 processSync(mapper);
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5941 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5943 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5944
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5946 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5948 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5949
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5951 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5953 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5954
5955 // exit hover when pointer goes away
5956 processUp(mapper);
5957 processSync(mapper);
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5959 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5960 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5961 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5962}
5963
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005964TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
5965 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005966 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005967 prepareButtons();
5968 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005969 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005970
5971 // Touch down.
5972 processDown(mapper, 100, 200);
5973 processPressure(mapper, 1);
5974 processSync(mapper);
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5976 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
5977
5978 // Reset the mapper. This should cancel the ongoing gesture.
5979 resetMapper(mapper, ARBITRARY_TIME);
5980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5981 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5984}
5985
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005986TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
5987 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005988 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005989 prepareButtons();
5990 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005991 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005992
5993 // Set the initial state for the touch pointer.
5994 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
5995 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
5996 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
5997 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
5998
5999 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006000 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6001 // does not generate any events.
6002 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006003
6004 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6005 // the recreated touch state to generate a down event.
6006 processSync(mapper);
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6008 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6009
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6011}
6012
lilinnan687e58f2022-07-19 16:00:50 +08006013TEST_F(SingleTouchInputMapperTest,
6014 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6015 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006016 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006017 prepareButtons();
6018 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006019 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006020 NotifyMotionArgs motionArgs;
6021
6022 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006023 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006024 processSync(mapper);
6025
6026 // We should receive a down event
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6029
6030 // Change display id
6031 clearViewports();
6032 prepareSecondaryDisplay(ViewportType::INTERNAL);
6033
6034 // We should receive a cancel event
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6036 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6037 // Then receive reset called
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6039}
6040
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006041TEST_F(SingleTouchInputMapperTest,
6042 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6043 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006044 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006045 prepareButtons();
6046 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006047 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6049 NotifyMotionArgs motionArgs;
6050
6051 // Start a new gesture.
6052 processDown(mapper, 100, 200);
6053 processSync(mapper);
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6056
6057 // Make the viewport inactive. This will put the device in disabled mode.
6058 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6059 viewport->isActive = false;
6060 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006061 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006062
6063 // We should receive a cancel event for the ongoing gesture.
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6065 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6066 // Then we should be notified that the device was reset.
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6068
6069 // No events are generated while the viewport is inactive.
6070 processMove(mapper, 101, 201);
6071 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006072 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006073 processSync(mapper);
6074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6075
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006076 // Start a new gesture while the viewport is still inactive.
6077 processDown(mapper, 300, 400);
6078 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6079 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6080 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6081 processSync(mapper);
6082
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006083 // Make the viewport active again. The device should resume processing events.
6084 viewport->isActive = true;
6085 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006086 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006087
6088 // The device is reset because it changes back to direct mode, without generating any events.
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6091
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006092 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006093 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6095 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006096
6097 // No more events.
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6100}
6101
Prabir Pradhan211ba622022-10-31 21:09:21 +00006102TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6103 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006104 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006105 prepareButtons();
6106 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006107 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6109
6110 // Press a stylus button.
6111 processKey(mapper, BTN_STYLUS, 1);
6112 processSync(mapper);
6113
6114 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6115 processDown(mapper, 100, 200);
6116 processSync(mapper);
6117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6118 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6119 WithCoords(toDisplayX(100), toDisplayY(200)),
6120 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6122 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6123 WithCoords(toDisplayX(100), toDisplayY(200)),
6124 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6125
6126 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6127 // the button has not actually been released, since there will be no pointers through which the
6128 // button state can be reported. The event is generated at the location of the pointer before
6129 // it went up.
6130 processUp(mapper);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6133 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6134 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6136 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6137 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6138}
6139
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006140TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6141 addConfigurationProperty("touch.deviceType", "touchScreen");
6142 prepareDisplay(ui::ROTATION_0);
6143 prepareButtons();
6144 prepareAxes(POSITION);
6145
6146 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6147
Arpit Singha8c236b2023-04-25 13:56:05 +00006148 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6150
6151 // Press a stylus button.
6152 processKey(mapper, BTN_STYLUS, 1);
6153 processSync(mapper);
6154
6155 // Start a touch gesture and ensure that the stylus button is not reported.
6156 processDown(mapper, 100, 200);
6157 processSync(mapper);
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6159 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6160
6161 // Release and press the stylus button again.
6162 processKey(mapper, BTN_STYLUS, 0);
6163 processSync(mapper);
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6165 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6166 processKey(mapper, BTN_STYLUS, 1);
6167 processSync(mapper);
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6169 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6170
6171 // Release the touch gesture.
6172 processUp(mapper);
6173 processSync(mapper);
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6175 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6176
6177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6178}
6179
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006180TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6181 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6182 prepareDisplay(ui::ROTATION_0);
6183 prepareButtons();
6184 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006185 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6187
6188 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6189}
6190
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006191TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6192 // Initialize the device without setting device source to touch navigation.
6193 addConfigurationProperty("touch.deviceType", "touchScreen");
6194 prepareDisplay(ui::ROTATION_0);
6195 prepareButtons();
6196 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006197 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006198
6199 // Ensure that the device is created as a touchscreen, not touch navigation.
6200 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6201
6202 // Add device type association after the device was created.
6203 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6204
6205 // Send update to the mapper.
6206 std::list<NotifyArgs> unused2 =
6207 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006208 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006209
6210 // Check whether device type update was successful.
6211 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6212}
6213
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006214TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6215 // Initialize the device without setting device source to touch navigation.
6216 addConfigurationProperty("touch.deviceType", "touchScreen");
6217 prepareDisplay(ui::ROTATION_0);
6218 prepareButtons();
6219 prepareAxes(POSITION);
6220 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6221
6222 // Set a physical frame in the display viewport.
6223 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6224 viewport->physicalLeft = 0;
6225 viewport->physicalTop = 0;
6226 viewport->physicalRight = DISPLAY_WIDTH / 2;
6227 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6228 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006229 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006230
Arpit Singha8c236b2023-04-25 13:56:05 +00006231 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006232
6233 // Hovering inside the physical frame produces events.
6234 processKey(mapper, BTN_TOOL_PEN, 1);
6235 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6238 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6240 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6241
6242 // Leaving the physical frame ends the hovering gesture.
6243 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6246 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6247
6248 // Moving outside the physical frame does not produce events.
6249 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6250 processSync(mapper);
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6252
6253 // Re-entering the physical frame produces events.
6254 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6257 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6259 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6260}
6261
Prabir Pradhan5632d622021-09-06 07:57:20 -07006262// --- TouchDisplayProjectionTest ---
6263
6264class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6265public:
6266 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6267 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6268 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006269 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6270 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6271 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006272 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006273 auto rotatedWidth = naturalDisplayWidth;
6274 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006275 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006276 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006277 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006278 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006279 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006280 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006281 inverseRotationFlags = ui::Transform::ROT_180;
6282 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006283 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006284 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006285 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006286 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006287 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006288 inverseRotationFlags = ui::Transform::ROT_0;
6289 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006290 }
6291
Prabir Pradhana9df3162022-12-05 23:57:27 +00006292 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006293 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6294
6295 std::optional<DisplayViewport> internalViewport =
6296 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6297 DisplayViewport& v = *internalViewport;
6298 v.displayId = DISPLAY_ID;
6299 v.orientation = orientation;
6300
6301 v.logicalLeft = 0;
6302 v.logicalTop = 0;
6303 v.logicalRight = 100;
6304 v.logicalBottom = 100;
6305
6306 v.physicalLeft = rotatedPhysicalDisplay.left;
6307 v.physicalTop = rotatedPhysicalDisplay.top;
6308 v.physicalRight = rotatedPhysicalDisplay.right;
6309 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6310
Prabir Pradhana9df3162022-12-05 23:57:27 +00006311 v.deviceWidth = rotatedWidth;
6312 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006313
6314 v.isActive = true;
6315 v.uniqueId = UNIQUE_ID;
6316 v.type = ViewportType::INTERNAL;
6317 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006318 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006319 }
6320
6321 void assertReceivedMove(const Point& point) {
6322 NotifyMotionArgs motionArgs;
6323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006325 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6327 1, 0, 0, 0, 0, 0, 0, 0));
6328 }
6329};
6330
6331TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6332 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006333 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006334
6335 prepareButtons();
6336 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006337 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006338
6339 NotifyMotionArgs motionArgs;
6340
6341 // Configure the DisplayViewport such that the logical display maps to a subsection of
6342 // the display panel called the physical display. Here, the physical display is bounded by the
6343 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6344 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6345 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6346 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6347
Michael Wrighta9cf4192022-12-01 23:46:39 +00006348 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006349 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6350
6351 // Touches outside the physical display should be ignored, and should not generate any
6352 // events. Ensure touches at the following points that lie outside of the physical display
6353 // area do not generate any events.
6354 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6355 processDown(mapper, toRawX(point.x), toRawY(point.y));
6356 processSync(mapper);
6357 processUp(mapper);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6360 << "Unexpected event generated for touch outside physical display at point: "
6361 << point.x << ", " << point.y;
6362 }
6363 }
6364}
6365
6366TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6367 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006368 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006369
6370 prepareButtons();
6371 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006372 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006373
6374 NotifyMotionArgs motionArgs;
6375
6376 // Configure the DisplayViewport such that the logical display maps to a subsection of
6377 // the display panel called the physical display. Here, the physical display is bounded by the
6378 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6379 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6380
Michael Wrighta9cf4192022-12-01 23:46:39 +00006381 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006382 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6383
6384 // Touches that start outside the physical display should be ignored until it enters the
6385 // physical display bounds, at which point it should generate a down event. Start a touch at
6386 // the point (5, 100), which is outside the physical display bounds.
6387 static const Point kOutsidePoint{5, 100};
6388 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6389 processSync(mapper);
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6391
6392 // Move the touch into the physical display area. This should generate a pointer down.
6393 processMove(mapper, toRawX(11), toRawY(21));
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006397 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006398 ASSERT_NO_FATAL_FAILURE(
6399 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6400
6401 // Move the touch inside the physical display area. This should generate a pointer move.
6402 processMove(mapper, toRawX(69), toRawY(159));
6403 processSync(mapper);
6404 assertReceivedMove({69, 159});
6405
6406 // Move outside the physical display area. Since the pointer is already down, this should
6407 // now continue generating events.
6408 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6409 processSync(mapper);
6410 assertReceivedMove(kOutsidePoint);
6411
6412 // Release. This should generate a pointer up.
6413 processUp(mapper);
6414 processSync(mapper);
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6416 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6417 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6418 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6419
6420 // Ensure no more events were generated.
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6423 }
6424}
6425
Prabir Pradhana9df3162022-12-05 23:57:27 +00006426// --- TouchscreenPrecisionTests ---
6427
6428// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6429// in various orientations and with different display rotations. We configure the touchscreen to
6430// have a higher resolution than that of the display by an integer scale factor in each axis so that
6431// we can enforce that coordinates match precisely as expected.
6432class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6433 public ::testing::WithParamInterface<ui::Rotation> {
6434public:
6435 void SetUp() override {
6436 SingleTouchInputMapperTest::SetUp();
6437
6438 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6439 // four times the resolution of the display in the Y axis.
6440 prepareButtons();
6441 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006442 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6443 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006444 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006445 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6446 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006447 }
6448
6449 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6450 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6451 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6452 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6453
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006454 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6455 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6456
6457 static const int32_t PRECISION_RAW_X_FLAT = 16;
6458 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6459
6460 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6461 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6462
Prabir Pradhana9df3162022-12-05 23:57:27 +00006463 static const std::array<Point, 4> kRawCorners;
6464};
6465
6466const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6467 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6468 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6469 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6470 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6471}};
6472
6473// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6474// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6475// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6476TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6477 enum class Orientation {
6478 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6479 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6480 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6481 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6482 ftl_last = ORIENTATION_270,
6483 };
6484 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6485 Orientation::ORIENTATION_270;
6486 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6487 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6488 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6489 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6490 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6491 };
6492
6493 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6494
6495 // Configure the touchscreen as being installed in the one of the four different orientations
6496 // relative to the display.
6497 addConfigurationProperty("touch.deviceType", "touchScreen");
6498 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6499 prepareDisplay(ui::ROTATION_0);
6500
Arpit Singha8c236b2023-04-25 13:56:05 +00006501 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006502
6503 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6504 // orientations of either 90 or 270) this means the display's natural resolution will be
6505 // flipped.
6506 const bool displayRotated =
6507 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6508 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6509 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6510 const Rect physicalFrame{0, 0, width, height};
6511 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6512
6513 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6514 const float expectedPrecisionX = displayRotated ? 4 : 2;
6515 const float expectedPrecisionY = displayRotated ? 2 : 4;
6516
6517 // Test all four corners.
6518 for (int i = 0; i < 4; i++) {
6519 const auto& raw = kRawCorners[i];
6520 processDown(mapper, raw.x, raw.y);
6521 processSync(mapper);
6522 const auto& expected = expectedPoints[i];
6523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6524 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6525 WithCoords(expected.x, expected.y),
6526 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6527 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6528 << "with touchscreen orientation "
6529 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6530 << expected.x << ", " << expected.y << ").";
6531 processUp(mapper);
6532 processSync(mapper);
6533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6534 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6535 WithCoords(expected.x, expected.y))));
6536 }
6537}
6538
Prabir Pradhan82687402022-12-06 01:32:53 +00006539TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6540 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6541 kMappedCorners = {
6542 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6543 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6544 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6545 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6546 };
6547
6548 const ui::Rotation displayRotation = GetParam();
6549
6550 addConfigurationProperty("touch.deviceType", "touchScreen");
6551 prepareDisplay(displayRotation);
6552
Arpit Singha8c236b2023-04-25 13:56:05 +00006553 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006554
6555 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6556
6557 // Test all four corners.
6558 for (int i = 0; i < 4; i++) {
6559 const auto& expected = expectedPoints[i];
6560 const auto& raw = kRawCorners[i];
6561 processDown(mapper, raw.x, raw.y);
6562 processSync(mapper);
6563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6564 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6565 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6566 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6567 << "with display rotation " << ui::toCString(displayRotation)
6568 << ", expected point (" << expected.x << ", " << expected.y << ").";
6569 processUp(mapper);
6570 processSync(mapper);
6571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6572 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6573 WithCoords(expected.x, expected.y))));
6574 }
6575}
6576
Prabir Pradhan3e798762022-12-02 21:02:11 +00006577TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6578 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6579 kMappedCorners = {
6580 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6581 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6582 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6583 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6584 };
6585
6586 const ui::Rotation displayRotation = GetParam();
6587
6588 addConfigurationProperty("touch.deviceType", "touchScreen");
6589 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6590
Arpit Singha8c236b2023-04-25 13:56:05 +00006591 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006592
6593 // Ori 270, so width and height swapped
6594 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6595 prepareDisplay(displayRotation);
6596 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6597
6598 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6599
6600 // Test all four corners.
6601 for (int i = 0; i < 4; i++) {
6602 const auto& expected = expectedPoints[i];
6603 const auto& raw = kRawCorners[i];
6604 processDown(mapper, raw.x, raw.y);
6605 processSync(mapper);
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6607 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6608 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6609 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6610 << "with display rotation " << ui::toCString(displayRotation)
6611 << ", expected point (" << expected.x << ", " << expected.y << ").";
6612 processUp(mapper);
6613 processSync(mapper);
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6615 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6616 WithCoords(expected.x, expected.y))));
6617 }
6618}
6619
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006620TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6621 const ui::Rotation displayRotation = GetParam();
6622
6623 addConfigurationProperty("touch.deviceType", "touchScreen");
6624 prepareDisplay(displayRotation);
6625
6626 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006627 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006628
6629 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6630 // MotionRanges use display pixels as their units
6631 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6632 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6633
6634 // The MotionRanges should be oriented in the rotated display's coordinate space
6635 const bool displayRotated =
6636 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6637
6638 constexpr float MAX_X = 479.5;
6639 constexpr float MAX_Y = 799.75;
6640 EXPECT_EQ(xRange->min, 0.f);
6641 EXPECT_EQ(yRange->min, 0.f);
6642 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6643 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6644
6645 EXPECT_EQ(xRange->flat, 8.f);
6646 EXPECT_EQ(yRange->flat, 8.f);
6647
6648 EXPECT_EQ(xRange->fuzz, 2.f);
6649 EXPECT_EQ(yRange->fuzz, 2.f);
6650
6651 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6652 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6653}
6654
Prabir Pradhana9df3162022-12-05 23:57:27 +00006655// Run the precision tests for all rotations.
6656INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6657 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6658 ui::ROTATION_270),
6659 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6660 return ftl::enum_string(testParamInfo.param);
6661 });
6662
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006663// --- ExternalStylusFusionTest ---
6664
6665class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6666public:
6667 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6668 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006669 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006670 prepareButtons();
6671 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006672 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006673
6674 mStylusState.when = ARBITRARY_TIME;
6675 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006676 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006677 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006678 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006679 processExternalStylusState(mapper);
6680 return mapper;
6681 }
6682
6683 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6684 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6685 for (const NotifyArgs& args : generatedArgs) {
6686 mFakeListener->notify(args);
6687 }
6688 // Loop the reader to flush the input listener queue.
6689 mReader->loopOnce();
6690 return generatedArgs;
6691 }
6692
6693protected:
6694 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006695
6696 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6697 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006698 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006699
6700 // The first pointer is withheld.
6701 processDown(mapper, 100, 200);
6702 processSync(mapper);
6703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6704 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6705 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6706
6707 // The external stylus reports pressure. The withheld finger pointer is released as a
6708 // stylus.
6709 mStylusState.pressure = 1.f;
6710 processExternalStylusState(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6712 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6713 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6714
6715 // Subsequent pointer events are not withheld.
6716 processMove(mapper, 101, 201);
6717 processSync(mapper);
6718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6719 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6720
6721 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6723 }
6724
6725 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6726 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6727
6728 // Releasing the touch pointer ends the gesture.
6729 processUp(mapper);
6730 processSync(mapper);
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006732 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006733 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006734
6735 mStylusState.pressure = 0.f;
6736 processExternalStylusState(mapper);
6737 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6739 }
6740
6741 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006742 // When stylus fusion is not successful, events should be reported with the original source.
6743 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006744 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006745 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006746
6747 // The first pointer is withheld when an external stylus is connected,
6748 // and a timeout is requested.
6749 processDown(mapper, 100, 200);
6750 processSync(mapper);
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6752 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6753 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6754
6755 // If the timeout expires early, it is requested again.
6756 handleTimeout(mapper, ARBITRARY_TIME + 1);
6757 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6758 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6759
6760 // When the timeout expires, the withheld touch is released as a finger pointer.
6761 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6763 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6764
6765 // Subsequent pointer events are not withheld.
6766 processMove(mapper, 101, 201);
6767 processSync(mapper);
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6769 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6770 processUp(mapper);
6771 processSync(mapper);
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6773 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6774
6775 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6777 }
6778
6779private:
6780 InputDeviceInfo mExternalStylusDeviceInfo{};
6781};
6782
6783TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6784 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006785 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006786}
6787
6788TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6789 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6790 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6791}
6792
6793TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6794 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6795 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6796}
6797
6798// Test a successful stylus fusion gesture where the pressure is reported by the external
6799// before the touch is reported by the touchscreen.
6800TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6801 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006802 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006803
6804 // The external stylus reports pressure first. It is ignored for now.
6805 mStylusState.pressure = 1.f;
6806 processExternalStylusState(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6808 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6809
6810 // When the touch goes down afterwards, it is reported as a stylus pointer.
6811 processDown(mapper, 100, 200);
6812 processSync(mapper);
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6814 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6815 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6816
6817 processMove(mapper, 101, 201);
6818 processSync(mapper);
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6820 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6821 processUp(mapper);
6822 processSync(mapper);
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6824 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6825
6826 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6828}
6829
6830TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6831 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6832
6833 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6834 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6835
6836 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6837 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6838 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6839 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6840}
6841
6842TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6843 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006844 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006845
6846 mStylusState.pressure = 0.8f;
6847 processExternalStylusState(mapper);
6848 processDown(mapper, 100, 200);
6849 processSync(mapper);
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6851 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6852 WithPressure(0.8f))));
6853 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6854
6855 // The external stylus reports a pressure change. We wait for some time for a touch event.
6856 mStylusState.pressure = 0.6f;
6857 processExternalStylusState(mapper);
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6859 ASSERT_NO_FATAL_FAILURE(
6860 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6861
6862 // If a touch is reported within the timeout, it reports the updated pressure.
6863 processMove(mapper, 101, 201);
6864 processSync(mapper);
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6866 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6867 WithPressure(0.6f))));
6868 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6869
6870 // There is another pressure change.
6871 mStylusState.pressure = 0.5f;
6872 processExternalStylusState(mapper);
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6874 ASSERT_NO_FATAL_FAILURE(
6875 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6876
6877 // If a touch is not reported within the timeout, a move event is generated to report
6878 // the new pressure.
6879 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6881 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6882 WithPressure(0.5f))));
6883
6884 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6885 // repeated indefinitely.
6886 mStylusState.pressure = 0.0f;
6887 processExternalStylusState(mapper);
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6889 ASSERT_NO_FATAL_FAILURE(
6890 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6891 processMove(mapper, 102, 202);
6892 processSync(mapper);
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6894 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6895 WithPressure(0.5f))));
6896 processMove(mapper, 103, 203);
6897 processSync(mapper);
6898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6899 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6900 WithPressure(0.5f))));
6901
6902 processUp(mapper);
6903 processSync(mapper);
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006905 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006906 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006907
6908 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6910}
6911
6912TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6913 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006914 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006915
6916 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006917 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006918 processExternalStylusState(mapper);
6919 processDown(mapper, 100, 200);
6920 processSync(mapper);
6921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6922 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006923 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006924 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6925
6926 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006927 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006928 processExternalStylusState(mapper);
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6930 ASSERT_NO_FATAL_FAILURE(
6931 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6932
6933 // If a touch is reported within the timeout, it reports the updated pressure.
6934 processMove(mapper, 101, 201);
6935 processSync(mapper);
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6937 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006938 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006939 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6940
6941 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006942 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006943 processExternalStylusState(mapper);
6944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6945 ASSERT_NO_FATAL_FAILURE(
6946 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6947
6948 // If a touch is not reported within the timeout, a move event is generated to report
6949 // the new tool type.
6950 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6952 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006953 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006954
6955 processUp(mapper);
6956 processSync(mapper);
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6958 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006959 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006960
6961 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6963}
6964
6965TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
6966 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006967 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006968
6969 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6970
6971 // The external stylus reports a button change. We wait for some time for a touch event.
6972 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
6973 processExternalStylusState(mapper);
6974 ASSERT_NO_FATAL_FAILURE(
6975 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6976
6977 // If a touch is reported within the timeout, it reports the updated button state.
6978 processMove(mapper, 101, 201);
6979 processSync(mapper);
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6981 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6982 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6984 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6985 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6986 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6987
6988 // The button is now released.
6989 mStylusState.buttons = 0;
6990 processExternalStylusState(mapper);
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6992 ASSERT_NO_FATAL_FAILURE(
6993 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6994
6995 // If a touch is not reported within the timeout, a move event is generated to report
6996 // the new button state.
6997 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6999 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7000 WithButtonState(0))));
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007002 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7003 WithButtonState(0))));
7004
7005 processUp(mapper);
7006 processSync(mapper);
7007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007008 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7009
7010 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7012}
7013
Michael Wrightd02c5b62014-02-10 15:10:22 -08007014// --- MultiTouchInputMapperTest ---
7015
7016class MultiTouchInputMapperTest : public TouchInputMapperTest {
7017protected:
7018 void prepareAxes(int axes);
7019
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007020 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7021 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7022 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7023 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7024 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7025 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7026 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7027 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7028 void processId(MultiTouchInputMapper& mapper, int32_t id);
7029 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7030 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7031 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007032 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007033 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007034 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7035 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036};
7037
7038void MultiTouchInputMapperTest::prepareAxes(int axes) {
7039 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007040 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7041 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007042 }
7043 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007044 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7045 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007046 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007047 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7048 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007049 }
7050 }
7051 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007052 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7053 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007054 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007055 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007056 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057 }
7058 }
7059 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007060 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7061 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007062 }
7063 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007064 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7065 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007066 }
7067 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007068 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7069 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070 }
7071 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007072 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7073 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074 }
7075 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007076 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7077 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007078 }
7079 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007080 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081 }
7082}
7083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007084void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7085 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7087 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007088}
7089
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007090void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7091 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007093}
7094
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007095void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7096 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007097 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098}
7099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007100void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007101 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102}
7103
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007104void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007105 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106}
7107
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007108void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7109 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111}
7112
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007113void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007115}
7116
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007117void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007119}
7120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007121void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123}
7124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007125void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127}
7128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007129void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131}
7132
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007133void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7134 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007136}
7137
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007138void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7139 int32_t value) {
7140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7142}
7143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007144void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146}
7147
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007148void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7149 nsecs_t readTime) {
7150 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151}
7152
Michael Wrightd02c5b62014-02-10 15:10:22 -08007153TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007155 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156 prepareAxes(POSITION);
7157 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007158 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159
arthurhungdcef2dc2020-08-11 14:47:50 +08007160 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161
7162 NotifyMotionArgs motionArgs;
7163
7164 // Two fingers down at once.
7165 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7166 processPosition(mapper, x1, y1);
7167 processMTSync(mapper);
7168 processPosition(mapper, x2, y2);
7169 processMTSync(mapper);
7170 processSync(mapper);
7171
7172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7173 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7174 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7175 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7176 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7177 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7178 ASSERT_EQ(0, motionArgs.flags);
7179 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7180 ASSERT_EQ(0, motionArgs.buttonState);
7181 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007182 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007183 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007184 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7186 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7187 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7188 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7189 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7190
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7192 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7193 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7194 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7195 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007196 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197 ASSERT_EQ(0, motionArgs.flags);
7198 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7199 ASSERT_EQ(0, motionArgs.buttonState);
7200 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007201 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007203 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007205 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7207 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7209 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7210 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7211 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7213
7214 // Move.
7215 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7216 processPosition(mapper, x1, y1);
7217 processMTSync(mapper);
7218 processPosition(mapper, x2, y2);
7219 processMTSync(mapper);
7220 processSync(mapper);
7221
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7224 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7226 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7228 ASSERT_EQ(0, motionArgs.flags);
7229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7230 ASSERT_EQ(0, motionArgs.buttonState);
7231 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007232 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007234 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007235 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007236 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7238 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7240 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7241 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7242 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7243 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7244
7245 // First finger up.
7246 x2 += 15; y2 -= 20;
7247 processPosition(mapper, x2, y2);
7248 processMTSync(mapper);
7249 processSync(mapper);
7250
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7253 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7254 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7255 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007256 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007257 ASSERT_EQ(0, motionArgs.flags);
7258 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7259 ASSERT_EQ(0, motionArgs.buttonState);
7260 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007261 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007262 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007263 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007264 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007265 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7267 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7268 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7269 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7270 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7271 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7272 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7273
7274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7275 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7276 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7277 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7278 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7279 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7280 ASSERT_EQ(0, motionArgs.flags);
7281 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7282 ASSERT_EQ(0, motionArgs.buttonState);
7283 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007284 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007285 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007286 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7288 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7289 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7290 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7292
7293 // Move.
7294 x2 += 20; y2 -= 25;
7295 processPosition(mapper, x2, y2);
7296 processMTSync(mapper);
7297 processSync(mapper);
7298
7299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7301 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7302 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7303 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7305 ASSERT_EQ(0, motionArgs.flags);
7306 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7307 ASSERT_EQ(0, motionArgs.buttonState);
7308 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007309 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007311 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7313 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7314 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7315 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7317
7318 // New finger down.
7319 int32_t x3 = 700, y3 = 300;
7320 processPosition(mapper, x2, y2);
7321 processMTSync(mapper);
7322 processPosition(mapper, x3, y3);
7323 processMTSync(mapper);
7324 processSync(mapper);
7325
7326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7328 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7329 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7330 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007331 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007332 ASSERT_EQ(0, motionArgs.flags);
7333 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7334 ASSERT_EQ(0, motionArgs.buttonState);
7335 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007336 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007338 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007340 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7342 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7344 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7345 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7346 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7347 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7348
7349 // Second finger up.
7350 x3 += 30; y3 -= 20;
7351 processPosition(mapper, x3, y3);
7352 processMTSync(mapper);
7353 processSync(mapper);
7354
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7356 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7357 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7358 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7359 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007360 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007361 ASSERT_EQ(0, motionArgs.flags);
7362 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7363 ASSERT_EQ(0, motionArgs.buttonState);
7364 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007365 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007366 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007367 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007368 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007369 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7371 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7372 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7373 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7374 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7375 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7376 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7377
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7379 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7380 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7381 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7382 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7384 ASSERT_EQ(0, motionArgs.flags);
7385 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7386 ASSERT_EQ(0, motionArgs.buttonState);
7387 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007388 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007389 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007390 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7392 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7393 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7394 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7395 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7396
7397 // Last finger up.
7398 processMTSync(mapper);
7399 processSync(mapper);
7400
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7403 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7404 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7405 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7406 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7407 ASSERT_EQ(0, motionArgs.flags);
7408 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7409 ASSERT_EQ(0, motionArgs.buttonState);
7410 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007411 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007412 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007413 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7415 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7416 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7417 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7419
7420 // Should not have sent any more keys or motions.
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7423}
7424
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007425TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7426 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007427 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007428
7429 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7430 /*fuzz*/ 0, /*resolution*/ 10);
7431 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7432 /*fuzz*/ 0, /*resolution*/ 11);
7433 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7434 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7435 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7436 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7437 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7438 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7439 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7440 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7441
Arpit Singha8c236b2023-04-25 13:56:05 +00007442 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007443
7444 // X and Y axes
7445 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7446 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7447 // Touch major and minor
7448 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7449 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7450 // Tool major and minor
7451 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7452 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7453}
7454
7455TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7456 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007457 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007458
7459 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7460 /*fuzz*/ 0, /*resolution*/ 10);
7461 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7462 /*fuzz*/ 0, /*resolution*/ 11);
7463
7464 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7465
Arpit Singha8c236b2023-04-25 13:56:05 +00007466 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007467
7468 // Touch major and minor
7469 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7470 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7471 // Tool major and minor
7472 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7473 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7474}
7475
Michael Wrightd02c5b62014-02-10 15:10:22 -08007476TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007477 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007478 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007479 prepareAxes(POSITION | ID);
7480 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007481 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007482
arthurhungdcef2dc2020-08-11 14:47:50 +08007483 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007484
7485 NotifyMotionArgs motionArgs;
7486
7487 // Two fingers down at once.
7488 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7489 processPosition(mapper, x1, y1);
7490 processId(mapper, 1);
7491 processMTSync(mapper);
7492 processPosition(mapper, x2, y2);
7493 processId(mapper, 2);
7494 processMTSync(mapper);
7495 processSync(mapper);
7496
7497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7498 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007499 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007500 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007501 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7503 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7504
7505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007506 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007507 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007508 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007509 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007510 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007511 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7513 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7515 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7516
7517 // Move.
7518 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7519 processPosition(mapper, x1, y1);
7520 processId(mapper, 1);
7521 processMTSync(mapper);
7522 processPosition(mapper, x2, y2);
7523 processId(mapper, 2);
7524 processMTSync(mapper);
7525 processSync(mapper);
7526
7527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007529 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007530 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007531 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007532 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007533 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7535 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7536 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7537 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7538
7539 // First finger up.
7540 x2 += 15; y2 -= 20;
7541 processPosition(mapper, x2, y2);
7542 processId(mapper, 2);
7543 processMTSync(mapper);
7544 processSync(mapper);
7545
7546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007547 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007548 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007550 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007551 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007552 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7554 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7556 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7557
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007560 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007561 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007562 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007563 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7564 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7565
7566 // Move.
7567 x2 += 20; y2 -= 25;
7568 processPosition(mapper, x2, y2);
7569 processId(mapper, 2);
7570 processMTSync(mapper);
7571 processSync(mapper);
7572
7573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007575 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007576 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007577 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7579 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7580
7581 // New finger down.
7582 int32_t x3 = 700, y3 = 300;
7583 processPosition(mapper, x2, y2);
7584 processId(mapper, 2);
7585 processMTSync(mapper);
7586 processPosition(mapper, x3, y3);
7587 processId(mapper, 3);
7588 processMTSync(mapper);
7589 processSync(mapper);
7590
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007592 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007593 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007594 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007595 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007596 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007597 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7599 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7601 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7602
7603 // Second finger up.
7604 x3 += 30; y3 -= 20;
7605 processPosition(mapper, x3, y3);
7606 processId(mapper, 3);
7607 processMTSync(mapper);
7608 processSync(mapper);
7609
7610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007611 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007612 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007613 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007614 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007615 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007616 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7618 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7620 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7621
7622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007624 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007625 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007626 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7628 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7629
7630 // Last finger up.
7631 processMTSync(mapper);
7632 processSync(mapper);
7633
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7635 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007636 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007637 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007638 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7640 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7641
7642 // Should not have sent any more keys or motions.
7643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7645}
7646
7647TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007649 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007650 prepareAxes(POSITION | ID | SLOT);
7651 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007652 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007653
arthurhungdcef2dc2020-08-11 14:47:50 +08007654 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007655
7656 NotifyMotionArgs motionArgs;
7657
7658 // Two fingers down at once.
7659 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7660 processPosition(mapper, x1, y1);
7661 processId(mapper, 1);
7662 processSlot(mapper, 1);
7663 processPosition(mapper, x2, y2);
7664 processId(mapper, 2);
7665 processSync(mapper);
7666
7667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7668 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007669 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007671 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7673 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7674
7675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007676 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007677 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007678 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007679 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007680 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007681 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7683 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7685 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7686
7687 // Move.
7688 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7689 processSlot(mapper, 0);
7690 processPosition(mapper, x1, y1);
7691 processSlot(mapper, 1);
7692 processPosition(mapper, x2, y2);
7693 processSync(mapper);
7694
7695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007697 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007698 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007699 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007700 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007701 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7703 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7705 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7706
7707 // First finger up.
7708 x2 += 15; y2 -= 20;
7709 processSlot(mapper, 0);
7710 processId(mapper, -1);
7711 processSlot(mapper, 1);
7712 processPosition(mapper, x2, y2);
7713 processSync(mapper);
7714
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007716 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007717 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007719 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007721 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7723 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7724 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7725 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7726
7727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007729 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007730 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007731 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7733 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7734
7735 // Move.
7736 x2 += 20; y2 -= 25;
7737 processPosition(mapper, x2, y2);
7738 processSync(mapper);
7739
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007742 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007743 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007744 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7746 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7747
7748 // New finger down.
7749 int32_t x3 = 700, y3 = 300;
7750 processPosition(mapper, x2, y2);
7751 processSlot(mapper, 0);
7752 processId(mapper, 3);
7753 processPosition(mapper, x3, y3);
7754 processSync(mapper);
7755
7756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007757 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007758 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007759 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007760 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007761 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007762 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7764 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7766 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7767
7768 // Second finger up.
7769 x3 += 30; y3 -= 20;
7770 processSlot(mapper, 1);
7771 processId(mapper, -1);
7772 processSlot(mapper, 0);
7773 processPosition(mapper, x3, y3);
7774 processSync(mapper);
7775
7776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007777 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007778 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007780 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007781 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007782 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7784 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7786 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7787
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007790 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007792 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7794 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7795
7796 // Last finger up.
7797 processId(mapper, -1);
7798 processSync(mapper);
7799
7800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7801 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007802 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007803 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007804 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7806 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7807
7808 // Should not have sent any more keys or motions.
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7811}
7812
7813TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007815 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007817 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818
7819 // These calculations are based on the input device calibration documentation.
7820 int32_t rawX = 100;
7821 int32_t rawY = 200;
7822 int32_t rawTouchMajor = 7;
7823 int32_t rawTouchMinor = 6;
7824 int32_t rawToolMajor = 9;
7825 int32_t rawToolMinor = 8;
7826 int32_t rawPressure = 11;
7827 int32_t rawDistance = 0;
7828 int32_t rawOrientation = 3;
7829 int32_t id = 5;
7830
7831 float x = toDisplayX(rawX);
7832 float y = toDisplayY(rawY);
7833 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7834 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7835 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7836 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7837 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7838 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7839 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7840 float distance = float(rawDistance);
7841
7842 processPosition(mapper, rawX, rawY);
7843 processTouchMajor(mapper, rawTouchMajor);
7844 processTouchMinor(mapper, rawTouchMinor);
7845 processToolMajor(mapper, rawToolMajor);
7846 processToolMinor(mapper, rawToolMinor);
7847 processPressure(mapper, rawPressure);
7848 processOrientation(mapper, rawOrientation);
7849 processDistance(mapper, rawDistance);
7850 processId(mapper, id);
7851 processMTSync(mapper);
7852 processSync(mapper);
7853
7854 NotifyMotionArgs args;
7855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7856 ASSERT_EQ(0, args.pointerProperties[0].id);
7857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7858 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7859 orientation, distance));
7860}
7861
7862TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007863 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007864 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007865 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7866 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007867 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868
7869 // These calculations are based on the input device calibration documentation.
7870 int32_t rawX = 100;
7871 int32_t rawY = 200;
7872 int32_t rawTouchMajor = 140;
7873 int32_t rawTouchMinor = 120;
7874 int32_t rawToolMajor = 180;
7875 int32_t rawToolMinor = 160;
7876
7877 float x = toDisplayX(rawX);
7878 float y = toDisplayY(rawY);
7879 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7880 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7881 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7882 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7883 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7884
7885 processPosition(mapper, rawX, rawY);
7886 processTouchMajor(mapper, rawTouchMajor);
7887 processTouchMinor(mapper, rawTouchMinor);
7888 processToolMajor(mapper, rawToolMajor);
7889 processToolMinor(mapper, rawToolMinor);
7890 processMTSync(mapper);
7891 processSync(mapper);
7892
7893 NotifyMotionArgs args;
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7896 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7897}
7898
7899TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007901 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902 prepareAxes(POSITION | TOUCH | TOOL);
7903 addConfigurationProperty("touch.size.calibration", "diameter");
7904 addConfigurationProperty("touch.size.scale", "10");
7905 addConfigurationProperty("touch.size.bias", "160");
7906 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007907 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007908
7909 // These calculations are based on the input device calibration documentation.
7910 // Note: We only provide a single common touch/tool value because the device is assumed
7911 // not to emit separate values for each pointer (isSummed = 1).
7912 int32_t rawX = 100;
7913 int32_t rawY = 200;
7914 int32_t rawX2 = 150;
7915 int32_t rawY2 = 250;
7916 int32_t rawTouchMajor = 5;
7917 int32_t rawToolMajor = 8;
7918
7919 float x = toDisplayX(rawX);
7920 float y = toDisplayY(rawY);
7921 float x2 = toDisplayX(rawX2);
7922 float y2 = toDisplayY(rawY2);
7923 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7924 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7925 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7926
7927 processPosition(mapper, rawX, rawY);
7928 processTouchMajor(mapper, rawTouchMajor);
7929 processToolMajor(mapper, rawToolMajor);
7930 processMTSync(mapper);
7931 processPosition(mapper, rawX2, rawY2);
7932 processTouchMajor(mapper, rawTouchMajor);
7933 processToolMajor(mapper, rawToolMajor);
7934 processMTSync(mapper);
7935 processSync(mapper);
7936
7937 NotifyMotionArgs args;
7938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7939 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7940
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007942 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007943 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7945 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7947 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7948}
7949
7950TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007952 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007953 prepareAxes(POSITION | TOUCH | TOOL);
7954 addConfigurationProperty("touch.size.calibration", "area");
7955 addConfigurationProperty("touch.size.scale", "43");
7956 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00007957 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958
7959 // These calculations are based on the input device calibration documentation.
7960 int32_t rawX = 100;
7961 int32_t rawY = 200;
7962 int32_t rawTouchMajor = 5;
7963 int32_t rawToolMajor = 8;
7964
7965 float x = toDisplayX(rawX);
7966 float y = toDisplayY(rawY);
7967 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7968 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7969 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7970
7971 processPosition(mapper, rawX, rawY);
7972 processTouchMajor(mapper, rawTouchMajor);
7973 processToolMajor(mapper, rawToolMajor);
7974 processMTSync(mapper);
7975 processSync(mapper);
7976
7977 NotifyMotionArgs args;
7978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7980 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7981}
7982
7983TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007985 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986 prepareAxes(POSITION | PRESSURE);
7987 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7988 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00007989 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990
Michael Wrightaa449c92017-12-13 21:21:43 +00007991 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00007992 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007993 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7994 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7995 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7996
Michael Wrightd02c5b62014-02-10 15:10:22 -08007997 // These calculations are based on the input device calibration documentation.
7998 int32_t rawX = 100;
7999 int32_t rawY = 200;
8000 int32_t rawPressure = 60;
8001
8002 float x = toDisplayX(rawX);
8003 float y = toDisplayY(rawY);
8004 float pressure = float(rawPressure) * 0.01f;
8005
8006 processPosition(mapper, rawX, rawY);
8007 processPressure(mapper, rawPressure);
8008 processMTSync(mapper);
8009 processSync(mapper);
8010
8011 NotifyMotionArgs args;
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8013 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8014 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8015}
8016
8017TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008018 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008019 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008020 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008021 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008022
8023 NotifyMotionArgs motionArgs;
8024 NotifyKeyArgs keyArgs;
8025
8026 processId(mapper, 1);
8027 processPosition(mapper, 100, 200);
8028 processSync(mapper);
8029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8030 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8031 ASSERT_EQ(0, motionArgs.buttonState);
8032
8033 // press BTN_LEFT, release BTN_LEFT
8034 processKey(mapper, BTN_LEFT, 1);
8035 processSync(mapper);
8036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8037 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8038 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8039
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8041 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8042 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8043
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044 processKey(mapper, BTN_LEFT, 0);
8045 processSync(mapper);
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008047 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008049
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008051 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008052 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008053
8054 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8055 processKey(mapper, BTN_RIGHT, 1);
8056 processKey(mapper, BTN_MIDDLE, 1);
8057 processSync(mapper);
8058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8060 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8061 motionArgs.buttonState);
8062
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8065 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8066
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8069 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8070 motionArgs.buttonState);
8071
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 processKey(mapper, BTN_RIGHT, 0);
8073 processSync(mapper);
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008075 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008076 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008077
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008080 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008081
8082 processKey(mapper, BTN_MIDDLE, 0);
8083 processSync(mapper);
8084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008085 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008086 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008087
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008089 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008090 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091
8092 // press BTN_BACK, release BTN_BACK
8093 processKey(mapper, BTN_BACK, 1);
8094 processSync(mapper);
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8096 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8097 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008098
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008101 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8102
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8105 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008106
8107 processKey(mapper, BTN_BACK, 0);
8108 processSync(mapper);
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008110 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008112
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008115 ASSERT_EQ(0, motionArgs.buttonState);
8116
Michael Wrightd02c5b62014-02-10 15:10:22 -08008117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8118 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8119 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8120
8121 // press BTN_SIDE, release BTN_SIDE
8122 processKey(mapper, BTN_SIDE, 1);
8123 processSync(mapper);
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8125 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8126 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008127
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008130 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8131
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8134 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135
8136 processKey(mapper, BTN_SIDE, 0);
8137 processSync(mapper);
8138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008139 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008141
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008144 ASSERT_EQ(0, motionArgs.buttonState);
8145
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8147 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8148 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8149
8150 // press BTN_FORWARD, release BTN_FORWARD
8151 processKey(mapper, BTN_FORWARD, 1);
8152 processSync(mapper);
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8154 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8155 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008156
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8160
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8163 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164
8165 processKey(mapper, BTN_FORWARD, 0);
8166 processSync(mapper);
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008168 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008170
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008173 ASSERT_EQ(0, motionArgs.buttonState);
8174
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8176 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8177 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8178
8179 // press BTN_EXTRA, release BTN_EXTRA
8180 processKey(mapper, BTN_EXTRA, 1);
8181 processSync(mapper);
8182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8183 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8184 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008185
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008188 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8189
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8191 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8192 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193
8194 processKey(mapper, BTN_EXTRA, 0);
8195 processSync(mapper);
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008199
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008202 ASSERT_EQ(0, motionArgs.buttonState);
8203
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8205 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8206 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8207
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8209
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210 // press BTN_STYLUS, release BTN_STYLUS
8211 processKey(mapper, BTN_STYLUS, 1);
8212 processSync(mapper);
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8214 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008215 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8216
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8219 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008220
8221 processKey(mapper, BTN_STYLUS, 0);
8222 processSync(mapper);
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008226
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008229 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008230
8231 // press BTN_STYLUS2, release BTN_STYLUS2
8232 processKey(mapper, BTN_STYLUS2, 1);
8233 processSync(mapper);
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008236 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8237
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8240 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241
8242 processKey(mapper, BTN_STYLUS2, 0);
8243 processSync(mapper);
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008250 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251
8252 // release touch
8253 processId(mapper, -1);
8254 processSync(mapper);
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8256 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8257 ASSERT_EQ(0, motionArgs.buttonState);
8258}
8259
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008260TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8261 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008262 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008263 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008264 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008265
8266 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8267 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8268
8269 // Touch down.
8270 processId(mapper, 1);
8271 processPosition(mapper, 100, 200);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8274 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8275
8276 // Press and release button mapped to the primary stylus button.
8277 processKey(mapper, BTN_A, 1);
8278 processSync(mapper);
8279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8280 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8281 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8283 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8284 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8285
8286 processKey(mapper, BTN_A, 0);
8287 processSync(mapper);
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8289 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8291 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8292
8293 // Press and release the HID usage mapped to the secondary stylus button.
8294 processHidUsage(mapper, 0xabcd, 1);
8295 processSync(mapper);
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8297 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8298 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8300 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8301 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8302
8303 processHidUsage(mapper, 0xabcd, 0);
8304 processSync(mapper);
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8306 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8308 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8309
8310 // Release touch.
8311 processId(mapper, -1);
8312 processSync(mapper);
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8314 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8315}
8316
Michael Wrightd02c5b62014-02-10 15:10:22 -08008317TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008318 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008319 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008320 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008321 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008322
8323 NotifyMotionArgs motionArgs;
8324
8325 // default tool type is finger
8326 processId(mapper, 1);
8327 processPosition(mapper, 100, 200);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008331 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008332
8333 // eraser
8334 processKey(mapper, BTN_TOOL_RUBBER, 1);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008338 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008339
8340 // stylus
8341 processKey(mapper, BTN_TOOL_RUBBER, 0);
8342 processKey(mapper, BTN_TOOL_PEN, 1);
8343 processSync(mapper);
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008346 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347
8348 // brush
8349 processKey(mapper, BTN_TOOL_PEN, 0);
8350 processKey(mapper, BTN_TOOL_BRUSH, 1);
8351 processSync(mapper);
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008354 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355
8356 // pencil
8357 processKey(mapper, BTN_TOOL_BRUSH, 0);
8358 processKey(mapper, BTN_TOOL_PENCIL, 1);
8359 processSync(mapper);
8360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008362 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008364 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008365 processKey(mapper, BTN_TOOL_PENCIL, 0);
8366 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8367 processSync(mapper);
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008370 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008371
8372 // mouse
8373 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8374 processKey(mapper, BTN_TOOL_MOUSE, 1);
8375 processSync(mapper);
8376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008378 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379
8380 // lens
8381 processKey(mapper, BTN_TOOL_MOUSE, 0);
8382 processKey(mapper, BTN_TOOL_LENS, 1);
8383 processSync(mapper);
8384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008386 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387
8388 // double-tap
8389 processKey(mapper, BTN_TOOL_LENS, 0);
8390 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8391 processSync(mapper);
8392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008394 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008395
8396 // triple-tap
8397 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8398 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8399 processSync(mapper);
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008402 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008403
8404 // quad-tap
8405 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8406 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8407 processSync(mapper);
8408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008410 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008411
8412 // finger
8413 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8414 processKey(mapper, BTN_TOOL_FINGER, 1);
8415 processSync(mapper);
8416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008418 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008419
8420 // stylus trumps finger
8421 processKey(mapper, BTN_TOOL_PEN, 1);
8422 processSync(mapper);
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008425 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426
8427 // eraser trumps stylus
8428 processKey(mapper, BTN_TOOL_RUBBER, 1);
8429 processSync(mapper);
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008432 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433
8434 // mouse trumps eraser
8435 processKey(mapper, BTN_TOOL_MOUSE, 1);
8436 processSync(mapper);
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008439 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008440
8441 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8442 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8443 processSync(mapper);
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008446 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447
8448 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8449 processToolType(mapper, MT_TOOL_PEN);
8450 processSync(mapper);
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008453 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008454
8455 // back to default tool type
8456 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8457 processKey(mapper, BTN_TOOL_MOUSE, 0);
8458 processKey(mapper, BTN_TOOL_RUBBER, 0);
8459 processKey(mapper, BTN_TOOL_PEN, 0);
8460 processKey(mapper, BTN_TOOL_FINGER, 0);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008464 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465}
8466
8467TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008468 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008469 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008470 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008471 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008472 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008473
8474 NotifyMotionArgs motionArgs;
8475
8476 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8477 processId(mapper, 1);
8478 processPosition(mapper, 100, 200);
8479 processSync(mapper);
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8483 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8484
8485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8488 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8489
8490 // move a little
8491 processPosition(mapper, 150, 250);
8492 processSync(mapper);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8496 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8497
8498 // down when BTN_TOUCH is pressed, pressure defaults to 1
8499 processKey(mapper, BTN_TOUCH, 1);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8504 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8505
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8507 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8509 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8510
8511 // up when BTN_TOUCH is released, hover restored
8512 processKey(mapper, BTN_TOUCH, 0);
8513 processSync(mapper);
8514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8515 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8517 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8518
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8522 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8523
8524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8527 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8528
8529 // exit hover when pointer goes away
8530 processId(mapper, -1);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8536}
8537
8538TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008540 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008541 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008542 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008543
8544 NotifyMotionArgs motionArgs;
8545
8546 // initially hovering because pressure is 0
8547 processId(mapper, 1);
8548 processPosition(mapper, 100, 200);
8549 processPressure(mapper, 0);
8550 processSync(mapper);
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8554 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8555
8556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8557 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8559 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8560
8561 // move a little
8562 processPosition(mapper, 150, 250);
8563 processSync(mapper);
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8565 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8567 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8568
8569 // down when pressure becomes non-zero
8570 processPressure(mapper, RAW_PRESSURE_MAX);
8571 processSync(mapper);
8572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8575 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8576
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8580 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8581
8582 // up when pressure becomes 0, hover restored
8583 processPressure(mapper, 0);
8584 processSync(mapper);
8585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8586 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8588 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8589
8590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8591 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8593 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8594
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8598 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8599
8600 // exit hover when pointer goes away
8601 processId(mapper, -1);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8606 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8607}
8608
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008609/**
8610 * Set the input device port <--> display port associations, and check that the
8611 * events are routed to the display that matches the display port.
8612 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8613 */
8614TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008615 const std::string usb2 = "USB2";
8616 const uint8_t hdmi1 = 0;
8617 const uint8_t hdmi2 = 1;
8618 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008619 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008620
8621 addConfigurationProperty("touch.deviceType", "touchScreen");
8622 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008623 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008624
8625 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8626 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8627
8628 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8629 // for this input device is specified, and the matching viewport is not present,
8630 // the input device should be disabled (at the mapper level).
8631
8632 // Add viewport for display 2 on hdmi2
8633 prepareSecondaryDisplay(type, hdmi2);
8634 // Send a touch event
8635 processPosition(mapper, 100, 100);
8636 processSync(mapper);
8637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8638
8639 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008640 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008641 // Send a touch event again
8642 processPosition(mapper, 100, 100);
8643 processSync(mapper);
8644
8645 NotifyMotionArgs args;
8646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8647 ASSERT_EQ(DISPLAY_ID, args.displayId);
8648}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008649
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008650TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8651 addConfigurationProperty("touch.deviceType", "touchScreen");
8652 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008653 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008654
8655 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8656
Michael Wrighta9cf4192022-12-01 23:46:39 +00008657 prepareDisplay(ui::ROTATION_0);
8658 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008659
8660 // Send a touch event
8661 processPosition(mapper, 100, 100);
8662 processSync(mapper);
8663
8664 NotifyMotionArgs args;
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8666 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8667}
8668
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008669TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008670 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008671
Michael Wrighta9cf4192022-12-01 23:46:39 +00008672 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008673 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008674 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008675
Josep del Río2d8c79a2023-01-23 19:33:50 +00008676 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008677
8678 NotifyMotionArgs motionArgs;
8679 processPosition(mapper, 100, 100);
8680 processSync(mapper);
8681
8682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8683 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00008684 ASSERT_EQ(ADISPLAY_ID_NONE, motionArgs.displayId);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008685}
8686
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008687/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008688 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8689 */
8690TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8691 addConfigurationProperty("touch.deviceType", "touchScreen");
8692 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008693 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008694
Michael Wrighta9cf4192022-12-01 23:46:39 +00008695 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008696 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8697 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8698 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8699 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008700
8701 NotifyMotionArgs args;
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8703 ASSERT_EQ(26, args.readTime);
8704
Harry Cutts33476232023-01-30 19:57:29 +00008705 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8706 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8707 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008708
8709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8710 ASSERT_EQ(33, args.readTime);
8711}
8712
8713/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008714 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8715 * events should not be delivered to the listener.
8716 */
8717TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8718 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008719 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008720 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008721 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008722 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008723 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008724 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008725
8726 NotifyMotionArgs motionArgs;
8727 processPosition(mapper, 100, 100);
8728 processSync(mapper);
8729
8730 mFakeListener->assertNotifyMotionWasNotCalled();
8731}
8732
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008733/**
8734 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8735 * the touch mapper can process the events and the events can be delivered to the listener.
8736 */
8737TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8738 addConfigurationProperty("touch.deviceType", "touchScreen");
8739 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008740 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008741 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008742 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008743 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008744 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008745
8746 NotifyMotionArgs motionArgs;
8747 processPosition(mapper, 100, 100);
8748 processSync(mapper);
8749
8750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8751 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8752}
8753
Josh Thielene986aed2023-06-01 14:17:30 +00008754/**
8755 * When the viewport is deactivated (isActive transitions from true to false),
8756 * and touch.enableForInactiveViewport is false, touches prior to the transition
8757 * should be cancelled.
8758 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008759TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8760 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008761 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008762 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008763 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008764 std::optional<DisplayViewport> optionalDisplayViewport =
8765 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8766 ASSERT_TRUE(optionalDisplayViewport.has_value());
8767 DisplayViewport displayViewport = *optionalDisplayViewport;
8768
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008769 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008770 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008771 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008772
8773 // Finger down
8774 int32_t x = 100, y = 100;
8775 processPosition(mapper, x, y);
8776 processSync(mapper);
8777
8778 NotifyMotionArgs motionArgs;
8779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8780 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8781
8782 // Deactivate display viewport
8783 displayViewport.isActive = false;
8784 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008785 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008786
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008787 // The ongoing touch should be canceled immediately
8788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8789 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8790
8791 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008792 x += 10, y += 10;
8793 processPosition(mapper, x, y);
8794 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008796
8797 // Reactivate display viewport
8798 displayViewport.isActive = true;
8799 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008800 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008801
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008802 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008803 x += 10, y += 10;
8804 processPosition(mapper, x, y);
8805 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8807 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008808}
8809
Josh Thielene986aed2023-06-01 14:17:30 +00008810/**
8811 * When the viewport is deactivated (isActive transitions from true to false),
8812 * and touch.enableForInactiveViewport is true, touches prior to the transition
8813 * should not be cancelled.
8814 */
8815TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8816 addConfigurationProperty("touch.deviceType", "touchScreen");
8817 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8818 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8819 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8820 std::optional<DisplayViewport> optionalDisplayViewport =
8821 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8822 ASSERT_TRUE(optionalDisplayViewport.has_value());
8823 DisplayViewport displayViewport = *optionalDisplayViewport;
8824
8825 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8826 prepareAxes(POSITION);
8827 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8828
8829 // Finger down
8830 int32_t x = 100, y = 100;
8831 processPosition(mapper, x, y);
8832 processSync(mapper);
8833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8834 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8835
8836 // Deactivate display viewport
8837 displayViewport.isActive = false;
8838 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8839 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8840
8841 // The ongoing touch should not be canceled
8842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8843
8844 // Finger move is not ignored
8845 x += 10, y += 10;
8846 processPosition(mapper, x, y);
8847 processSync(mapper);
8848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8849 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8850
8851 // Reactivate display viewport
8852 displayViewport.isActive = true;
8853 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8854 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8855
8856 // Finger move continues and does not start new gesture
8857 x += 10, y += 10;
8858 processPosition(mapper, x, y);
8859 processSync(mapper);
8860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8861 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8862}
8863
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008864TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008865 prepareAxes(POSITION);
8866 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008867 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008868 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008869
8870 NotifyMotionArgs motionArgs;
8871 // Unrotated video frame
8872 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8873 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008874 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008875 processPosition(mapper, 100, 200);
8876 processSync(mapper);
8877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8878 ASSERT_EQ(frames, motionArgs.videoFrames);
8879
8880 // Subsequent touch events should not have any videoframes
8881 // This is implemented separately in FakeEventHub,
8882 // but that should match the behaviour of TouchVideoDevice.
8883 processPosition(mapper, 200, 200);
8884 processSync(mapper);
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8886 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8887}
8888
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008889TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008890 prepareAxes(POSITION);
8891 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008892 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008893 // Unrotated video frame
8894 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8895 NotifyMotionArgs motionArgs;
8896
8897 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008898 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008899 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008900 clearViewports();
8901 prepareDisplay(orientation);
8902 std::vector<TouchVideoFrame> frames{frame};
8903 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8904 processPosition(mapper, 100, 200);
8905 processSync(mapper);
8906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8907 ASSERT_EQ(frames, motionArgs.videoFrames);
8908 }
8909}
8910
8911TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8912 prepareAxes(POSITION);
8913 addConfigurationProperty("touch.deviceType", "touchScreen");
8914 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8915 // orientation-aware are affected by display rotation.
8916 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008917 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008918 // Unrotated video frame
8919 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8920 NotifyMotionArgs motionArgs;
8921
8922 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008923 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008924 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008925 clearViewports();
8926 prepareDisplay(orientation);
8927 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008928 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008929 processPosition(mapper, 100, 200);
8930 processSync(mapper);
8931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008932 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8933 // compared to the display. This is so that when the window transform (which contains the
8934 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8935 // window's coordinate space.
8936 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008937 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008938
8939 // Release finger.
8940 processSync(mapper);
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008942 }
8943}
8944
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008945TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008946 prepareAxes(POSITION);
8947 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008948 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008949 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8950 // so mix these.
8951 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8952 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8953 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8954 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8955 NotifyMotionArgs motionArgs;
8956
Michael Wrighta9cf4192022-12-01 23:46:39 +00008957 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008958 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008959 processPosition(mapper, 100, 200);
8960 processSync(mapper);
8961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008962 ASSERT_EQ(frames, motionArgs.videoFrames);
8963}
8964
8965TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8966 prepareAxes(POSITION);
8967 addConfigurationProperty("touch.deviceType", "touchScreen");
8968 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8969 // orientation-aware are affected by display rotation.
8970 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008971 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008972 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8973 // so mix these.
8974 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8975 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8976 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8977 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8978 NotifyMotionArgs motionArgs;
8979
Michael Wrighta9cf4192022-12-01 23:46:39 +00008980 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008981 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8982 processPosition(mapper, 100, 200);
8983 processSync(mapper);
8984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8985 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8986 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8987 // compared to the display. This is so that when the window transform (which contains the
8988 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8989 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008990 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008991 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008992 ASSERT_EQ(frames, motionArgs.videoFrames);
8993}
8994
Arthur Hung9da14732019-09-02 16:16:58 +08008995/**
8996 * If we had defined port associations, but the viewport is not ready, the touch device would be
8997 * expected to be disabled, and it should be enabled after the viewport has found.
8998 */
8999TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009000 constexpr uint8_t hdmi2 = 1;
9001 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009002 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009003
9004 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9005
9006 addConfigurationProperty("touch.deviceType", "touchScreen");
9007 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009008 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009009
9010 ASSERT_EQ(mDevice->isEnabled(), false);
9011
9012 // Add display on hdmi2, the device should be enabled and can receive touch event.
9013 prepareSecondaryDisplay(type, hdmi2);
9014 ASSERT_EQ(mDevice->isEnabled(), true);
9015
9016 // Send a touch event.
9017 processPosition(mapper, 100, 100);
9018 processSync(mapper);
9019
9020 NotifyMotionArgs args;
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9022 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9023}
9024
Arthur Hung421eb1c2020-01-16 00:09:42 +08009025TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009026 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009027 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009028 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009029 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009030
9031 NotifyMotionArgs motionArgs;
9032
9033 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9034 // finger down
9035 processId(mapper, 1);
9036 processPosition(mapper, x1, y1);
9037 processSync(mapper);
9038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9039 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009040 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009041
9042 // finger move
9043 processId(mapper, 1);
9044 processPosition(mapper, x2, y2);
9045 processSync(mapper);
9046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009048 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009049
9050 // finger up.
9051 processId(mapper, -1);
9052 processSync(mapper);
9053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9054 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009055 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009056
9057 // new finger down
9058 processId(mapper, 1);
9059 processPosition(mapper, x3, y3);
9060 processSync(mapper);
9061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9062 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009063 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009064}
9065
9066/**
arthurhungcc7f9802020-04-30 17:55:40 +08009067 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9068 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009069 */
arthurhungcc7f9802020-04-30 17:55:40 +08009070TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009071 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009072 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009073 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009074 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009075
9076 NotifyMotionArgs motionArgs;
9077
9078 // default tool type is finger
9079 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009080 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009081 processPosition(mapper, x1, y1);
9082 processSync(mapper);
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009085 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009086
9087 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9088 processToolType(mapper, MT_TOOL_PALM);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9091 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9092
9093 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009094 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009095 processPosition(mapper, x2, y2);
9096 processSync(mapper);
9097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9098
9099 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009100 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009101 processSync(mapper);
9102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9103
9104 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009105 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009106 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009107 processPosition(mapper, x3, y3);
9108 processSync(mapper);
9109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9110 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009111 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009112}
9113
arthurhungbf89a482020-04-17 17:37:55 +08009114/**
arthurhungcc7f9802020-04-30 17:55:40 +08009115 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9116 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009117 */
arthurhungcc7f9802020-04-30 17:55:40 +08009118TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009119 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009120 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009121 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009122 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009123
9124 NotifyMotionArgs motionArgs;
9125
9126 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009127 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9128 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009129 processPosition(mapper, x1, y1);
9130 processSync(mapper);
9131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9132 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009133 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009134
9135 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009136 processSlot(mapper, SECOND_SLOT);
9137 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009138 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009139 processSync(mapper);
9140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009141 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009142 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009143
9144 // If the tool type of the first finger changes to MT_TOOL_PALM,
9145 // we expect to receive ACTION_POINTER_UP with cancel flag.
9146 processSlot(mapper, FIRST_SLOT);
9147 processId(mapper, FIRST_TRACKING_ID);
9148 processToolType(mapper, MT_TOOL_PALM);
9149 processSync(mapper);
9150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009151 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009152 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9153
9154 // The following MOVE events of second finger should be processed.
9155 processSlot(mapper, SECOND_SLOT);
9156 processId(mapper, SECOND_TRACKING_ID);
9157 processPosition(mapper, x2 + 1, y2 + 1);
9158 processSync(mapper);
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009161 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009162
9163 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9164 // it. Second finger receive move.
9165 processSlot(mapper, FIRST_SLOT);
9166 processId(mapper, INVALID_TRACKING_ID);
9167 processSync(mapper);
9168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009170 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009171
9172 // Second finger keeps moving.
9173 processSlot(mapper, SECOND_SLOT);
9174 processId(mapper, SECOND_TRACKING_ID);
9175 processPosition(mapper, x2 + 2, y2 + 2);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009179 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009180
9181 // Second finger up.
9182 processId(mapper, INVALID_TRACKING_ID);
9183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9185 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9186 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9187}
9188
9189/**
9190 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9191 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9192 */
9193TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9194 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009195 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009196 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009197 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009198
9199 NotifyMotionArgs motionArgs;
9200
9201 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9202 // First finger down.
9203 processId(mapper, FIRST_TRACKING_ID);
9204 processPosition(mapper, x1, y1);
9205 processSync(mapper);
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9207 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009208 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009209
9210 // Second finger down.
9211 processSlot(mapper, SECOND_SLOT);
9212 processId(mapper, SECOND_TRACKING_ID);
9213 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009214 processSync(mapper);
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009216 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009217 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009218
arthurhungcc7f9802020-04-30 17:55:40 +08009219 // If the tool type of the first finger changes to MT_TOOL_PALM,
9220 // we expect to receive ACTION_POINTER_UP with cancel flag.
9221 processSlot(mapper, FIRST_SLOT);
9222 processId(mapper, FIRST_TRACKING_ID);
9223 processToolType(mapper, MT_TOOL_PALM);
9224 processSync(mapper);
9225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009226 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009227 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9228
9229 // Second finger keeps moving.
9230 processSlot(mapper, SECOND_SLOT);
9231 processId(mapper, SECOND_TRACKING_ID);
9232 processPosition(mapper, x2 + 1, y2 + 1);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9236
9237 // second finger becomes palm, receive cancel due to only 1 finger is active.
9238 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009239 processToolType(mapper, MT_TOOL_PALM);
9240 processSync(mapper);
9241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9242 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9243
arthurhungcc7f9802020-04-30 17:55:40 +08009244 // third finger down.
9245 processSlot(mapper, THIRD_SLOT);
9246 processId(mapper, THIRD_TRACKING_ID);
9247 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009248 processPosition(mapper, x3, y3);
9249 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9251 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009252 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009253 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009254
9255 // third finger move
9256 processId(mapper, THIRD_TRACKING_ID);
9257 processPosition(mapper, x3 + 1, y3 + 1);
9258 processSync(mapper);
9259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9261
9262 // first finger up, third finger receive move.
9263 processSlot(mapper, FIRST_SLOT);
9264 processId(mapper, INVALID_TRACKING_ID);
9265 processSync(mapper);
9266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009268 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009269
9270 // second finger up, third finger receive move.
9271 processSlot(mapper, SECOND_SLOT);
9272 processId(mapper, INVALID_TRACKING_ID);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009276 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009277
9278 // third finger up.
9279 processSlot(mapper, THIRD_SLOT);
9280 processId(mapper, INVALID_TRACKING_ID);
9281 processSync(mapper);
9282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9283 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9284 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9285}
9286
9287/**
9288 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9289 * and the active finger could still be allowed to receive the events
9290 */
9291TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9292 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009293 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009294 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009295 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009296
9297 NotifyMotionArgs motionArgs;
9298
9299 // default tool type is finger
9300 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9301 processId(mapper, FIRST_TRACKING_ID);
9302 processPosition(mapper, x1, y1);
9303 processSync(mapper);
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9305 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009306 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009307
9308 // Second finger down.
9309 processSlot(mapper, SECOND_SLOT);
9310 processId(mapper, SECOND_TRACKING_ID);
9311 processPosition(mapper, x2, y2);
9312 processSync(mapper);
9313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009314 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009315 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009316
9317 // If the tool type of the second finger changes to MT_TOOL_PALM,
9318 // we expect to receive ACTION_POINTER_UP with cancel flag.
9319 processId(mapper, SECOND_TRACKING_ID);
9320 processToolType(mapper, MT_TOOL_PALM);
9321 processSync(mapper);
9322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009323 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009324 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9325
9326 // The following MOVE event should be processed.
9327 processSlot(mapper, FIRST_SLOT);
9328 processId(mapper, FIRST_TRACKING_ID);
9329 processPosition(mapper, x1 + 1, y1 + 1);
9330 processSync(mapper);
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009333 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009334
9335 // second finger up.
9336 processSlot(mapper, SECOND_SLOT);
9337 processId(mapper, INVALID_TRACKING_ID);
9338 processSync(mapper);
9339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9341
9342 // first finger keep moving
9343 processSlot(mapper, FIRST_SLOT);
9344 processId(mapper, FIRST_TRACKING_ID);
9345 processPosition(mapper, x1 + 2, y1 + 2);
9346 processSync(mapper);
9347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9349
9350 // first finger up.
9351 processId(mapper, INVALID_TRACKING_ID);
9352 processSync(mapper);
9353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9354 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9355 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009356}
9357
Arthur Hung9ad18942021-06-19 02:04:46 +00009358/**
9359 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9360 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9361 * cause slot be valid again.
9362 */
9363TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9364 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009365 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009366 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009367 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009368
9369 NotifyMotionArgs motionArgs;
9370
9371 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9372 // First finger down.
9373 processId(mapper, FIRST_TRACKING_ID);
9374 processPosition(mapper, x1, y1);
9375 processPressure(mapper, RAW_PRESSURE_MAX);
9376 processSync(mapper);
9377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9378 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009379 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009380
9381 // First finger move.
9382 processId(mapper, FIRST_TRACKING_ID);
9383 processPosition(mapper, x1 + 1, y1 + 1);
9384 processPressure(mapper, RAW_PRESSURE_MAX);
9385 processSync(mapper);
9386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009388 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009389
9390 // Second finger down.
9391 processSlot(mapper, SECOND_SLOT);
9392 processId(mapper, SECOND_TRACKING_ID);
9393 processPosition(mapper, x2, y2);
9394 processPressure(mapper, RAW_PRESSURE_MAX);
9395 processSync(mapper);
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009397 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009398 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009399
9400 // second finger up with some unexpected data.
9401 processSlot(mapper, SECOND_SLOT);
9402 processId(mapper, INVALID_TRACKING_ID);
9403 processPosition(mapper, x2, y2);
9404 processSync(mapper);
9405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009406 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009407 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009408
9409 // first finger up with some unexpected data.
9410 processSlot(mapper, FIRST_SLOT);
9411 processId(mapper, INVALID_TRACKING_ID);
9412 processPosition(mapper, x2, y2);
9413 processPressure(mapper, RAW_PRESSURE_MAX);
9414 processSync(mapper);
9415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9416 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009417 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009418}
9419
Arpit Singh4b4a4572023-11-24 18:19:56 +00009420TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009421 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009422 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009423 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009424 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009425
9426 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009427 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009428 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009429 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009430 processPressure(mapper, RAW_PRESSURE_MAX);
9431 processSync(mapper);
9432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9433 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9434
9435 // Second finger down.
9436 processSlot(mapper, SECOND_SLOT);
9437 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009438 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009439 processPressure(mapper, RAW_PRESSURE_MAX);
9440 processSync(mapper);
9441 ASSERT_NO_FATAL_FAILURE(
9442 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9443
Arpit Singh4b4a4572023-11-24 18:19:56 +00009444 // Set MT Slot state to be repopulated for the required slots
9445 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9446 mtSlotValues[0] = FIRST_TRACKING_ID;
9447 mtSlotValues[1] = SECOND_TRACKING_ID;
9448 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9449
9450 mtSlotValues[0] = x1;
9451 mtSlotValues[1] = x2;
9452 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9453
9454 mtSlotValues[0] = y1;
9455 mtSlotValues[1] = y2;
9456 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9457
9458 mtSlotValues[0] = RAW_PRESSURE_MAX;
9459 mtSlotValues[1] = RAW_PRESSURE_MAX;
9460 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9461
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009462 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009463 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009464 resetMapper(mapper, ARBITRARY_TIME);
9465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9466 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009467
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009468 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9469 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009470 processPosition(mapper, 301, 302);
9471 processSync(mapper);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9473 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9475 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009476
9477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9478}
9479
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009480TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009481 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009482 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009483 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009484 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009485
9486 // First finger touches down and releases.
9487 processId(mapper, FIRST_TRACKING_ID);
9488 processPosition(mapper, 100, 200);
9489 processPressure(mapper, RAW_PRESSURE_MAX);
9490 processSync(mapper);
9491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9492 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9493 processId(mapper, INVALID_TRACKING_ID);
9494 processSync(mapper);
9495 ASSERT_NO_FATAL_FAILURE(
9496 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9497
9498 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9499 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009500 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9502
9503 // Send an empty sync frame. Since there are no pointers, no events are generated.
9504 processSync(mapper);
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9506}
9507
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009508TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009509 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009510 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009511 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009512 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009514
9515 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9516 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9517 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9518 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9519 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9520
9521 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009522 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009523 processId(mapper, FIRST_TRACKING_ID);
9524 processToolType(mapper, MT_TOOL_PEN);
9525 processPosition(mapper, 100, 200);
9526 processPressure(mapper, RAW_PRESSURE_MAX);
9527 processSync(mapper);
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9529 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9530 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009531 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009532
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009533 // Now that we know the device supports styluses, ensure that the device is re-configured with
9534 // the stylus source.
9535 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9536 {
9537 const auto& devices = mReader->getInputDevices();
9538 auto deviceInfo =
9539 std::find_if(devices.begin(), devices.end(),
9540 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9541 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9542 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9543 }
9544
9545 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9547
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009548 processId(mapper, INVALID_TRACKING_ID);
9549 processSync(mapper);
9550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9551 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9552 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009553 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009554}
9555
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009556// --- MultiTouchInputMapperTest_ExternalDevice ---
9557
9558class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9559protected:
Chris Yea52ade12020-08-27 16:49:20 -07009560 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009561};
9562
9563/**
9564 * Expect fallback to internal viewport if device is external and external viewport is not present.
9565 */
9566TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9567 prepareAxes(POSITION);
9568 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009569 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009570 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009571
9572 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9573
9574 NotifyMotionArgs motionArgs;
9575
9576 // Expect the event to be sent to the internal viewport,
9577 // because an external viewport is not present.
9578 processPosition(mapper, 100, 100);
9579 processSync(mapper);
9580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9581 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9582
9583 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009584 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009585 processPosition(mapper, 100, 100);
9586 processSync(mapper);
9587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9588 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9589}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009590
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00009591// TODO(b/281840344): Remove the test when the old touchpad stack is removed. It is currently
9592// unclear what the behavior of the touchpad logic in TouchInputMapper should do after the
9593// PointerChoreographer refactor.
9594TEST_F(MultiTouchInputMapperTest, DISABLED_Process_TouchpadPointer) {
Harry Cutts8722be92024-04-05 14:46:05 +00009595 // prepare device
Michael Wrighta9cf4192022-12-01 23:46:39 +00009596 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009597 prepareAxes(POSITION | ID | SLOT);
9598 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9599 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009600 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009601 // run uncaptured pointer tests - pushes out generic events
9602 // FINGER 0 DOWN
9603 processId(mapper, 3);
9604 processPosition(mapper, 100, 100);
9605 processKey(mapper, BTN_TOUCH, 1);
9606 processSync(mapper);
9607
9608 // start at (100,100), cursor should be at (0,0) * scale
9609 NotifyMotionArgs args;
9610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9611 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9612 ASSERT_NO_FATAL_FAILURE(
9613 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9614
9615 // FINGER 0 MOVE
9616 processPosition(mapper, 200, 200);
9617 processSync(mapper);
9618
9619 // compute scaling to help with touch position checking
9620 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9621 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9622 float scale =
9623 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9624
9625 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9627 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9629 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009630
9631 // BUTTON DOWN
9632 processKey(mapper, BTN_LEFT, 1);
9633 processSync(mapper);
9634
9635 // touchinputmapper design sends a move before button press
9636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9637 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9639 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9640
9641 // BUTTON UP
9642 processKey(mapper, BTN_LEFT, 0);
9643 processSync(mapper);
9644
9645 // touchinputmapper design sends a move after button release
9646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9647 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9649 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009650}
9651
Harry Cutts8722be92024-04-05 14:46:05 +00009652TEST_F(MultiTouchInputMapperTest, Touchpad_GetSources) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00009653 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009654 prepareAxes(POSITION | ID | SLOT);
9655 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Hiroki Sato25040232024-02-22 17:21:22 +09009656 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Arpit Singha8c236b2023-04-25 13:56:05 +00009657 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009658
Josep del Río2d8c79a2023-01-23 19:33:50 +00009659 // uncaptured touchpad should be a pointer device
9660 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009661}
9662
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009663// --- BluetoothMultiTouchInputMapperTest ---
9664
9665class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9666protected:
9667 void SetUp() override {
9668 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9669 }
9670};
9671
9672TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9673 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009674 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009675 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009676 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009677
9678 nsecs_t kernelEventTime = ARBITRARY_TIME;
9679 nsecs_t expectedEventTime = ARBITRARY_TIME;
9680 // Touch down.
9681 processId(mapper, FIRST_TRACKING_ID);
9682 processPosition(mapper, 100, 200);
9683 processPressure(mapper, RAW_PRESSURE_MAX);
9684 processSync(mapper, ARBITRARY_TIME);
9685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9686 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9687
9688 // Process several events that come in quick succession, according to their timestamps.
9689 for (int i = 0; i < 3; i++) {
9690 constexpr static nsecs_t delta = ms2ns(1);
9691 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9692 kernelEventTime += delta;
9693 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9694
9695 processPosition(mapper, 101 + i, 201 + i);
9696 processSync(mapper, kernelEventTime);
9697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9698 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9699 WithEventTime(expectedEventTime))));
9700 }
9701
9702 // Release the touch.
9703 processId(mapper, INVALID_TRACKING_ID);
9704 processPressure(mapper, RAW_PRESSURE_MIN);
9705 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9707 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9708 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9709}
9710
9711// --- MultiTouchPointerModeTest ---
9712
HQ Liue6983c72022-04-19 22:14:56 +00009713class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9714protected:
9715 float mPointerMovementScale;
9716 float mPointerXZoomScale;
9717 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9718 addConfigurationProperty("touch.deviceType", "pointer");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009719 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009720
9721 prepareAxes(POSITION);
9722 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9723 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9724 // needs to be disabled, and the pointer gesture needs to be enabled.
Hiroki Sato25040232024-02-22 17:21:22 +09009725 mFakePolicy->setPointerCapture(/*window=*/nullptr);
HQ Liue6983c72022-04-19 22:14:56 +00009726 mFakePolicy->setPointerGestureEnabled(true);
HQ Liue6983c72022-04-19 22:14:56 +00009727
9728 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9729 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9730 mPointerMovementScale =
9731 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9732 mPointerXZoomScale =
9733 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9734 }
9735
9736 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9737 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9738 /*flat*/ 0,
9739 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9740 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9741 /*flat*/ 0,
9742 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9743 }
9744};
9745
9746/**
9747 * Two fingers down on a pointer mode touch pad. The width
9748 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9749 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9750 * be greater than the both value to be freeform gesture, so that after two
9751 * fingers start to move downwards, the gesture should be swipe.
9752 */
9753TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9754 // The min freeform gesture width is 25units/mm x 30mm = 750
9755 // which is greater than fraction of the diagnal length of the touchpad (349).
9756 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +00009757 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009758 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009759 NotifyMotionArgs motionArgs;
9760
9761 // Two fingers down at once.
9762 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9763 // Pointer's initial position is used the [0,0] coordinate.
9764 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9765
9766 processId(mapper, FIRST_TRACKING_ID);
9767 processPosition(mapper, x1, y1);
9768 processMTSync(mapper);
9769 processId(mapper, SECOND_TRACKING_ID);
9770 processPosition(mapper, x2, y2);
9771 processMTSync(mapper);
9772 processSync(mapper);
9773
9774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009775 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009776 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009777 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009778 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009779 ASSERT_NO_FATAL_FAILURE(
9780 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9781
9782 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9783 // that there should be 1 pointer.
9784 int32_t movingDistance = 200;
9785 y1 += movingDistance;
9786 y2 += movingDistance;
9787
9788 processId(mapper, FIRST_TRACKING_ID);
9789 processPosition(mapper, x1, y1);
9790 processMTSync(mapper);
9791 processId(mapper, SECOND_TRACKING_ID);
9792 processPosition(mapper, x2, y2);
9793 processMTSync(mapper);
9794 processSync(mapper);
9795
9796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009797 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009799 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009800 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9802 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9803 0, 0, 0, 0));
9804}
9805
9806/**
9807 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9808 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9809 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9810 * value to be freeform gesture, so that after two fingers start to move downwards,
9811 * the gesture should be swipe.
9812 */
9813TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9814 // The min freeform gesture width is 5units/mm x 30mm = 150
9815 // which is greater than fraction of the diagnal length of the touchpad (349).
9816 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +00009817 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +00009818 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009819 NotifyMotionArgs motionArgs;
9820
9821 // Two fingers down at once.
9822 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9823 // Pointer's initial position is used the [0,0] coordinate.
9824 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9825
9826 processId(mapper, FIRST_TRACKING_ID);
9827 processPosition(mapper, x1, y1);
9828 processMTSync(mapper);
9829 processId(mapper, SECOND_TRACKING_ID);
9830 processPosition(mapper, x2, y2);
9831 processMTSync(mapper);
9832 processSync(mapper);
9833
9834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009835 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009836 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009837 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009838 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009839 ASSERT_NO_FATAL_FAILURE(
9840 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9841
9842 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9843 // and there should be 1 pointer.
9844 int32_t movingDistance = 200;
9845 y1 += movingDistance;
9846 y2 += movingDistance;
9847
9848 processId(mapper, FIRST_TRACKING_ID);
9849 processPosition(mapper, x1, y1);
9850 processMTSync(mapper);
9851 processId(mapper, SECOND_TRACKING_ID);
9852 processPosition(mapper, x2, y2);
9853 processMTSync(mapper);
9854 processSync(mapper);
9855
9856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009857 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009859 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009860 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009861 // New coordinate is the scaled relative coordinate from the initial coordinate.
9862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9863 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9864 0, 0, 0, 0));
9865}
9866
9867/**
9868 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9869 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9870 * freeform gestures after two fingers start to move downwards.
9871 */
9872TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +00009873 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009874 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009875
9876 NotifyMotionArgs motionArgs;
9877
9878 // Two fingers down at once. Wider than the max swipe width.
9879 // The gesture is expected to be PRESS, then transformed to FREEFORM
9880 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9881
9882 processId(mapper, FIRST_TRACKING_ID);
9883 processPosition(mapper, x1, y1);
9884 processMTSync(mapper);
9885 processId(mapper, SECOND_TRACKING_ID);
9886 processPosition(mapper, x2, y2);
9887 processMTSync(mapper);
9888 processSync(mapper);
9889
9890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009891 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009892 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009893 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009894 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009895 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9896 ASSERT_NO_FATAL_FAILURE(
9897 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9898
9899 int32_t movingDistance = 200;
9900
9901 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9902 // then two down events for two pointers.
9903 y1 += movingDistance;
9904 y2 += movingDistance;
9905
9906 processId(mapper, FIRST_TRACKING_ID);
9907 processPosition(mapper, x1, y1);
9908 processMTSync(mapper);
9909 processId(mapper, SECOND_TRACKING_ID);
9910 processPosition(mapper, x2, y2);
9911 processMTSync(mapper);
9912 processSync(mapper);
9913
9914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9915 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009916 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009917 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009919 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009920 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009921 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009923 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009924 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009925 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009926 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009927 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009928 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009929 // Two pointers' scaled relative coordinates from their initial centroid.
9930 // Initial y coordinates are 0 as y1 and y2 have the same value.
9931 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9932 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9933 // When pointers move, the new coordinates equal to the initial coordinates plus
9934 // scaled moving distance.
9935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9936 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9937 0, 0, 0, 0));
9938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9939 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9940 0, 0, 0, 0));
9941
9942 // Move two fingers down again, expect one MOVE motion event.
9943 y1 += movingDistance;
9944 y2 += movingDistance;
9945
9946 processId(mapper, FIRST_TRACKING_ID);
9947 processPosition(mapper, x1, y1);
9948 processMTSync(mapper);
9949 processId(mapper, SECOND_TRACKING_ID);
9950 processPosition(mapper, x2, y2);
9951 processMTSync(mapper);
9952 processSync(mapper);
9953
9954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009955 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009957 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009958 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9960 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9961 0, 0, 0, 0, 0));
9962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9963 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9964 0, 0, 0, 0, 0));
9965}
9966
Harry Cutts39b7ca22022-10-05 15:55:48 +00009967TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +00009968 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009969 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +00009970 NotifyMotionArgs motionArgs;
9971
9972 // Place two fingers down.
9973 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9974
9975 processId(mapper, FIRST_TRACKING_ID);
9976 processPosition(mapper, x1, y1);
9977 processMTSync(mapper);
9978 processId(mapper, SECOND_TRACKING_ID);
9979 processPosition(mapper, x2, y2);
9980 processMTSync(mapper);
9981 processSync(mapper);
9982
9983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009984 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +00009985 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9986 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
9987 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
9988 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
9989
9990 // Move the two fingers down and to the left.
9991 int32_t movingDistance = 200;
9992 x1 -= movingDistance;
9993 y1 += movingDistance;
9994 x2 -= movingDistance;
9995 y2 += movingDistance;
9996
9997 processId(mapper, FIRST_TRACKING_ID);
9998 processPosition(mapper, x1, y1);
9999 processMTSync(mapper);
10000 processId(mapper, SECOND_TRACKING_ID);
10001 processPosition(mapper, x2, y2);
10002 processMTSync(mapper);
10003 processSync(mapper);
10004
10005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010006 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10008 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10009 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10010 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10011}
10012
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010013TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010014 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010015 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010016 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10018
10019 // Start a stylus gesture.
10020 processKey(mapper, BTN_TOOL_PEN, 1);
10021 processId(mapper, FIRST_TRACKING_ID);
10022 processPosition(mapper, 100, 200);
10023 processSync(mapper);
10024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10025 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10026 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010027 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010028 // TODO(b/257078296): Pointer mode generates extra event.
10029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10030 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10031 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010032 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10034
10035 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10036 // gesture should be disabled.
10037 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10038 viewport->isActive = false;
10039 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010040 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10042 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10043 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010044 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010045 // TODO(b/257078296): Pointer mode generates extra event.
10046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10047 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10048 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010049 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10051}
10052
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010053// --- JoystickInputMapperTest ---
10054
10055class JoystickInputMapperTest : public InputMapperTest {
10056protected:
10057 static const int32_t RAW_X_MIN;
10058 static const int32_t RAW_X_MAX;
10059 static const int32_t RAW_Y_MIN;
10060 static const int32_t RAW_Y_MAX;
10061
10062 void SetUp() override {
10063 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10064 }
10065 void prepareAxes() {
10066 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10067 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10068 }
10069
10070 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10072 }
10073
10074 void processSync(JoystickInputMapper& mapper) {
10075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10076 }
10077
Michael Wrighta9cf4192022-12-01 23:46:39 +000010078 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010079 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10080 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10081 NO_PORT, ViewportType::VIRTUAL);
10082 }
10083};
10084
10085const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10086const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10087const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10088const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10089
10090TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10091 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010092 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010093
10094 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10095
Michael Wrighta9cf4192022-12-01 23:46:39 +000010096 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010097
10098 // Send an axis event
10099 processAxis(mapper, ABS_X, 100);
10100 processSync(mapper);
10101
10102 NotifyMotionArgs args;
10103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10104 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10105
10106 // Send another axis event
10107 processAxis(mapper, ABS_Y, 100);
10108 processSync(mapper);
10109
10110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10111 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10112}
10113
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010114// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010115
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010116class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010117protected:
10118 static const char* DEVICE_NAME;
10119 static const char* DEVICE_LOCATION;
10120 static const int32_t DEVICE_ID;
10121 static const int32_t DEVICE_GENERATION;
10122 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010123 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010124 static const int32_t EVENTHUB_ID;
10125
10126 std::shared_ptr<FakeEventHub> mFakeEventHub;
10127 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010128 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010129 std::unique_ptr<InstrumentedInputReader> mReader;
10130 std::shared_ptr<InputDevice> mDevice;
10131
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010132 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010133 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010134 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010135 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010136 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010137 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010138 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10139 }
10140
10141 void SetUp() override { SetUp(DEVICE_CLASSES); }
10142
10143 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010144 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010145 mFakePolicy.clear();
10146 }
10147
Chris Yee2b1e5c2021-03-10 22:45:12 -080010148 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10149 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010150 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010151 InputDeviceIdentifier identifier;
10152 identifier.name = name;
10153 identifier.location = location;
10154 std::shared_ptr<InputDevice> device =
10155 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10156 identifier);
10157 mReader->pushNextDevice(device);
10158 mFakeEventHub->addDevice(eventHubId, name, classes);
10159 mReader->loopOnce();
10160 return device;
10161 }
10162
10163 template <class T, typename... Args>
10164 T& addControllerAndConfigure(Args... args) {
10165 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10166
10167 return controller;
10168 }
10169};
10170
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010171const char* PeripheralControllerTest::DEVICE_NAME = "device";
10172const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10173const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10174const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10175const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010176const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10177 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010178const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010179
10180// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010181class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010182protected:
10183 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010184 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010185 }
10186};
10187
10188TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010189 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010190
Harry Cuttsa5b71292022-11-28 12:56:17 +000010191 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10192 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10193 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010194}
10195
10196TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010197 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010198
Harry Cuttsa5b71292022-11-28 12:56:17 +000010199 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10200 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10201 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010202}
10203
10204// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010205class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010206protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010207 void SetUp() override {
10208 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10209 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010210};
10211
Chris Ye85758332021-05-16 23:05:17 -070010212TEST_F(LightControllerTest, MonoLight) {
10213 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010214 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010215 .maxBrightness = 255,
10216 .flags = InputLightClass::BRIGHTNESS,
10217 .path = ""};
10218 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010219
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010220 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010221 InputDeviceInfo info;
10222 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010223 std::vector<InputDeviceLightInfo> lights = info.getLights();
10224 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010225 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10226 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10227
10228 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10229 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10230}
10231
DingYong99f2c3c2023-12-20 15:46:06 +080010232TEST_F(LightControllerTest, MonoKeyboardMuteLight) {
10233 RawLightInfo infoMono = {.id = 1,
10234 .name = "mono_keyboard_mute",
10235 .maxBrightness = 255,
10236 .flags = InputLightClass::BRIGHTNESS |
10237 InputLightClass::KEYBOARD_MIC_MUTE,
10238 .path = ""};
10239 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10240
10241 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10242 std::list<NotifyArgs> unused =
10243 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10244 /*changes=*/{});
10245
10246 InputDeviceInfo info;
10247 controller.populateDeviceInfo(&info);
10248 std::vector<InputDeviceLightInfo> lights = info.getLights();
10249 ASSERT_EQ(1U, lights.size());
10250 ASSERT_EQ(InputDeviceLightType::KEYBOARD_MIC_MUTE, lights[0].type);
10251 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10252}
10253
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010254TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10255 RawLightInfo infoMono = {.id = 1,
10256 .name = "mono_keyboard_backlight",
10257 .maxBrightness = 255,
10258 .flags = InputLightClass::BRIGHTNESS |
10259 InputLightClass::KEYBOARD_BACKLIGHT,
10260 .path = ""};
10261 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10262
10263 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10264 InputDeviceInfo info;
10265 controller.populateDeviceInfo(&info);
10266 std::vector<InputDeviceLightInfo> lights = info.getLights();
10267 ASSERT_EQ(1U, lights.size());
10268 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10269 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010270
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010271 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10272 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010273}
10274
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010275TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10276 RawLightInfo infoMono = {.id = 1,
10277 .name = "mono_light",
10278 .maxBrightness = 255,
10279 .flags = InputLightClass::BRIGHTNESS,
10280 .path = ""};
10281 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10282 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10283 "0,100,200");
10284
10285 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10286 std::list<NotifyArgs> unused =
10287 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10288 /*changes=*/{});
10289
10290 InputDeviceInfo info;
10291 controller.populateDeviceInfo(&info);
10292 std::vector<InputDeviceLightInfo> lights = info.getLights();
10293 ASSERT_EQ(1U, lights.size());
10294 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10295}
10296
10297TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10298 RawLightInfo infoMono = {.id = 1,
10299 .name = "mono_keyboard_backlight",
10300 .maxBrightness = 255,
10301 .flags = InputLightClass::BRIGHTNESS |
10302 InputLightClass::KEYBOARD_BACKLIGHT,
10303 .path = ""};
10304 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10305
10306 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10307 std::list<NotifyArgs> unused =
10308 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10309 /*changes=*/{});
10310
10311 InputDeviceInfo info;
10312 controller.populateDeviceInfo(&info);
10313 std::vector<InputDeviceLightInfo> lights = info.getLights();
10314 ASSERT_EQ(1U, lights.size());
10315 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10316}
10317
10318TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10319 RawLightInfo infoMono = {.id = 1,
10320 .name = "mono_keyboard_backlight",
10321 .maxBrightness = 255,
10322 .flags = InputLightClass::BRIGHTNESS |
10323 InputLightClass::KEYBOARD_BACKLIGHT,
10324 .path = ""};
10325 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10326 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10327 "0,100,200");
10328
10329 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10330 std::list<NotifyArgs> unused =
10331 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10332 /*changes=*/{});
10333
10334 InputDeviceInfo info;
10335 controller.populateDeviceInfo(&info);
10336 std::vector<InputDeviceLightInfo> lights = info.getLights();
10337 ASSERT_EQ(1U, lights.size());
10338 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10339 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10340 ASSERT_EQ(BrightnessLevel(0), *it);
10341 std::advance(it, 1);
10342 ASSERT_EQ(BrightnessLevel(100), *it);
10343 std::advance(it, 1);
10344 ASSERT_EQ(BrightnessLevel(200), *it);
10345}
10346
10347TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10348 RawLightInfo infoMono = {.id = 1,
10349 .name = "mono_keyboard_backlight",
10350 .maxBrightness = 255,
10351 .flags = InputLightClass::BRIGHTNESS |
10352 InputLightClass::KEYBOARD_BACKLIGHT,
10353 .path = ""};
10354 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10355 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10356 "0,100,200,300,400,500");
10357
10358 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10359 std::list<NotifyArgs> unused =
10360 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10361 /*changes=*/{});
10362
10363 InputDeviceInfo info;
10364 controller.populateDeviceInfo(&info);
10365 std::vector<InputDeviceLightInfo> lights = info.getLights();
10366 ASSERT_EQ(1U, lights.size());
10367 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10368}
10369
Chris Yee2b1e5c2021-03-10 22:45:12 -080010370TEST_F(LightControllerTest, RGBLight) {
10371 RawLightInfo infoRed = {.id = 1,
10372 .name = "red",
10373 .maxBrightness = 255,
10374 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10375 .path = ""};
10376 RawLightInfo infoGreen = {.id = 2,
10377 .name = "green",
10378 .maxBrightness = 255,
10379 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10380 .path = ""};
10381 RawLightInfo infoBlue = {.id = 3,
10382 .name = "blue",
10383 .maxBrightness = 255,
10384 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10385 .path = ""};
10386 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10387 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10388 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10389
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010390 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010391 InputDeviceInfo info;
10392 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010393 std::vector<InputDeviceLightInfo> lights = info.getLights();
10394 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010395 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10396 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10397 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10398
10399 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10400 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10401}
10402
10403TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10404 RawLightInfo infoRed = {.id = 1,
10405 .name = "red_keyboard_backlight",
10406 .maxBrightness = 255,
10407 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10408 InputLightClass::KEYBOARD_BACKLIGHT,
10409 .path = ""};
10410 RawLightInfo infoGreen = {.id = 2,
10411 .name = "green_keyboard_backlight",
10412 .maxBrightness = 255,
10413 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10414 InputLightClass::KEYBOARD_BACKLIGHT,
10415 .path = ""};
10416 RawLightInfo infoBlue = {.id = 3,
10417 .name = "blue_keyboard_backlight",
10418 .maxBrightness = 255,
10419 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10420 InputLightClass::KEYBOARD_BACKLIGHT,
10421 .path = ""};
10422 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10423 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10424 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10425
10426 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10427 InputDeviceInfo info;
10428 controller.populateDeviceInfo(&info);
10429 std::vector<InputDeviceLightInfo> lights = info.getLights();
10430 ASSERT_EQ(1U, lights.size());
10431 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10432 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10433 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10434
10435 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10436 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10437}
10438
10439TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10440 RawLightInfo infoRed = {.id = 1,
10441 .name = "red",
10442 .maxBrightness = 255,
10443 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10444 .path = ""};
10445 RawLightInfo infoGreen = {.id = 2,
10446 .name = "green",
10447 .maxBrightness = 255,
10448 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10449 .path = ""};
10450 RawLightInfo infoBlue = {.id = 3,
10451 .name = "blue",
10452 .maxBrightness = 255,
10453 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10454 .path = ""};
10455 RawLightInfo infoGlobal = {.id = 3,
10456 .name = "global_keyboard_backlight",
10457 .maxBrightness = 255,
10458 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10459 InputLightClass::KEYBOARD_BACKLIGHT,
10460 .path = ""};
10461 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10462 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10463 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10464 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10465
10466 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10467 InputDeviceInfo info;
10468 controller.populateDeviceInfo(&info);
10469 std::vector<InputDeviceLightInfo> lights = info.getLights();
10470 ASSERT_EQ(1U, lights.size());
10471 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10472 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10473 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010474
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010475 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10476 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010477}
10478
10479TEST_F(LightControllerTest, MultiColorRGBLight) {
10480 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010481 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010482 .maxBrightness = 255,
10483 .flags = InputLightClass::BRIGHTNESS |
10484 InputLightClass::MULTI_INTENSITY |
10485 InputLightClass::MULTI_INDEX,
10486 .path = ""};
10487
10488 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10489
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010490 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010491 InputDeviceInfo info;
10492 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010493 std::vector<InputDeviceLightInfo> lights = info.getLights();
10494 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010495 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10496 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10497 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10498
10499 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10500 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10501}
10502
10503TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10504 RawLightInfo infoColor = {.id = 1,
10505 .name = "multi_color_keyboard_backlight",
10506 .maxBrightness = 255,
10507 .flags = InputLightClass::BRIGHTNESS |
10508 InputLightClass::MULTI_INTENSITY |
10509 InputLightClass::MULTI_INDEX |
10510 InputLightClass::KEYBOARD_BACKLIGHT,
10511 .path = ""};
10512
10513 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10514
10515 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10516 InputDeviceInfo info;
10517 controller.populateDeviceInfo(&info);
10518 std::vector<InputDeviceLightInfo> lights = info.getLights();
10519 ASSERT_EQ(1U, lights.size());
10520 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10521 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10522 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010523
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010524 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10525 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010526}
10527
10528TEST_F(LightControllerTest, PlayerIdLight) {
10529 RawLightInfo info1 = {.id = 1,
10530 .name = "player1",
10531 .maxBrightness = 255,
10532 .flags = InputLightClass::BRIGHTNESS,
10533 .path = ""};
10534 RawLightInfo info2 = {.id = 2,
10535 .name = "player2",
10536 .maxBrightness = 255,
10537 .flags = InputLightClass::BRIGHTNESS,
10538 .path = ""};
10539 RawLightInfo info3 = {.id = 3,
10540 .name = "player3",
10541 .maxBrightness = 255,
10542 .flags = InputLightClass::BRIGHTNESS,
10543 .path = ""};
10544 RawLightInfo info4 = {.id = 4,
10545 .name = "player4",
10546 .maxBrightness = 255,
10547 .flags = InputLightClass::BRIGHTNESS,
10548 .path = ""};
10549 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10550 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10551 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10552 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10553
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010554 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010555 InputDeviceInfo info;
10556 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010557 std::vector<InputDeviceLightInfo> lights = info.getLights();
10558 ASSERT_EQ(1U, lights.size());
10559 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010560 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10561 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010562
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010563 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10564 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10565 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010566}
10567
Michael Wrightd02c5b62014-02-10 15:10:22 -080010568} // namespace android