blob: 9bbc44aa363842dd9c76b23a31ad8d5719baea55 [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>
Prabir Pradhan018faea2024-05-08 21:52:54 +000030#include <NotifyArgsBuilders.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070031#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070032#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070033#include <SingleTouchInputMapper.h>
34#include <SwitchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000035#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070036#include <TestInputListener.h>
37#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080038#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000039#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070040#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090041#include <com_android_input_flags.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000042#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043#include <gtest/gtest.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 Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +000059using testing::VariantWith;
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.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070064static constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string DISPLAY_UNIQUE_ID = "local:1";
Linnan Li13bf76a2024-05-05 19:18:02 +080066static constexpr ui::LogicalDisplayId SECONDARY_DISPLAY_ID =
67 ui::LogicalDisplayId{DISPLAY_ID.val() + 1};
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t DISPLAY_WIDTH = 480;
69static constexpr int32_t DISPLAY_HEIGHT = 800;
Linnan Li13bf76a2024-05-05 19:18:02 +080070static constexpr ui::LogicalDisplayId VIRTUAL_DISPLAY_ID = ui::LogicalDisplayId{1};
arthurhungcc7f9802020-04-30 17:55:40 +080071static 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
Harry Cuttsa32a1192024-06-04 15:10:31 +0000313 std::list<NotifyArgs> process(const RawEvent& rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700314 std::scoped_lock<std::mutex> lock(mLock);
Harry Cuttsa32a1192024-06-04 15:10:31 +0000315 mLastEvent = rawEvent;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 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
Linnan Li13bf76a2024-05-05 19:18:02 +0800362 virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplay() {
Arthur Hungc23540e2018-11-29 20:42:11 +0800363 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";
Linnan Li13bf76a2024-05-05 19:18:02 +0800421 constexpr ui::LogicalDisplayId virtualDisplayId1 = ui::LogicalDisplayId{2};
422 constexpr ui::LogicalDisplayId virtualDisplayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700423
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";
Linnan Li13bf76a2024-05-05 19:18:02 +0800479 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{2};
480 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700481
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";
Linnan Li13bf76a2024-05-05 19:18:02 +0800524 constexpr ui::LogicalDisplayId nonDefaultDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700525 ASSERT_NE(nonDefaultDisplayId, ui::LogicalDisplayId::DEFAULT)
526 << "Test display ID should not be ui::LogicalDisplayId::DEFAULT ";
Michael Wrightdde67b82020-10-27 16:09:22 +0000527
528 // Add the default display first and ensure it gets returned.
529 mFakePolicy->clearViewports();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700530 mFakePolicy->addDisplayViewport(ui::LogicalDisplayId::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);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700540 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000541 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);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700548 mFakePolicy->addDisplayViewport(ui::LogicalDisplayId::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);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700554 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000555 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";
Linnan Li13bf76a2024-05-05 19:18:02 +0800565 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{1};
566 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{2};
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700567 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 }
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000627 mReader->loopOnce();
628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700629 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700631 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 }
633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800634 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700635 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000636 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700637 }
638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800639 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700640 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000641 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700642 }
643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700645 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700646 ftl::Flags<InputDeviceClass> classes,
647 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800648 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000650 FakeInputMapper& mapper =
651 device->addMapper<FakeInputMapper>(eventHubId,
652 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800653 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800654 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 return mapper;
656 }
657};
658
Chris Ye98d3f532020-10-01 21:48:59 -0700659TEST_F(InputReaderTest, PolicyGetInputDevices) {
660 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700661 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700662 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663
664 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700665 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800667 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100668 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000671 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672}
673
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000674TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
675 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
676 mFakeEventHub->setSysfsRootPath(1, "xyz");
677
678 // Should also have received a notification describing the new input device.
679 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
680 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
681 ASSERT_EQ(0U, inputDevice.getLights().size());
682
683 RawLightInfo infoMonolight = {.id = 123,
684 .name = "mono_keyboard_backlight",
685 .maxBrightness = 255,
686 .flags = InputLightClass::BRIGHTNESS,
687 .path = ""};
688 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
689 mReader->sysfsNodeChanged("xyz");
690 mReader->loopOnce();
691
692 // Should also have received a notification describing the new recreated input device.
693 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
694 inputDevice = mFakePolicy->getInputDevices()[0];
695 ASSERT_EQ(1U, inputDevice.getLights().size());
696}
697
Chris Yee7310032020-09-22 15:36:28 -0700698TEST_F(InputReaderTest, GetMergedInputDevices) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 ASSERT_NO_FATAL_FAILURE(
713 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
714 ASSERT_NO_FATAL_FAILURE(
715 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
716
717 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000718 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700719}
720
Chris Yee14523a2020-12-19 13:46:00 -0800721TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
723 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
724 // Add two subdevices to device
725 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
726 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000727 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
728 AINPUT_SOURCE_KEYBOARD);
729 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
730 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800731
732 // Push same device instance for next device to be added, so they'll have same identifier.
733 mReader->pushNextDevice(device);
734 mReader->pushNextDevice(device);
735 // Sensor device is initially disabled
736 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
737 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
738 nullptr));
739 // Device is disabled because the only sub device is a sensor device and disabled initially.
740 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
741 ASSERT_FALSE(device->isEnabled());
742 ASSERT_NO_FATAL_FAILURE(
743 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
744 // The merged device is enabled if any sub device is enabled
745 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
746 ASSERT_TRUE(device->isEnabled());
747}
748
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700749TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800750 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700751 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800752 constexpr int32_t eventHubId = 1;
753 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700754 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000755 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
756 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800757 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800758 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700759
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760 NotifyDeviceResetArgs resetArgs;
761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700762 ASSERT_EQ(deviceId, resetArgs.deviceId);
763
764 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800765 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000766 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700767
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700769 ASSERT_EQ(deviceId, resetArgs.deviceId);
770 ASSERT_EQ(device->isEnabled(), false);
771
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800772 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000773 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700775 ASSERT_EQ(device->isEnabled(), false);
776
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800777 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000778 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700780 ASSERT_EQ(deviceId, resetArgs.deviceId);
781 ASSERT_EQ(device->isEnabled(), true);
782}
783
Michael Wrightd02c5b62014-02-10 15:10:22 -0800784TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800785 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700786 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800787 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800788 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800789 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800790 AINPUT_SOURCE_KEYBOARD, nullptr);
791 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800792
793 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
794 AINPUT_SOURCE_ANY, AKEYCODE_A))
795 << "Should return unknown when the device id is >= 0 but unknown.";
796
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800797 ASSERT_EQ(AKEY_STATE_UNKNOWN,
798 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
799 << "Should return unknown when the device id is valid but the sources are not "
800 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800801
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800802 ASSERT_EQ(AKEY_STATE_DOWN,
803 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
804 AKEYCODE_A))
805 << "Should return value provided by mapper when device id is valid and the device "
806 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800807
808 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
809 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
810 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
811
812 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
813 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
814 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
815}
816
Philip Junker4af3b3d2021-12-14 10:36:55 +0100817TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
818 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
819 constexpr int32_t eventHubId = 1;
820 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
821 InputDeviceClass::KEYBOARD,
822 AINPUT_SOURCE_KEYBOARD, nullptr);
823 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
824
825 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
826 << "Should return unknown when the device with the specified id is not found.";
827
828 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
829 << "Should return correct mapping when device id is valid and mapping exists.";
830
831 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
832 << "Should return the location key code when device id is valid and there's no "
833 "mapping.";
834}
835
836TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
837 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
838 constexpr int32_t eventHubId = 1;
839 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
840 InputDeviceClass::JOYSTICK,
841 AINPUT_SOURCE_GAMEPAD, nullptr);
842 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
843
844 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
845 << "Should return unknown when the device id is valid but there is no keyboard mapper";
846}
847
Michael Wrightd02c5b62014-02-10 15:10:22 -0800848TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800849 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700850 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800851 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800852 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800853 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800854 AINPUT_SOURCE_KEYBOARD, nullptr);
855 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800856
857 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
858 AINPUT_SOURCE_ANY, KEY_A))
859 << "Should return unknown when the device id is >= 0 but unknown.";
860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800861 ASSERT_EQ(AKEY_STATE_UNKNOWN,
862 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
863 << "Should return unknown when the device id is valid but the sources are not "
864 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800865
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 ASSERT_EQ(AKEY_STATE_DOWN,
867 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
868 KEY_A))
869 << "Should return value provided by mapper when device id is valid and the device "
870 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800871
872 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
873 AINPUT_SOURCE_TRACKBALL, KEY_A))
874 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
875
876 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
877 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
878 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
879}
880
881TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800882 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700883 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800884 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800885 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800886 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800887 AINPUT_SOURCE_KEYBOARD, nullptr);
888 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889
890 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
891 AINPUT_SOURCE_ANY, SW_LID))
892 << "Should return unknown when the device id is >= 0 but unknown.";
893
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800894 ASSERT_EQ(AKEY_STATE_UNKNOWN,
895 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
896 << "Should return unknown when the device id is valid but the sources are not "
897 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800899 ASSERT_EQ(AKEY_STATE_DOWN,
900 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
901 SW_LID))
902 << "Should return value provided by mapper when device id is valid and the device "
903 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800904
905 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
906 AINPUT_SOURCE_TRACKBALL, SW_LID))
907 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
908
909 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
910 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
911 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
912}
913
914TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800915 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700916 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800917 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800918 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800919 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800920 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100921
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800922 mapper.addSupportedKeyCode(AKEYCODE_A);
923 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700925 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 uint8_t flags[4] = { 0, 0, 0, 1 };
927
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700928 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 << "Should return false when device id is >= 0 but unknown.";
930 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
931
932 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700933 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800934 << "Should return false when device id is valid but the sources are not supported by "
935 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800936 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
937
938 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700939 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800940 keyCodes, flags))
941 << "Should return value provided by mapper when device id is valid and the device "
942 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800943 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
944
945 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700946 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
947 << "Should return false when the device id is < 0 but the sources are not supported by "
948 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800949 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
950
951 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700952 ASSERT_TRUE(
953 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
954 << "Should return value provided by mapper when device id is < 0 and one of the "
955 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800956 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
957}
958
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000959TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800960 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700961 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000962 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800963 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000964 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800965 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800966 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800967 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000969 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000970 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800971 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
972
973 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800974 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000975 ASSERT_EQ(when, event.when);
976 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800977 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800978 ASSERT_EQ(EV_KEY, event.type);
979 ASSERT_EQ(KEY_A, event.code);
980 ASSERT_EQ(1, event.value);
981}
982
Garfield Tan1c7bc862020-01-28 13:24:04 -0800983TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800984 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700985 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800986 constexpr int32_t eventHubId = 1;
987 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800988 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000989 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
990 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800991 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800992 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800993
994 NotifyDeviceResetArgs resetArgs;
995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800996 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800998 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000999 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001001 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001002 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001003
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001004 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001005 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001007 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001008 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001010 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001011 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001013 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001014 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001015}
1016
Garfield Tan1c7bc862020-01-28 13:24:04 -08001017TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1018 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001019 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001020 constexpr int32_t eventHubId = 1;
1021 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1022 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001023 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1024 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001025 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001026 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1027
1028 NotifyDeviceResetArgs resetArgs;
1029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1030 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1031}
1032
Arthur Hungc23540e2018-11-29 20:42:11 +08001033TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001034 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001035 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001036 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001037 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001038 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1039 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001040 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1041 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001042 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001043
1044 const uint8_t hdmi1 = 1;
1045
1046 // Associated touch screen with second display.
1047 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1048
1049 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001050 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001051 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001052 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001053 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001054 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001055 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001056 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001057 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001058
1059 // Add the device, and make sure all of the callbacks are triggered.
1060 // The device is added after the input port associations are processed since
1061 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001062 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001064 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001065
Arthur Hung2c9a3342019-07-23 14:18:59 +08001066 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001067 ASSERT_EQ(deviceId, device->getId());
1068 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1069 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001070
1071 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001072 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001073 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001074 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001075}
1076
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001077TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1078 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001079 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001080 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1081 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1082 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001083 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1084 AINPUT_SOURCE_KEYBOARD);
1085 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1086 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001087 mReader->pushNextDevice(device);
1088 mReader->pushNextDevice(device);
1089 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1090 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1091
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001092 NotifyDeviceResetArgs resetArgs;
1093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1094 ASSERT_EQ(deviceId, resetArgs.deviceId);
1095 ASSERT_TRUE(device->isEnabled());
1096 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1097 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1098
1099 disableDevice(deviceId);
1100 mReader->loopOnce();
1101
1102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1103 ASSERT_EQ(deviceId, resetArgs.deviceId);
1104 ASSERT_FALSE(device->isEnabled());
1105 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1106 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1107
1108 enableDevice(deviceId);
1109 mReader->loopOnce();
1110
1111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1112 ASSERT_EQ(deviceId, resetArgs.deviceId);
1113 ASSERT_TRUE(device->isEnabled());
1114 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1115 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1116}
1117
1118TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1119 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001120 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001121 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1122 // Add two subdevices to device
1123 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1124 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001125 device->addMapper<FakeInputMapper>(eventHubIds[0],
1126 mFakePolicy->getReaderConfiguration(),
1127 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001128 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001129 device->addMapper<FakeInputMapper>(eventHubIds[1],
1130 mFakePolicy->getReaderConfiguration(),
1131 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001132 mReader->pushNextDevice(device);
1133 mReader->pushNextDevice(device);
1134 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1135 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1136
1137 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1138 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1139
1140 ASSERT_EQ(AKEY_STATE_DOWN,
1141 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1142 ASSERT_EQ(AKEY_STATE_DOWN,
1143 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1144 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1145 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1146}
1147
Prabir Pradhan7e186182020-11-10 13:56:45 -08001148TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1149 NotifyPointerCaptureChangedArgs args;
1150
Hiroki Sato25040232024-02-22 17:21:22 +09001151 auto request = mFakePolicy->setPointerCapture(/*window=*/sp<BBinder>::make());
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001152 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001153 mReader->loopOnce();
1154 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001155 ASSERT_TRUE(args.request.isEnable()) << "Pointer Capture should be enabled.";
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001156 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001157
Hiroki Sato25040232024-02-22 17:21:22 +09001158 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001159 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001160 mReader->loopOnce();
1161 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001162 ASSERT_FALSE(args.request.isEnable()) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001163
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001164 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001165 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001166 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001167 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001168 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001169}
1170
Prabir Pradhan018faea2024-05-08 21:52:54 +00001171TEST_F(InputReaderTest, GetLastUsedInputDeviceId) {
1172 constexpr int32_t FIRST_DEVICE_ID = END_RESERVED_ID + 1000;
1173 constexpr int32_t SECOND_DEVICE_ID = FIRST_DEVICE_ID + 1;
1174 FakeInputMapper& firstMapper =
1175 addDeviceWithFakeInputMapper(FIRST_DEVICE_ID, FIRST_DEVICE_ID, "first",
1176 InputDeviceClass::KEYBOARD, AINPUT_SOURCE_KEYBOARD,
1177 /*configuration=*/nullptr);
1178 FakeInputMapper& secondMapper =
1179 addDeviceWithFakeInputMapper(SECOND_DEVICE_ID, SECOND_DEVICE_ID, "second",
1180 InputDeviceClass::TOUCH_MT, AINPUT_SOURCE_STYLUS,
1181 /*configuration=*/nullptr);
1182
1183 ASSERT_EQ(ReservedInputDeviceId::INVALID_INPUT_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1184
1185 // Start a new key gesture from the first device
1186 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
1187 .deviceId(FIRST_DEVICE_ID)
1188 .build()});
1189 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1190 mReader->loopOnce();
1191 ASSERT_EQ(firstMapper.getDeviceId(), mReader->getLastUsedInputDeviceId());
1192
1193 // Start a new touch gesture from the second device
1194 secondMapper.setProcessResult(
1195 {MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
1196 .deviceId(SECOND_DEVICE_ID)
1197 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER))
1198 .build()});
1199 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1200 mReader->loopOnce();
1201 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1202
1203 // Releasing the key is not a new gesture, so it does not update the last used device
1204 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
1205 .deviceId(FIRST_DEVICE_ID)
1206 .build()});
1207 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1208 mReader->loopOnce();
1209 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1210
1211 // But pressing a new key does start a new gesture
1212 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
1213 .deviceId(FIRST_DEVICE_ID)
1214 .build()});
1215 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1216 mReader->loopOnce();
1217 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1218
1219 // Moving or ending a touch gesture does not update the last used device
1220 secondMapper.setProcessResult(
1221 {MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
1222 .deviceId(SECOND_DEVICE_ID)
1223 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1224 .build()});
1225 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1226 mReader->loopOnce();
1227 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1228 secondMapper.setProcessResult({MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
1229 .deviceId(SECOND_DEVICE_ID)
1230 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1231 .build()});
1232 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1233 mReader->loopOnce();
1234 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1235
1236 // Starting a new hover gesture updates the last used device
1237 secondMapper.setProcessResult(
1238 {MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
1239 .deviceId(SECOND_DEVICE_ID)
1240 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1241 .build()});
1242 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1243 mReader->loopOnce();
1244 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1245}
1246
Chris Ye87143712020-11-10 05:05:58 +00001247class FakeVibratorInputMapper : public FakeInputMapper {
1248public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001249 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1250 const InputReaderConfiguration& readerConfig, uint32_t sources)
1251 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001252
1253 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1254};
1255
1256TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1257 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001258 ftl::Flags<InputDeviceClass> deviceClass =
1259 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001260 constexpr int32_t eventHubId = 1;
1261 const char* DEVICE_LOCATION = "BLUETOOTH";
1262 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1263 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001264 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1265 mFakePolicy->getReaderConfiguration(),
1266 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001267 mReader->pushNextDevice(device);
1268
1269 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1270 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1271
1272 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1273 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1274}
1275
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001276// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001277
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001278class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001279public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001280 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001281
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001282 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001283
Andy Chenf9f1a022022-08-29 20:07:10 -04001284 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1285
Chris Yee2b1e5c2021-03-10 22:45:12 -08001286 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1287
1288 void dump(std::string& dump) override {}
1289
1290 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1291 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001292 }
1293
Chris Yee2b1e5c2021-03-10 22:45:12 -08001294 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1295 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001296 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001297
1298 bool setLightColor(int32_t lightId, int32_t color) override {
1299 getDeviceContext().setLightBrightness(lightId, color >> 24);
1300 return true;
1301 }
1302
1303 std::optional<int32_t> getLightColor(int32_t lightId) override {
1304 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1305 if (!result.has_value()) {
1306 return std::nullopt;
1307 }
1308 return result.value() << 24;
1309 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001310
1311 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1312
1313 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1314
1315private:
1316 InputDeviceContext& mDeviceContext;
1317 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1318 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001319 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001320};
1321
Chris Yee2b1e5c2021-03-10 22:45:12 -08001322TEST_F(InputReaderTest, BatteryGetCapacity) {
1323 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001324 ftl::Flags<InputDeviceClass> deviceClass =
1325 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001326 constexpr int32_t eventHubId = 1;
1327 const char* DEVICE_LOCATION = "BLUETOOTH";
1328 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001329 FakePeripheralController& controller =
1330 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001331 mReader->pushNextDevice(device);
1332
1333 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1334
Harry Cuttsa5b71292022-11-28 12:56:17 +00001335 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1336 FakeEventHub::BATTERY_CAPACITY);
1337 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001338}
1339
1340TEST_F(InputReaderTest, BatteryGetStatus) {
1341 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001342 ftl::Flags<InputDeviceClass> deviceClass =
1343 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001344 constexpr int32_t eventHubId = 1;
1345 const char* DEVICE_LOCATION = "BLUETOOTH";
1346 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001347 FakePeripheralController& controller =
1348 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001349 mReader->pushNextDevice(device);
1350
1351 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1352
Harry Cuttsa5b71292022-11-28 12:56:17 +00001353 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1354 FakeEventHub::BATTERY_STATUS);
1355 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001356}
1357
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001358TEST_F(InputReaderTest, BatteryGetDevicePath) {
1359 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1360 ftl::Flags<InputDeviceClass> deviceClass =
1361 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1362 constexpr int32_t eventHubId = 1;
1363 const char* DEVICE_LOCATION = "BLUETOOTH";
1364 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1365 device->addController<FakePeripheralController>(eventHubId);
1366 mReader->pushNextDevice(device);
1367
1368 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1369
Harry Cuttsa5b71292022-11-28 12:56:17 +00001370 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001371}
1372
Chris Ye3fdbfef2021-01-06 18:45:18 -08001373TEST_F(InputReaderTest, LightGetColor) {
1374 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001375 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001376 constexpr int32_t eventHubId = 1;
1377 const char* DEVICE_LOCATION = "BLUETOOTH";
1378 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001379 FakePeripheralController& controller =
1380 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001381 mReader->pushNextDevice(device);
1382 RawLightInfo info = {.id = 1,
1383 .name = "Mono",
1384 .maxBrightness = 255,
1385 .flags = InputLightClass::BRIGHTNESS,
1386 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001387 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1388 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001389
1390 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001391
Harry Cutts33476232023-01-30 19:57:29 +00001392 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1393 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1394 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1395 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001396}
1397
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001398// --- InputReaderIntegrationTest ---
1399
1400// These tests create and interact with the InputReader only through its interface.
1401// The InputReader is started during SetUp(), which starts its processing in its own
1402// thread. The tests use linux uinput to emulate input devices.
1403// NOTE: Interacting with the physical device while these tests are running may cause
1404// the tests to fail.
1405class InputReaderIntegrationTest : public testing::Test {
1406protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001407 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001408 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001409 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001410
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001411 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1412 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1413
Chris Yea52ade12020-08-27 16:49:20 -07001414 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001415#if !defined(__ANDROID__)
1416 GTEST_SKIP();
1417#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001418 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001419
Arpit Singh440bf652023-08-09 09:23:43 +00001420 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001421 }
1422
Chris Yea52ade12020-08-27 16:49:20 -07001423 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001424#if !defined(__ANDROID__)
1425 return;
1426#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001427 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001428 mReader.reset();
1429 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001430 mFakePolicy.clear();
1431 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001432
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001433 std::optional<InputDeviceInfo> waitForDevice(const std::string& deviceName) {
1434 std::chrono::time_point start = std::chrono::steady_clock::now();
1435 while (true) {
1436 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1437 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1438 [&deviceName](const InputDeviceInfo& info) {
1439 return info.getIdentifier().name == deviceName;
1440 });
1441 if (it != inputDevices.end()) {
1442 return std::make_optional(*it);
1443 }
1444 std::this_thread::sleep_for(1ms);
1445 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
1446 if (elapsed > 5s) {
1447 return {};
1448 }
1449 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001450 }
Arpit Singh440bf652023-08-09 09:23:43 +00001451
1452 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001453 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1454 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001455
1456 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1457 *mTestListener);
1458 ASSERT_EQ(mReader->start(), OK);
1459
1460 // Since this test is run on a real device, all the input devices connected
1461 // to the test device will show up in mReader. We wait for those input devices to
1462 // show up before beginning the tests.
Arpit Singh440bf652023-08-09 09:23:43 +00001463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Vaibhav Devmuraric109d812024-07-10 14:21:27 +00001464 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Arpit Singh440bf652023-08-09 09:23:43 +00001465 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001466};
1467
1468TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1469 // An invalid input device that is only used for this test.
1470 class InvalidUinputDevice : public UinputDevice {
1471 public:
Harry Cutts33476232023-01-30 19:57:29 +00001472 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001473
1474 private:
1475 void configureDevice(int fd, uinput_user_dev* device) override {}
1476 };
1477
1478 const size_t numDevices = mFakePolicy->getInputDevices().size();
1479
1480 // UinputDevice does not set any event or key bits, so InputReader should not
1481 // consider it as a valid device.
1482 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1483 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001484 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1485
1486 invalidDevice.reset();
1487 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001488 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1489}
1490
1491TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1492 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1493
1494 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1495 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001496 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1497
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001498 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001499 ASSERT_TRUE(device.has_value());
1500 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1501 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1502 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001503
1504 keyboard.reset();
1505 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001506 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1507}
1508
1509TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1510 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1511 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1512
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001513 NotifyKeyArgs keyArgs;
1514 keyboard->pressAndReleaseHomeKey();
1515 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1516 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001517 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Vaibhav Devmuraribffa9402024-07-16 13:17:33 +00001518
1519 int32_t prevId = keyArgs.id;
1520 nsecs_t prevTimestamp = keyArgs.eventTime;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001521
1522 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1523 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001524 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001525 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001526 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001527}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001528
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001529TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1530 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1531 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1532
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001533 const auto device = waitForDevice(stylus->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001534 ASSERT_TRUE(device.has_value());
1535
Prabir Pradhana3621852022-10-14 18:57:23 +00001536 // An external stylus with buttons should also be recognized as a keyboard.
1537 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001538 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1539 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1540
1541 const auto DOWN =
1542 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1543 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1544
1545 stylus->pressAndReleaseKey(BTN_STYLUS);
1546 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1547 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1548 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1549 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1550
1551 stylus->pressAndReleaseKey(BTN_STYLUS2);
1552 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1553 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1554 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1555 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1556
1557 stylus->pressAndReleaseKey(BTN_STYLUS3);
1558 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1559 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1560 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1561 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1562}
1563
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001564TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1565 std::unique_ptr<UinputKeyboard> keyboard =
1566 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1567 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1568 KEY_R, KEY_T, KEY_Y,
1569 BTN_STYLUS, BTN_STYLUS2,
1570 BTN_STYLUS3});
1571 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1572
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001573 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001574 ASSERT_TRUE(device.has_value());
1575
1576 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1577 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1578 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1579 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1580}
1581
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001582TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1583 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1584 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1585 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1586 createUinputDevice<UinputKeyboardWithHidUsage>(
1587 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1588 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1589
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001590 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001591 ASSERT_TRUE(device.has_value());
1592
1593 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1594 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1595
1596 // If a device supports reporting HID usage codes, it shouldn't automatically support
1597 // stylus keys.
1598 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1599 uint8_t outFlags[] = {0};
1600 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1601 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1602}
1603
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001604/**
1605 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1606 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1607 * are passed to the listener.
1608 */
1609static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1610TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1611 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1612 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1613 NotifyKeyArgs keyArgs;
1614
1615 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1616 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1617 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1618 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1619
1620 controller->pressAndReleaseKey(BTN_GEAR_UP);
1621 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1622 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1623 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1624}
1625
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001626// --- TouchIntegrationTest ---
1627
Arpit Singh440bf652023-08-09 09:23:43 +00001628class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001629protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001630 const std::string UNIQUE_ID = "local:0";
1631
Chris Yea52ade12020-08-27 16:49:20 -07001632 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001633#if !defined(__ANDROID__)
1634 GTEST_SKIP();
1635#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001636 InputReaderIntegrationTest::SetUp();
1637 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001638 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1639 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001640
1641 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1642 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001643 const auto info = waitForDevice(mDevice->getName());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001644 ASSERT_TRUE(info);
1645 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001646 }
1647
Linnan Li13bf76a2024-05-05 19:18:02 +08001648 void setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001649 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001650 std::optional<uint8_t> physicalPort,
1651 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001652 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001653 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001654 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001655 }
1656
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001657 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1658 NotifyMotionArgs args;
1659 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1660 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001661 ASSERT_EQ(points.size(), args.getPointerCount());
1662 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001663 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1664 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1665 }
1666 }
1667
Arthur Hungaab25622020-01-16 11:22:11 +08001668 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001669 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001670};
1671
Arpit Singh440bf652023-08-09 09:23:43 +00001672enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1673
1674class TouchIntegrationTest : public BaseTouchIntegrationTest,
1675 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1676protected:
1677 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1678 const std::string INPUT_PORT = "uinput_touch/input0";
1679
1680 void SetUp() override {
1681#if !defined(__ANDROID__)
1682 GTEST_SKIP();
1683#endif
1684 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1685 BaseTouchIntegrationTest::SetUp();
1686 return;
1687 }
1688
1689 // setup policy with a input-port or UniqueId association to the display
1690 bool isInputPortAssociation =
1691 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1692
1693 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1694 if (isInputPortAssociation) {
1695 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1696 } else {
1697 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1698 }
Arpit Singh440bf652023-08-09 09:23:43 +00001699
1700 InputReaderIntegrationTest::setupInputReader();
1701
1702 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1703 INPUT_PORT);
1704 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1705
1706 // Add a display linked to a physical port or UniqueId.
1707 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1708 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1709 ViewportType::INTERNAL);
1710 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001711 const auto info = waitForDevice(mDevice->getName());
Arpit Singh440bf652023-08-09 09:23:43 +00001712 ASSERT_TRUE(info);
1713 mDeviceInfo = *info;
1714 }
1715};
1716
1717TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001718 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1719 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1720 // presses).
1721 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1722 mDeviceInfo.getSources());
1723}
1724
Arpit Singh440bf652023-08-09 09:23:43 +00001725TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001726 NotifyMotionArgs args;
1727 const Point centerPoint = mDevice->getCenterPoint();
1728
1729 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001730 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001731 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001732 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001733 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1734 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1735
1736 // ACTION_MOVE
1737 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001738 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001739 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1741
1742 // ACTION_UP
1743 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001744 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001745 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1746 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1747}
1748
Arpit Singh440bf652023-08-09 09:23:43 +00001749TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001750 NotifyMotionArgs args;
1751 const Point centerPoint = mDevice->getCenterPoint();
1752
1753 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001754 mDevice->sendSlot(FIRST_SLOT);
1755 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001756 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001757 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001758 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1759 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1760
1761 // ACTION_POINTER_DOWN (Second slot)
1762 const Point secondPoint = centerPoint + Point(100, 100);
1763 mDevice->sendSlot(SECOND_SLOT);
1764 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001765 mDevice->sendDown(secondPoint);
1766 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001767 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001768 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001769
1770 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001771 mDevice->sendMove(secondPoint + Point(1, 1));
1772 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001773 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1775
1776 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001777 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001778 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001779 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001780 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001781
1782 // ACTION_UP
1783 mDevice->sendSlot(FIRST_SLOT);
1784 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001785 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001786 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1787 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1788}
1789
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001790/**
1791 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1792 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1793 * data?
1794 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1795 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1796 * for Pointer 0 only is generated after.
1797 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1798 * events, we will not miss any information.
1799 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1800 * event generated afterwards that contains the newest movement of pointer 0.
1801 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1802 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1803 * losing information about non-palm pointers.
1804 */
Arpit Singh440bf652023-08-09 09:23:43 +00001805TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001806 NotifyMotionArgs args;
1807 const Point centerPoint = mDevice->getCenterPoint();
1808
1809 // ACTION_DOWN
1810 mDevice->sendSlot(FIRST_SLOT);
1811 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1812 mDevice->sendDown(centerPoint);
1813 mDevice->sendSync();
1814 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1815
1816 // ACTION_POINTER_DOWN (Second slot)
1817 const Point secondPoint = centerPoint + Point(100, 100);
1818 mDevice->sendSlot(SECOND_SLOT);
1819 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1820 mDevice->sendDown(secondPoint);
1821 mDevice->sendSync();
1822 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1823
1824 // ACTION_MOVE (First slot)
1825 mDevice->sendSlot(FIRST_SLOT);
1826 mDevice->sendMove(centerPoint + Point(5, 5));
1827 // ACTION_POINTER_UP (Second slot)
1828 mDevice->sendSlot(SECOND_SLOT);
1829 mDevice->sendPointerUp();
1830 // Send a single sync for the above 2 pointer updates
1831 mDevice->sendSync();
1832
1833 // First, we should get POINTER_UP for the second pointer
1834 assertReceivedMotion(ACTION_POINTER_1_UP,
1835 {/*first pointer */ centerPoint + Point(5, 5),
1836 /*second pointer*/ secondPoint});
1837
1838 // Next, the MOVE event for the first pointer
1839 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1840}
1841
1842/**
1843 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1844 * move, and then it will go up, all in the same frame.
1845 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1846 * gets sent to the listener.
1847 */
Arpit Singh440bf652023-08-09 09:23:43 +00001848TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001849 NotifyMotionArgs args;
1850 const Point centerPoint = mDevice->getCenterPoint();
1851
1852 // ACTION_DOWN
1853 mDevice->sendSlot(FIRST_SLOT);
1854 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1855 mDevice->sendDown(centerPoint);
1856 mDevice->sendSync();
1857 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1858
1859 // ACTION_POINTER_DOWN (Second slot)
1860 const Point secondPoint = centerPoint + Point(100, 100);
1861 mDevice->sendSlot(SECOND_SLOT);
1862 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1863 mDevice->sendDown(secondPoint);
1864 mDevice->sendSync();
1865 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1866
1867 // ACTION_MOVE (First slot)
1868 mDevice->sendSlot(FIRST_SLOT);
1869 mDevice->sendMove(centerPoint + Point(5, 5));
1870 // ACTION_POINTER_UP (Second slot)
1871 mDevice->sendSlot(SECOND_SLOT);
1872 mDevice->sendMove(secondPoint + Point(6, 6));
1873 mDevice->sendPointerUp();
1874 // Send a single sync for the above 2 pointer updates
1875 mDevice->sendSync();
1876
1877 // First, we should get POINTER_UP for the second pointer
1878 // The movement of the second pointer during the liftoff frame is ignored.
1879 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1880 assertReceivedMotion(ACTION_POINTER_1_UP,
1881 {/*first pointer */ centerPoint + Point(5, 5),
1882 /*second pointer*/ secondPoint});
1883
1884 // Next, the MOVE event for the first pointer
1885 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1886}
1887
Arpit Singh440bf652023-08-09 09:23:43 +00001888TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001889 NotifyMotionArgs args;
1890 const Point centerPoint = mDevice->getCenterPoint();
1891
1892 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001893 mDevice->sendSlot(FIRST_SLOT);
1894 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001895 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001896 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001897 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1898 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1899
arthurhungcc7f9802020-04-30 17:55:40 +08001900 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001901 const Point secondPoint = centerPoint + Point(100, 100);
1902 mDevice->sendSlot(SECOND_SLOT);
1903 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1904 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001905 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001906 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001907 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001908
arthurhungcc7f9802020-04-30 17:55:40 +08001909 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001910 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001911 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001912 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1914
arthurhungcc7f9802020-04-30 17:55:40 +08001915 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1916 // a palm event.
1917 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001918 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001919 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001920 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001921 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001922 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001923
arthurhungcc7f9802020-04-30 17:55:40 +08001924 // Send up to second slot, expect first slot send moving.
1925 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001926 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001927 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001929
arthurhungcc7f9802020-04-30 17:55:40 +08001930 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001931 mDevice->sendSlot(FIRST_SLOT);
1932 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001933 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001934
arthurhungcc7f9802020-04-30 17:55:40 +08001935 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1936 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001937}
1938
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001939/**
1940 * Some drivers historically have reported axis values outside of the range specified in the
1941 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1942 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1943 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1944 * and no units for pressure (resolution) is specified by the evdev documentation.
1945 */
1946TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1947 const Point centerPoint = mDevice->getCenterPoint();
1948
1949 // Down with pressure outside the reported range
1950 mDevice->sendSlot(FIRST_SLOT);
1951 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1952 mDevice->sendDown(centerPoint);
1953 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1954 mDevice->sendSync();
1955 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1956 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1957
1958 // Move to a point outside the reported range
1959 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1960 mDevice->sendSync();
1961 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1962 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1963
1964 // Up
1965 mDevice->sendUp();
1966 mDevice->sendSync();
1967 ASSERT_NO_FATAL_FAILURE(
1968 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1969}
1970
Arpit Singh440bf652023-08-09 09:23:43 +00001971TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001972 const Point centerPoint = mDevice->getCenterPoint();
1973
1974 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1975 mDevice->sendSlot(FIRST_SLOT);
1976 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1977 mDevice->sendToolType(MT_TOOL_PEN);
1978 mDevice->sendDown(centerPoint);
1979 mDevice->sendSync();
1980 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1981 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001982 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001983
1984 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1985
1986 // Release the stylus touch.
1987 mDevice->sendUp();
1988 mDevice->sendSync();
1989 ASSERT_NO_FATAL_FAILURE(
1990 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1991
1992 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1993
1994 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1995 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1996 mDevice->sendToolType(MT_TOOL_FINGER);
1997 mDevice->sendDown(centerPoint);
1998 mDevice->sendSync();
1999 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2000 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002001 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002002
2003 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2004
2005 mDevice->sendUp();
2006 mDevice->sendSync();
2007 ASSERT_NO_FATAL_FAILURE(
2008 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2009
2010 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2011 // The policy should be notified of the stylus presence.
2012 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2013 mDevice->sendToolType(MT_TOOL_PEN);
2014 mDevice->sendMove(centerPoint);
2015 mDevice->sendSync();
2016 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2017 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002018 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002019
2020 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2021}
2022
Arpit Singh440bf652023-08-09 09:23:43 +00002023TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002024 const Point centerPoint = mDevice->getCenterPoint();
2025
2026 // Down
2027 mDevice->sendSlot(FIRST_SLOT);
2028 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2029 mDevice->sendDown(centerPoint);
2030 mDevice->sendSync();
2031 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2032 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
2033
2034 // Move
2035 mDevice->sendMove(centerPoint + Point(1, 1));
2036 mDevice->sendSync();
2037 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2038 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2039
2040 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2041 auto externalStylus = createUinputDevice<UinputExternalStylus>();
2042 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002043 const auto stylusInfo = waitForDevice(externalStylus->getName());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002044 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002045
2046 // Move
2047 mDevice->sendMove(centerPoint + Point(2, 2));
2048 mDevice->sendSync();
2049 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2050 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2051
2052 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2053 externalStylus.reset();
2054 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002055 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2056
2057 // Up
2058 mDevice->sendUp();
2059 mDevice->sendSync();
2060 ASSERT_NO_FATAL_FAILURE(
2061 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2062
2063 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2064}
2065
Arpit Singh440bf652023-08-09 09:23:43 +00002066INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2067 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2068 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2069 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2070
Prabir Pradhan124ea442022-10-28 20:27:44 +00002071// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002072
Prabir Pradhan124ea442022-10-28 20:27:44 +00002073// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2074// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2075// stylus.
2076template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002077class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002078protected:
2079 void SetUp() override {
2080#if !defined(__ANDROID__)
2081 GTEST_SKIP();
2082#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002083 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002084 mTouchscreen = mDevice.get();
2085 mTouchscreenInfo = mDeviceInfo;
2086
2087 setUpStylusDevice();
2088 }
2089
2090 UinputStylusDevice* mStylus{nullptr};
2091 InputDeviceInfo mStylusInfo{};
2092
2093 UinputTouchScreen* mTouchscreen{nullptr};
2094 InputDeviceInfo mTouchscreenInfo{};
2095
2096private:
2097 // When we are attempting to test stylus button events that are sent from the touchscreen,
2098 // use the same Uinput device for the touchscreen and the stylus.
2099 template <typename T = UinputStylusDevice>
2100 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2101 mStylus = mDevice.get();
2102 mStylusInfo = mDeviceInfo;
2103 }
2104
2105 // When we are attempting to stylus buttons from an external stylus being merged with touches
2106 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2107 template <typename T = UinputStylusDevice>
2108 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2109 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2110 mStylus = mStylusDeviceLifecycleTracker.get();
2111 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002112 const auto info = waitForDevice(mStylus->getName());
Prabir Pradhan124ea442022-10-28 20:27:44 +00002113 ASSERT_TRUE(info);
2114 mStylusInfo = *info;
2115 }
2116
2117 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2118
2119 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002120 using BaseTouchIntegrationTest::mDevice;
2121 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002122};
2123
2124using StylusButtonIntegrationTestTypes =
2125 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2126TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2127
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002128TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002129 const auto stylusId = TestFixture::mStylusInfo.getId();
2130
2131 TestFixture::mStylus->pressKey(BTN_STYLUS);
2132 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2133 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2134 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2135
2136 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2137 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002138 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002139 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002140}
2141
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002142TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002143 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2144 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2145 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002146
2147 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002148 TestFixture::mStylus->pressKey(BTN_STYLUS);
2149 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002150 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002151 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002152
2153 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002154 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2155 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2156 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2157 TestFixture::mTouchscreen->sendDown(centerPoint);
2158 TestFixture::mTouchscreen->sendSync();
2159 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002160 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002161 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002162 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2163 WithDeviceId(touchscreenId))));
2164 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002165 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002166 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002167 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2168 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002169
Prabir Pradhan124ea442022-10-28 20:27:44 +00002170 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2171 TestFixture::mTouchscreen->sendSync();
2172 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002173 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002174 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002175 WithDeviceId(touchscreenId))));
2176 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002177 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002178 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002179 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002180
2181 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002182 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2183 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002184 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002185 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002186}
2187
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002188TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002189 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2190 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2191 const auto stylusId = TestFixture::mStylusInfo.getId();
2192 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002193 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002194
2195 // Press the stylus button.
2196 TestFixture::mStylus->pressKey(BTN_STYLUS);
2197 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2198 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2199 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2200
2201 // Start hovering with the stylus.
2202 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2203 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2204 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2205 TestFixture::mTouchscreen->sendMove(centerPoint);
2206 TestFixture::mTouchscreen->sendSync();
2207 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2208 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2209 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2210 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2211 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2212 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2213 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2214 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2215 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2216
2217 // Touch down with the stylus.
2218 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2219 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2220 TestFixture::mTouchscreen->sendDown(centerPoint);
2221 TestFixture::mTouchscreen->sendSync();
2222 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2223 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2224 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2225
2226 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2227 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2228 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2229
2230 // Stop touching with the stylus, and start hovering.
2231 TestFixture::mTouchscreen->sendUp();
2232 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2233 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2234 TestFixture::mTouchscreen->sendMove(centerPoint);
2235 TestFixture::mTouchscreen->sendSync();
2236 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2237 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2238 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2239 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2240 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2241 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2242 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2243 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2244 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2245
2246 // Stop hovering.
2247 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2248 TestFixture::mTouchscreen->sendSync();
2249 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2250 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2251 WithButtonState(0))));
2252 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2253 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2254 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2255 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2256
2257 // Release the stylus button.
2258 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2259 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2260 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2261 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2262}
2263
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002264TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002265 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2266 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2267 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002268
2269 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002270 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2271 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2272 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2273 TestFixture::mTouchscreen->sendDown(centerPoint);
2274 TestFixture::mTouchscreen->sendSync();
2275 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002276 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002277 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002278 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002279
2280 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002281 TestFixture::mStylus->pressKey(BTN_STYLUS);
2282 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002283 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002284 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2285 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002286 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002287 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002288 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2289 WithDeviceId(touchscreenId))));
2290 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002291 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002292 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002293 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2294 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002295
Prabir Pradhan124ea442022-10-28 20:27:44 +00002296 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2297 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002298 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002299 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2300 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002301 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002302 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002303 WithDeviceId(touchscreenId))));
2304 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002305 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002306 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002307 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002308
2309 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002310 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2311 TestFixture::mTouchscreen->sendSync();
2312 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002313 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002314 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002315 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002316}
2317
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002318TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002319 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2320 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002321 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002322
2323 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2324 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2325 const auto stylusId = TestFixture::mStylusInfo.getId();
2326
2327 // Start a stylus gesture. By the time this event is processed, the configuration change that
2328 // was requested is guaranteed to be completed.
2329 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2330 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2331 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2332 TestFixture::mTouchscreen->sendDown(centerPoint);
2333 TestFixture::mTouchscreen->sendSync();
2334 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2335 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002336 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002337 WithDeviceId(touchscreenId))));
2338
2339 // Press and release a stylus button. Each change only generates a MOVE motion event.
2340 // Key events are unaffected.
2341 TestFixture::mStylus->pressKey(BTN_STYLUS);
2342 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2343 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2344 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2345 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2346 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002347 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002348 WithDeviceId(touchscreenId))));
2349
2350 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2351 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2352 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2353 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2354 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2355 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002356 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002357 WithDeviceId(touchscreenId))));
2358
2359 // Finish the stylus gesture.
2360 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2361 TestFixture::mTouchscreen->sendSync();
2362 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2363 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002364 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002365 WithDeviceId(touchscreenId))));
2366}
2367
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002368// --- ExternalStylusIntegrationTest ---
2369
2370// Verify the behavior of an external stylus. An external stylus can report pressure or button
2371// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2372// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002373using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002374
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002375TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2376 // Create an external stylus capable of reporting pressure data that
2377 // should be fused with a touch pointer.
2378 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2379 createUinputDevice<UinputExternalStylusWithPressure>();
2380 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002381 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002382 ASSERT_TRUE(stylusInfo);
2383
2384 // Connecting an external stylus changes the source of the touchscreen.
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002385 const auto deviceInfo = waitForDevice(mDevice->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002386 ASSERT_TRUE(deviceInfo);
2387 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2388}
2389
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002390TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002391 const Point centerPoint = mDevice->getCenterPoint();
2392
2393 // Create an external stylus capable of reporting pressure data that
2394 // should be fused with a touch pointer.
2395 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2396 createUinputDevice<UinputExternalStylusWithPressure>();
2397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002398 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002399 ASSERT_TRUE(stylusInfo);
2400
2401 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2402
2403 const auto touchscreenId = mDeviceInfo.getId();
2404
2405 // Set a pressure value on the stylus. It doesn't generate any events.
2406 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2407 stylus->setPressure(100);
2408 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2409
2410 // Start a finger gesture, and ensure it shows up as stylus gesture
2411 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002412 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002413 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002414 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002415 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002416 mDevice->sendSync();
2417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002418 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2419 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2420 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002421
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002422 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2423 // event with the updated pressure.
2424 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002426 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2427 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2428 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002429
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002430 // The external stylus did not generate any events.
2431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2432 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2433}
2434
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002435TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002436 const Point centerPoint = mDevice->getCenterPoint();
2437
2438 // Create an external stylus capable of reporting pressure data that
2439 // should be fused with a touch pointer.
2440 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2441 createUinputDevice<UinputExternalStylusWithPressure>();
2442 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002443 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002444 ASSERT_TRUE(stylusInfo);
2445
2446 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2447
2448 const auto touchscreenId = mDeviceInfo.getId();
2449
2450 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2451 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002452 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2453 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002454 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002455 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002456
2457 // Start a finger gesture. The touch device will withhold generating any touches for
2458 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2459 mDevice->sendSlot(FIRST_SLOT);
2460 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2461 mDevice->sendToolType(MT_TOOL_FINGER);
2462 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002463 const auto syncTime = std::chrono::system_clock::now();
2464 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2465 // is generated in that period, there will be a race condition between the event being generated
2466 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2467 // will reduce the liklihood of the race condition occurring.
2468 const auto waitUntilTimeForNoEvent =
2469 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002470 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002473 // Since the external stylus did not report a pressure value within the timeout,
2474 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002475 const auto waitUntilTimeForEvent = syncTime +
2476 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2477 ASSERT_NO_FATAL_FAILURE(
2478 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2479 AMOTION_EVENT_ACTION_DOWN),
2480 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2481 AINPUT_SOURCE_STYLUS),
2482 WithToolType(ToolType::FINGER),
2483 WithDeviceId(touchscreenId),
2484 WithPressure(1.f)),
2485 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002486
2487 // Change the pressure on the external stylus. Since the pressure was not present at the start
2488 // of the gesture, it is ignored for now.
2489 stylus->setPressure(200);
2490 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2491
2492 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002493 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2494 mDevice->sendSync();
2495 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2496 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002497 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002498 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002499
2500 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2501 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2502 mDevice->sendToolType(MT_TOOL_FINGER);
2503 mDevice->sendDown(centerPoint);
2504 mDevice->sendSync();
2505 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002506 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2507 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2508 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002509
2510 // The external stylus did not generate any events.
2511 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2512 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002514
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002515TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002516 const Point centerPoint = mDevice->getCenterPoint();
2517
2518 // Create an external stylus device that does not support pressure. It should not affect any
2519 // touch pointers.
2520 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2521 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002522 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002523 ASSERT_TRUE(stylusInfo);
2524
2525 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2526
2527 const auto touchscreenId = mDeviceInfo.getId();
2528
2529 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2530 // pressure data from the external stylus.
2531 mDevice->sendSlot(FIRST_SLOT);
2532 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2533 mDevice->sendToolType(MT_TOOL_FINGER);
2534 mDevice->sendDown(centerPoint);
2535 auto waitUntil = std::chrono::system_clock::now() +
2536 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2537 mDevice->sendSync();
2538 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002539 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2540 AMOTION_EVENT_ACTION_DOWN),
2541 WithToolType(ToolType::FINGER),
2542 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2543 AINPUT_SOURCE_STYLUS),
2544 WithButtonState(0),
2545 WithDeviceId(touchscreenId),
2546 WithPressure(1.f)),
2547 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002548
2549 // The external stylus did not generate any events.
2550 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2551 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2552}
2553
Michael Wrightd02c5b62014-02-10 15:10:22 -08002554// --- InputDeviceTest ---
2555class InputDeviceTest : public testing::Test {
2556protected:
2557 static const char* DEVICE_NAME;
2558 static const char* DEVICE_LOCATION;
2559 static const int32_t DEVICE_ID;
2560 static const int32_t DEVICE_GENERATION;
2561 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002562 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563 static const int32_t EVENTHUB_ID;
2564 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2565
2566 std::shared_ptr<FakeEventHub> mFakeEventHub;
2567 sp<FakeInputReaderPolicy> mFakePolicy;
2568 std::unique_ptr<TestInputListener> mFakeListener;
2569 std::unique_ptr<InstrumentedInputReader> mReader;
2570 std::shared_ptr<InputDevice> mDevice;
2571
2572 void SetUp() override {
2573 mFakeEventHub = std::make_unique<FakeEventHub>();
2574 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2575 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002576 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002578 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579 identifier.name = DEVICE_NAME;
2580 identifier.location = DEVICE_LOCATION;
2581 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2582 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2583 identifier);
2584 mReader->pushNextDevice(mDevice);
2585 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002586 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002588
2589 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002590 mFakeListener.reset();
2591 mFakePolicy.clear();
2592 }
2593};
2594
2595const char* InputDeviceTest::DEVICE_NAME = "device";
2596const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2597const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2598const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002599const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002600const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2601 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002602const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002603const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2604
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002605TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002606 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002607 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2608 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002609}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002610
Michael Wrightd02c5b62014-02-10 15:10:22 -08002611TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2612 ASSERT_EQ(mDevice->isEnabled(), false);
2613}
2614
2615TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2616 // Configuration.
2617 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002618 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619
2620 // Reset.
2621 unused += mDevice->reset(ARBITRARY_TIME);
2622
2623 NotifyDeviceResetArgs resetArgs;
2624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2625 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2626 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2627
2628 // Metadata.
2629 ASSERT_TRUE(mDevice->isIgnored());
2630 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2631
2632 InputDeviceInfo info = mDevice->getDeviceInfo();
2633 ASSERT_EQ(DEVICE_ID, info.getId());
2634 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2635 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2636 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2637
2638 // State queries.
2639 ASSERT_EQ(0, mDevice->getMetaState());
2640
2641 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2642 << "Ignored device should return unknown key code state.";
2643 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2644 << "Ignored device should return unknown scan code state.";
2645 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2646 << "Ignored device should return unknown switch state.";
2647
2648 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2649 uint8_t flags[2] = { 0, 1 };
2650 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2651 << "Ignored device should never mark any key codes.";
2652 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2653 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2654}
2655
2656TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2657 // Configuration.
2658 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2659
2660 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002661 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2662 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2664 mapper1.setMetaState(AMETA_ALT_ON);
2665 mapper1.addSupportedKeyCode(AKEYCODE_A);
2666 mapper1.addSupportedKeyCode(AKEYCODE_B);
2667 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2668 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2669 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2670 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2671 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2672
2673 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002674 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2675 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002676 mapper2.setMetaState(AMETA_SHIFT_ON);
2677
2678 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002679 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002680
Harry Cuttsf13161a2023-03-08 14:15:49 +00002681 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2682 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002683 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002684 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002686 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2687 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002688
2689 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002690 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002691 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2692 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002693
2694 NotifyDeviceResetArgs resetArgs;
2695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2696 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2697 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2698
2699 // Metadata.
2700 ASSERT_FALSE(mDevice->isIgnored());
2701 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2702
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002703 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002705 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2707 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2708
2709 // State queries.
2710 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2711 << "Should query mappers and combine meta states.";
2712
2713 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2714 << "Should return unknown key code state when source not supported.";
2715 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2716 << "Should return unknown scan code state when source not supported.";
2717 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2718 << "Should return unknown switch state when source not supported.";
2719
2720 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2721 << "Should query mapper when source is supported.";
2722 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2723 << "Should query mapper when source is supported.";
2724 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2725 << "Should query mapper when source is supported.";
2726
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002727 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002728 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002729 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002730 << "Should do nothing when source is unsupported.";
2731 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2732 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2733 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2734 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2735
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002736 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002737 << "Should query mapper when source is supported.";
2738 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2739 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2740 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2741 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2742
2743 // Event handling.
2744 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002745 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002746 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002747
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002748 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2749 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002750}
2751
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002752TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2753 // Set some behavior to force the configuration to be update.
2754 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2755 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2756 AINPUT_SOURCE_KEYBOARD);
2757
2758 std::list<NotifyArgs> unused =
2759 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2760 /*changes=*/{});
2761
2762 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2763}
2764
2765TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2766 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2767 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2768 AINPUT_SOURCE_KEYBOARD);
2769
2770 std::list<NotifyArgs> unused =
2771 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2772 /*changes=*/{});
2773
2774 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2775}
2776
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002777TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2778 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2779 FakeInputMapper& mapper =
2780 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2781 AINPUT_SOURCE_KEYBOARD);
2782 NotifyMotionArgs args1;
2783 NotifySwitchArgs args2;
2784 NotifyKeyArgs args3;
2785 mapper.setProcessResult({args1, args2, args3});
2786
2787 InputReaderConfiguration config;
2788 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2789
2790 RawEvent event;
2791 event.deviceId = EVENTHUB_ID;
2792 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2793
2794 for (auto& arg : notifyArgs) {
2795 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2796 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2797 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2798 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2799 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2800 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2801 }
2802 }
2803}
2804
2805TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2806 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2807 FakeInputMapper& mapper =
2808 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2809 AINPUT_SOURCE_KEYBOARD);
2810 NotifyMotionArgs args;
2811 mapper.setProcessResult({args});
2812
2813 InputReaderConfiguration config;
2814 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2815
2816 RawEvent event;
2817 event.deviceId = EVENTHUB_ID;
2818 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2819
2820 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2821 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2822}
2823
2824TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2825 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2826 FakeInputMapper& mapper =
2827 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2828 AINPUT_SOURCE_KEYBOARD);
2829 NotifyMotionArgs args;
2830 args.policyFlags = POLICY_FLAG_WAKE;
2831 mapper.setProcessResult({args});
2832
2833 InputReaderConfiguration config;
2834 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2835
2836 RawEvent event;
2837 event.deviceId = EVENTHUB_ID;
2838 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2839
2840 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2841 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2842}
2843
Arthur Hung2c9a3342019-07-23 14:18:59 +08002844// A single input device is associated with a specific display. Check that:
2845// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002846// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002847TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002848 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2849 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002850
2851 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002852 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002853 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2854 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002855
2856 // Device should be enabled by default.
2857 ASSERT_TRUE(mDevice->isEnabled());
2858
2859 // Prepare associated info.
2860 constexpr uint8_t hdmi = 1;
2861 const std::string UNIQUE_ID = "local:1";
2862
2863 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002864 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002865 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002866 // Device should be disabled because it is associated with a specific display via
2867 // input port <-> display port association, but the corresponding display is not found
2868 ASSERT_FALSE(mDevice->isEnabled());
2869
2870 // Prepare displays.
2871 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002872 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002873 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002874 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002875 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002876 ASSERT_TRUE(mDevice->isEnabled());
2877
2878 // Device should be disabled after set disable.
2879 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002880 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002881 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002882 ASSERT_FALSE(mDevice->isEnabled());
2883
2884 // Device should still be disabled even found the associated display.
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::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002887 ASSERT_FALSE(mDevice->isEnabled());
2888}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889
Christine Franks1ba71cc2021-04-07 14:37:42 -07002890TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2891 // Device should be enabled by default.
2892 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002893 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2894 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002895 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002896 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2897 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002898 ASSERT_TRUE(mDevice->isEnabled());
2899
2900 // Device should be disabled because it is associated with a specific display, but the
2901 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002902 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002903 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002904 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002905 ASSERT_FALSE(mDevice->isEnabled());
2906
2907 // Device should be enabled when a display is found.
2908 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002909 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002910 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002911 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002912 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002913 ASSERT_TRUE(mDevice->isEnabled());
2914
2915 // Device should be disabled after set disable.
2916 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002917 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002918 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002919 ASSERT_FALSE(mDevice->isEnabled());
2920
2921 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002922 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002923 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002924 ASSERT_FALSE(mDevice->isEnabled());
2925}
2926
Christine Franks2a2293c2022-01-18 11:51:16 -08002927TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2928 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002929 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2930 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002931 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002932 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2933 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002934
Christine Franks2a2293c2022-01-18 11:51:16 -08002935 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2936 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002937 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002938 NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002939 const auto initialGeneration = mDevice->getGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002940 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002941 InputReaderConfiguration::Change::DISPLAY_INFO);
Antonio Kantek0ac5e092024-04-22 17:10:27 +00002942 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueIdByPort());
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002943 ASSERT_GT(mDevice->getGeneration(), initialGeneration);
2944 ASSERT_EQ(mDevice->getDeviceInfo().getAssociatedDisplayId(), SECONDARY_DISPLAY_ID);
Christine Franks2a2293c2022-01-18 11:51:16 -08002945}
2946
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002947/**
2948 * This test reproduces a crash caused by a dangling reference that remains after device is added
2949 * and removed. The reference is accessed in InputDevice::dump(..);
2950 */
2951TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2952 constexpr int32_t TEST_EVENTHUB_ID = 10;
2953 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2954
Harry Cutts33476232023-01-30 19:57:29 +00002955 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002956 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2957 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002958 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2959 std::string dumpStr, eventHubDevStr;
2960 device.dump(dumpStr, eventHubDevStr);
2961}
2962
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002963TEST_F(InputDeviceTest, GetBluetoothAddress) {
2964 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2965 ASSERT_TRUE(address);
2966 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2967}
2968
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002969TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2970 mFakePolicy->clearViewports();
2971 FakeInputMapper& mapper =
2972 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2973 AINPUT_SOURCE_KEYBOARD);
2974 std::list<NotifyArgs> unused =
2975 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2976 /*changes=*/{});
2977
2978 mapper.assertConfigureWasCalled();
2979 mapper.assertResetWasNotCalled();
2980
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002981 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002982 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002983 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002984 .type = EV_SYN,
2985 .code = SYN_REPORT,
2986 .value = 0};
2987
2988 // Events are processed normally.
2989 unused = mDevice->process(&event, /*count=*/1);
2990 mapper.assertProcessWasCalled();
2991
2992 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002993 event.type = EV_SYN;
2994 event.code = SYN_DROPPED;
2995 event.value = 0;
2996 unused = mDevice->process(&event, /*count=*/1);
2997 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002998
2999 // All events until the next SYN_REPORT should be dropped.
3000 event.type = EV_KEY;
3001 event.code = KEY_A;
3002 event.value = 1;
3003 unused = mDevice->process(&event, /*count=*/1);
3004 mapper.assertProcessWasNotCalled();
3005
3006 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00003007 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003008 event.type = EV_SYN;
3009 event.code = SYN_REPORT;
3010 event.value = 0;
3011 unused = mDevice->process(&event, /*count=*/1);
3012 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00003013 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003014
3015 // The mapper receives events normally now.
3016 event.type = EV_KEY;
3017 event.code = KEY_B;
3018 event.value = 1;
3019 unused = mDevice->process(&event, /*count=*/1);
3020 mapper.assertProcessWasCalled();
3021}
3022
Michael Wrightd02c5b62014-02-10 15:10:22 -08003023// --- SwitchInputMapperTest ---
3024
3025class SwitchInputMapperTest : public InputMapperTest {
3026protected:
3027};
3028
3029TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003030 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003032 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003033}
3034
3035TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003036 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003037
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003038 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003039 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003040
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003041 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003042 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003043}
3044
3045TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003046 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003047 std::list<NotifyArgs> out;
3048 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3049 ASSERT_TRUE(out.empty());
3050 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3051 ASSERT_TRUE(out.empty());
3052 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3053 ASSERT_TRUE(out.empty());
3054 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003055
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003056 ASSERT_EQ(1u, out.size());
3057 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003058 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003059 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3060 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003061 args.switchMask);
3062 ASSERT_EQ(uint32_t(0), args.policyFlags);
3063}
3064
Chris Ye87143712020-11-10 05:05:58 +00003065// --- VibratorInputMapperTest ---
3066class VibratorInputMapperTest : public InputMapperTest {
3067protected:
3068 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3069};
3070
3071TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003072 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003073
3074 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3075}
3076
3077TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003078 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003079
3080 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3081}
3082
3083TEST_F(VibratorInputMapperTest, Vibrate) {
3084 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003085 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003086 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003087
3088 VibrationElement pattern(2);
3089 VibrationSequence sequence(2);
3090 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003091 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3092 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003093 sequence.addElement(pattern);
3094 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003095 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3096 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003097 sequence.addElement(pattern);
3098
3099 std::vector<int64_t> timings = {0, 1};
3100 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3101
3102 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003103 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003104 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003105 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003106 // Verify vibrator state listener was notified.
3107 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003108 ASSERT_EQ(1u, out.size());
3109 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3110 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3111 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003112 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003113 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003114 ASSERT_FALSE(mapper.isVibrating());
3115 // Verify vibrator state listener was notified.
3116 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003117 ASSERT_EQ(1u, out.size());
3118 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3119 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3120 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003121}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003122
Chris Yef59a2f42020-10-16 12:55:26 -07003123// --- SensorInputMapperTest ---
3124
3125class SensorInputMapperTest : public InputMapperTest {
3126protected:
3127 static const int32_t ACCEL_RAW_MIN;
3128 static const int32_t ACCEL_RAW_MAX;
3129 static const int32_t ACCEL_RAW_FUZZ;
3130 static const int32_t ACCEL_RAW_FLAT;
3131 static const int32_t ACCEL_RAW_RESOLUTION;
3132
3133 static const int32_t GYRO_RAW_MIN;
3134 static const int32_t GYRO_RAW_MAX;
3135 static const int32_t GYRO_RAW_FUZZ;
3136 static const int32_t GYRO_RAW_FLAT;
3137 static const int32_t GYRO_RAW_RESOLUTION;
3138
3139 static const float GRAVITY_MS2_UNIT;
3140 static const float DEGREE_RADIAN_UNIT;
3141
3142 void prepareAccelAxes();
3143 void prepareGyroAxes();
3144 void setAccelProperties();
3145 void setGyroProperties();
3146 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3147};
3148
3149const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3150const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3151const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3152const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3153const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3154
3155const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3156const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3157const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3158const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3159const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3160
3161const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3162const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3163
3164void SensorInputMapperTest::prepareAccelAxes() {
3165 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3166 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3167 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3168 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3169 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3170 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3171}
3172
3173void SensorInputMapperTest::prepareGyroAxes() {
3174 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3175 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3176 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3177 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3178 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3179 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3180}
3181
3182void SensorInputMapperTest::setAccelProperties() {
3183 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3184 /* sensorDataIndex */ 0);
3185 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3186 /* sensorDataIndex */ 1);
3187 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3188 /* sensorDataIndex */ 2);
3189 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3190 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3191 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3192 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3193 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3194}
3195
3196void SensorInputMapperTest::setGyroProperties() {
3197 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3198 /* sensorDataIndex */ 0);
3199 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3200 /* sensorDataIndex */ 1);
3201 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3202 /* sensorDataIndex */ 2);
3203 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3204 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3205 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3206 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3207 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3208}
3209
3210TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003211 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003212
3213 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3214}
3215
3216TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3217 setAccelProperties();
3218 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003219 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003220
3221 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3222 std::chrono::microseconds(10000),
3223 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003224 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003230
3231 NotifySensorArgs args;
3232 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3233 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3234 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3235
3236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3237 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3238 ASSERT_EQ(args.deviceId, DEVICE_ID);
3239 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3240 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3241 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3242 ASSERT_EQ(args.values, values);
3243 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3244}
3245
3246TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3247 setGyroProperties();
3248 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003249 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003250
3251 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3252 std::chrono::microseconds(10000),
3253 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003254 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3257 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3258 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003260
3261 NotifySensorArgs args;
3262 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3263 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3264 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3265
3266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3267 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3268 ASSERT_EQ(args.deviceId, DEVICE_ID);
3269 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3270 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3271 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3272 ASSERT_EQ(args.values, values);
3273 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3274}
3275
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276// --- KeyboardInputMapperTest ---
3277
3278class KeyboardInputMapperTest : public InputMapperTest {
3279protected:
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003280 void SetUp() override {
3281 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
3282 InputDeviceClass::ALPHAKEY);
3283 }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003284 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003285 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003286 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003287
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003289 int32_t originalKeyCode, int32_t rotatedKeyCode,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003290 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291};
3292
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003293/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3294 * orientation.
3295 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003296void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003297 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3298 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003299}
3300
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003301void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003302 int32_t originalScanCode, int32_t originalKeyCode,
Linnan Li13bf76a2024-05-05 19:18:02 +08003303 int32_t rotatedKeyCode,
3304 ui::LogicalDisplayId displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305 NotifyKeyArgs args;
3306
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3309 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3310 ASSERT_EQ(originalScanCode, args.scanCode);
3311 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003312 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3316 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3317 ASSERT_EQ(originalScanCode, args.scanCode);
3318 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003319 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003320}
3321
Michael Wrightd02c5b62014-02-10 15:10:22 -08003322TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003323 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003324 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003326 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003327}
3328
3329TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3330 const int32_t USAGE_A = 0x070004;
3331 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3333 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003334 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3335 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3336 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003337
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003339 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003340 // Initial metastate is AMETA_NONE.
3341 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342
3343 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345 NotifyKeyArgs args;
3346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3347 ASSERT_EQ(DEVICE_ID, args.deviceId);
3348 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3349 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3350 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3351 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3352 ASSERT_EQ(KEY_HOME, args.scanCode);
3353 ASSERT_EQ(AMETA_NONE, args.metaState);
3354 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3355 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3356 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3357
3358 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003359 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3361 ASSERT_EQ(DEVICE_ID, args.deviceId);
3362 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3363 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3364 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3365 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3366 ASSERT_EQ(KEY_HOME, args.scanCode);
3367 ASSERT_EQ(AMETA_NONE, args.metaState);
3368 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3369 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3370 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3371
3372 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003373 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3374 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3376 ASSERT_EQ(DEVICE_ID, args.deviceId);
3377 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3378 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3379 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3380 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3381 ASSERT_EQ(0, args.scanCode);
3382 ASSERT_EQ(AMETA_NONE, args.metaState);
3383 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3384 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3385 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3386
3387 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003388 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3389 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3391 ASSERT_EQ(DEVICE_ID, args.deviceId);
3392 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3393 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3395 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3396 ASSERT_EQ(0, args.scanCode);
3397 ASSERT_EQ(AMETA_NONE, args.metaState);
3398 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3399 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3400 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3401
3402 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003403 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3406 ASSERT_EQ(DEVICE_ID, args.deviceId);
3407 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3408 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3409 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3410 ASSERT_EQ(0, args.keyCode);
3411 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3412 ASSERT_EQ(AMETA_NONE, args.metaState);
3413 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3414 ASSERT_EQ(0U, args.policyFlags);
3415 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3416
3417 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3419 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3421 ASSERT_EQ(DEVICE_ID, args.deviceId);
3422 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3423 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3424 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3425 ASSERT_EQ(0, args.keyCode);
3426 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3427 ASSERT_EQ(AMETA_NONE, args.metaState);
3428 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3429 ASSERT_EQ(0U, args.policyFlags);
3430 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3431}
3432
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003433TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3434 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3435 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3436 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3437
3438 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003439 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003440
3441 // Key down by scan code.
3442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3443 NotifyKeyArgs args;
3444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3445 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3446
3447 // Key up by scan code.
3448 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3450 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3451}
3452
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003453/**
3454 * Ensure that the readTime is set to the time when the EV_KEY is received.
3455 */
3456TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3457 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3458
3459 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003460 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003461 NotifyKeyArgs args;
3462
3463 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003464 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3466 ASSERT_EQ(12, args.readTime);
3467
3468 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003469 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3471 ASSERT_EQ(15, args.readTime);
3472}
3473
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003475 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3476 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003477 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3478 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3479 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003481 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003482 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483
Arthur Hung95f68612022-04-07 14:08:22 +08003484 // Initial metastate is AMETA_NONE.
3485 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486
3487 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003489 NotifyKeyArgs args;
3490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3491 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003492 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003493 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494
3495 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003496 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3498 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003499 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003500
3501 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003502 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3504 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003505 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003506
3507 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003508 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3510 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003511 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003512 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513}
3514
3515TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003516 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3517 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3518 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3519 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003522 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523
Michael Wrighta9cf4192022-12-01 23:46:39 +00003524 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3526 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3527 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3528 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3529 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3530 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3531 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3532 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3533}
3534
3535TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003536 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3537 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3538 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3539 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003543 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544
Michael Wrighta9cf4192022-12-01 23:46:39 +00003545 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003546 ASSERT_NO_FATAL_FAILURE(
3547 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3548 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3549 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3550 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3551 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3552 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3553 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003554
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003555 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003556 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003557 ASSERT_NO_FATAL_FAILURE(
3558 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3559 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3560 AKEYCODE_DPAD_UP, DISPLAY_ID));
3561 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3562 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3563 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3564 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003566 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003567 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003568 ASSERT_NO_FATAL_FAILURE(
3569 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3570 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3571 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3572 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3573 AKEYCODE_DPAD_UP, DISPLAY_ID));
3574 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3575 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003576
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003577 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003578 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003579 ASSERT_NO_FATAL_FAILURE(
3580 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3581 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3582 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3583 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3584 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3586 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003587
3588 // Special case: if orientation changes while key is down, we still emit the same keycode
3589 // in the key up as we did in the key down.
3590 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003591 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003592 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3595 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3596 ASSERT_EQ(KEY_UP, args.scanCode);
3597 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3598
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003599 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003600 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003601 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3603 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3604 ASSERT_EQ(KEY_UP, args.scanCode);
3605 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3606}
3607
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003608TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3609 // If the keyboard is not orientation aware,
3610 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003611 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003612
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003613 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003614 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003615 NotifyKeyArgs args;
3616
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003617 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003618 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003622 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003623
Michael Wrighta9cf4192022-12-01 23:46:39 +00003624 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003627 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003629 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003630}
3631
3632TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3633 // If the keyboard is orientation aware,
3634 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003636
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003637 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003638 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003639 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003640 NotifyKeyArgs args;
3641
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003642 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003643 // ^--- already checked by the previous test
3644
Michael Wrighta9cf4192022-12-01 23:46:39 +00003645 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003646 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3651 ASSERT_EQ(DISPLAY_ID, args.displayId);
3652
Linnan Li13bf76a2024-05-05 19:18:02 +08003653 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003654 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003655 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003656 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3661 ASSERT_EQ(newDisplayId, args.displayId);
3662}
3663
Michael Wrightd02c5b62014-02-10 15:10:22 -08003664TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003665 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003666 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003667
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003668 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003671 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003672 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673}
3674
Philip Junker4af3b3d2021-12-14 10:36:55 +01003675TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3676 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003677 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Philip Junker4af3b3d2021-12-14 10:36:55 +01003678
3679 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3680 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3681 << "If a mapping is available, the result is equal to the mapping";
3682
3683 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3684 << "If no mapping is available, the result is the key location";
3685}
3686
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003688 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003689 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003690
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003691 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003692 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003694 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003695 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696}
3697
3698TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003699 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003700 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003702 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003705 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706 ASSERT_TRUE(flags[0]);
3707 ASSERT_FALSE(flags[1]);
3708}
3709
3710TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003711 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3712 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3713 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3714 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3715 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3716 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003718 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003719 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003720 // Initial metastate is AMETA_NONE.
3721 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722
3723 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003724 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3725 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3726 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727
3728 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003729 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003731 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3732 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3733 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
3736 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003739 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3740 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3741 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003742 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743
3744 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003745 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003747 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3748 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3749 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003750 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751
3752 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003753 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3754 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003755 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3756 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3757 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003758 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759
3760 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003763 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3764 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3765 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003767
3768 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003769 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3770 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003771 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3772 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3773 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003774 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003775}
3776
Chris Yea52ade12020-08-27 16:49:20 -07003777TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3778 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3779 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3780 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3781 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3782
3783 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003784 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Chris Yea52ade12020-08-27 16:49:20 -07003785
Chris Yea52ade12020-08-27 16:49:20 -07003786 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003787 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003788 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3789 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3790 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3791 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3792
3793 NotifyKeyArgs args;
3794 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3797 ASSERT_EQ(AMETA_NONE, args.metaState);
3798 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3799 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3800 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3801
3802 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003803 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3805 ASSERT_EQ(AMETA_NONE, args.metaState);
3806 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3807 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3808 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3809}
3810
Arthur Hung2c9a3342019-07-23 14:18:59 +08003811TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3812 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003813 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3814 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3815 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3816 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003817
3818 // keyboard 2.
3819 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003820 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003821 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003822 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003823 std::shared_ptr<InputDevice> device2 =
3824 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003825 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003826
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003827 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3828 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3829 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3830 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003831
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003832 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003833 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003834
Arpit Singh67ca6842023-04-26 14:43:16 +00003835 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003836 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003837 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3838 mFakePolicy
3839 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003840 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003841 std::list<NotifyArgs> unused =
3842 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003843 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003844 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003845
3846 // Prepared displays and associated info.
3847 constexpr uint8_t hdmi1 = 0;
3848 constexpr uint8_t hdmi2 = 1;
3849 const std::string SECONDARY_UNIQUE_ID = "local:1";
3850
3851 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3852 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3853
3854 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003855 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003856 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003857 ASSERT_FALSE(device2->isEnabled());
3858
3859 // Prepare second display.
Linnan Li13bf76a2024-05-05 19:18:02 +08003860 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Michael Wrighta9cf4192022-12-01 23:46:39 +00003861 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003862 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003863 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003864 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003865 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003866 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003867 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003868
3869 // Device should be enabled after the associated display is found.
3870 ASSERT_TRUE(mDevice->isEnabled());
3871 ASSERT_TRUE(device2->isEnabled());
3872
3873 // Test pad key events
3874 ASSERT_NO_FATAL_FAILURE(
3875 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3876 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3877 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3878 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3879 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3880 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3881 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3882
3883 ASSERT_NO_FATAL_FAILURE(
3884 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3885 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3886 AKEYCODE_DPAD_RIGHT, newDisplayId));
3887 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3888 AKEYCODE_DPAD_DOWN, newDisplayId));
3889 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3890 AKEYCODE_DPAD_LEFT, newDisplayId));
3891}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892
arthurhungc903df12020-08-11 15:08:42 +08003893TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3894 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3895 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3896 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3897 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3898 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3899 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3900
3901 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003902 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003903 // Initial metastate is AMETA_NONE.
3904 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003905
3906 // Initialization should have turned all of the lights off.
3907 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3908 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3909 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3910
3911 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003912 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3913 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003914 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3915 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3916
3917 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003920 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3921 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3922
3923 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003926 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3927 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3928
3929 mFakeEventHub->removeDevice(EVENTHUB_ID);
3930 mReader->loopOnce();
3931
3932 // keyboard 2 should default toggle keys.
3933 const std::string USB2 = "USB2";
3934 const std::string DEVICE_NAME2 = "KEYBOARD2";
3935 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3936 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3937 std::shared_ptr<InputDevice> device2 =
3938 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003939 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003940 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3941 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3942 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3943 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3944 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3945 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3946
Arpit Singh67ca6842023-04-26 14:43:16 +00003947 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003948 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003949 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3950 mFakePolicy
3951 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003952 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003953 std::list<NotifyArgs> unused =
3954 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003955 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003956 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003957
3958 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3959 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3960 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003961 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3962 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003963}
3964
Arthur Hungcb40a002021-08-03 14:31:01 +00003965TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3966 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3967 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3968 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3969
3970 // Suppose we have two mappers. (DPAD + KEYBOARD)
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003971 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD);
Arthur Hungcb40a002021-08-03 14:31:01 +00003972 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003973 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003974 // Initial metastate is AMETA_NONE.
3975 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003976
3977 mReader->toggleCapsLockState(DEVICE_ID);
3978 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3979}
3980
Arthur Hungfb3cc112022-04-13 07:39:50 +00003981TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3982 // keyboard 1.
3983 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3984 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3985 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3986 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3987 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3988 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3989
3990 KeyboardInputMapper& mapper1 =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003991 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003992
3993 // keyboard 2.
3994 const std::string USB2 = "USB2";
3995 const std::string DEVICE_NAME2 = "KEYBOARD2";
3996 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3997 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3998 std::shared_ptr<InputDevice> device2 =
3999 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4000 ftl::Flags<InputDeviceClass>(0));
4001 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4002 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4003 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4004 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4005 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4006 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4007
Arpit Singh67ca6842023-04-26 14:43:16 +00004008 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004009 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00004010 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
4011 mFakePolicy
4012 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004013 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004014 std::list<NotifyArgs> unused =
4015 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004016 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004017 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004018
Arthur Hung95f68612022-04-07 14:08:22 +08004019 // Initial metastate is AMETA_NONE.
4020 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4021 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4022
4023 // Toggle num lock on and off.
4024 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4025 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004026 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4027 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4028 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4029
4030 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4031 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4032 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4033 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4034 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4035
4036 // Toggle caps lock on and off.
4037 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4038 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4039 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4040 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4041 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4042
4043 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4044 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4045 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4046 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4047 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4048
4049 // Toggle scroll lock on and off.
4050 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4051 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4052 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4053 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4054 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4055
4056 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4057 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4058 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4059 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4060 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4061}
4062
Arthur Hung2141d542022-08-23 07:45:21 +00004063TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4064 const int32_t USAGE_A = 0x070004;
4065 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4066 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4067
4068 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004069 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung2141d542022-08-23 07:45:21 +00004070 // Key down by scan code.
4071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4072 NotifyKeyArgs args;
4073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4074 ASSERT_EQ(DEVICE_ID, args.deviceId);
4075 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4076 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4077 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4078 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4079 ASSERT_EQ(KEY_HOME, args.scanCode);
4080 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4081
4082 // Disable device, it should synthesize cancellation events for down events.
4083 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004084 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004085
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4087 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4088 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4089 ASSERT_EQ(KEY_HOME, args.scanCode);
4090 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4091}
4092
Zixuan Qufecb6062022-11-12 04:44:31 +00004093TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004094 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Zixuan Qufecb6062022-11-12 04:44:31 +00004095 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004096 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4097 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004098
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004099 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004100 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4101
4102 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004103 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004104
4105 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4106 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4107 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4108 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4109 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004110 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4111
4112 // Call change layout association with the same values: Generation shouldn't change
4113 generation = mReader->getContext()->getGeneration();
4114 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4115 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4116 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4117 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004118}
4119
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004120TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4121 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4122 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4123
4124 // Configuration
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004125 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004126 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004127 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004128
4129 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4130 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4131}
4132
Justin Chung71ddb432023-03-27 04:29:07 +00004133TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4135 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004136 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Justin Chung71ddb432023-03-27 04:29:07 +00004137 NotifyKeyArgs args;
4138
4139 // Key down
4140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4142 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4143}
4144
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004145// --- KeyboardInputMapperTest_ExternalAlphabeticDevice ---
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004146
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004147class KeyboardInputMapperTest_ExternalAlphabeticDevice : public InputMapperTest {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004148protected:
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004149 void SetUp() override {
4150 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
4151 InputDeviceClass::ALPHAKEY | InputDeviceClass::EXTERNAL);
4152 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004153};
4154
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004155// --- KeyboardInputMapperTest_ExternalNonAlphabeticDevice ---
4156
4157class KeyboardInputMapperTest_ExternalNonAlphabeticDevice : public InputMapperTest {
4158protected:
4159 void SetUp() override {
4160 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
4161 InputDeviceClass::EXTERNAL);
4162 }
4163};
4164
4165TEST_F(KeyboardInputMapperTest_ExternalAlphabeticDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004166 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4167 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004168
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004169 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4170 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4171 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4172 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004174 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004175 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Powei Fengd041c5d2019-05-03 17:11:33 -07004176
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004178 NotifyKeyArgs args;
4179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4180 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4181
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004182 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4184 ASSERT_EQ(uint32_t(0), args.policyFlags);
4185
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004188 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004189
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004190 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4192 ASSERT_EQ(uint32_t(0), args.policyFlags);
4193
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4196 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4197
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004198 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4200 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4201}
4202
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004203TEST_F(KeyboardInputMapperTest_ExternalNonAlphabeticDevice, WakeBehavior_NonAlphabeticKeyboard) {
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004204 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4205 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4206
4207 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4208 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4209 POLICY_FLAG_WAKE);
4210
4211 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004212 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004213
4214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4215 NotifyKeyArgs args;
4216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4217 ASSERT_EQ(uint32_t(0), args.policyFlags);
4218
4219 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4221 ASSERT_EQ(uint32_t(0), args.policyFlags);
4222
4223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4225 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4226
4227 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4229 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4230}
4231
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004232TEST_F(KeyboardInputMapperTest_ExternalAlphabeticDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004233 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004234
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004235 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4236 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4237 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004238
Powei Fengd041c5d2019-05-03 17:11:33 -07004239 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004240 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004241 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Powei Fengd041c5d2019-05-03 17:11:33 -07004242
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004243 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004244 NotifyKeyArgs args;
4245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4246 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4247
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004248 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4250 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4251
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4254 ASSERT_EQ(uint32_t(0), args.policyFlags);
4255
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004256 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4258 ASSERT_EQ(uint32_t(0), args.policyFlags);
4259
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4262 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4263
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004264 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4266 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4267}
4268
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269// --- TouchInputMapperTest ---
4270
4271class TouchInputMapperTest : public InputMapperTest {
4272protected:
4273 static const int32_t RAW_X_MIN;
4274 static const int32_t RAW_X_MAX;
4275 static const int32_t RAW_Y_MIN;
4276 static const int32_t RAW_Y_MAX;
4277 static const int32_t RAW_TOUCH_MIN;
4278 static const int32_t RAW_TOUCH_MAX;
4279 static const int32_t RAW_TOOL_MIN;
4280 static const int32_t RAW_TOOL_MAX;
4281 static const int32_t RAW_PRESSURE_MIN;
4282 static const int32_t RAW_PRESSURE_MAX;
4283 static const int32_t RAW_ORIENTATION_MIN;
4284 static const int32_t RAW_ORIENTATION_MAX;
4285 static const int32_t RAW_DISTANCE_MIN;
4286 static const int32_t RAW_DISTANCE_MAX;
4287 static const int32_t RAW_TILT_MIN;
4288 static const int32_t RAW_TILT_MAX;
4289 static const int32_t RAW_ID_MIN;
4290 static const int32_t RAW_ID_MAX;
4291 static const int32_t RAW_SLOT_MIN;
4292 static const int32_t RAW_SLOT_MAX;
4293 static const float X_PRECISION;
4294 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004295 static const float X_PRECISION_VIRTUAL;
4296 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297
4298 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004299 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300
4301 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4302
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004303 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004304 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004305
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 enum Axes {
4307 POSITION = 1 << 0,
4308 TOUCH = 1 << 1,
4309 TOOL = 1 << 2,
4310 PRESSURE = 1 << 3,
4311 ORIENTATION = 1 << 4,
4312 MINOR = 1 << 5,
4313 ID = 1 << 6,
4314 DISTANCE = 1 << 7,
4315 TILT = 1 << 8,
4316 SLOT = 1 << 9,
4317 TOOL_TYPE = 1 << 10,
4318 };
4319
Michael Wrighta9cf4192022-12-01 23:46:39 +00004320 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004321 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004322 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004324 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325 int32_t toRawX(float displayX);
4326 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004327 int32_t toRotatedRawX(float displayX);
4328 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004329 float toCookedX(float rawX, float rawY);
4330 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004331 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004332 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004333 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004334 float toDisplayY(int32_t rawY, int32_t displayHeight);
4335
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336};
4337
4338const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4339const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4340const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4341const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4342const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4343const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4344const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4345const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004346const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4347const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4349const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4350const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4351const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4352const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4353const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4354const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4355const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4356const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4357const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4358const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4359const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004360const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4361 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4362const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4363 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004364const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4365 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366
4367const float TouchInputMapperTest::GEOMETRIC_SCALE =
4368 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4369 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4370
4371const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4372 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4373 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4374};
4375
Michael Wrighta9cf4192022-12-01 23:46:39 +00004376void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004377 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4378 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004379}
4380
4381void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4382 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004383 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004384}
4385
Michael Wrighta9cf4192022-12-01 23:46:39 +00004386void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004387 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4388 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4389 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004390}
4391
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004393 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4394 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4395 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4396 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397}
4398
Jason Gerecke489fda82012-09-07 17:19:40 -07004399void TouchInputMapperTest::prepareLocationCalibration() {
4400 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4401}
4402
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403int32_t TouchInputMapperTest::toRawX(float displayX) {
4404 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4405}
4406
4407int32_t TouchInputMapperTest::toRawY(float displayY) {
4408 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4409}
4410
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004411int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4412 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4413}
4414
4415int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4416 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4417}
4418
Jason Gerecke489fda82012-09-07 17:19:40 -07004419float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4420 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4421 return rawX;
4422}
4423
4424float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4425 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4426 return rawY;
4427}
4428
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004430 return toDisplayX(rawX, DISPLAY_WIDTH);
4431}
4432
4433float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4434 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435}
4436
4437float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004438 return toDisplayY(rawY, DISPLAY_HEIGHT);
4439}
4440
4441float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4442 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004443}
4444
4445
4446// --- SingleTouchInputMapperTest ---
4447
4448class SingleTouchInputMapperTest : public TouchInputMapperTest {
4449protected:
4450 void prepareButtons();
4451 void prepareAxes(int axes);
4452
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004453 std::list<NotifyArgs> processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4454 std::list<NotifyArgs> processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4455 std::list<NotifyArgs> processUp(SingleTouchInputMapper& mappery);
4456 std::list<NotifyArgs> processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4457 std::list<NotifyArgs> processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4458 std::list<NotifyArgs> processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4459 std::list<NotifyArgs> processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4460 std::list<NotifyArgs> processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4461 std::list<NotifyArgs> processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462};
4463
4464void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004465 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466}
4467
4468void SingleTouchInputMapperTest::prepareAxes(int axes) {
4469 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004470 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4471 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472 }
4473 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004474 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4475 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 }
4477 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004478 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4479 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480 }
4481 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004482 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4483 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484 }
4485 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004486 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4487 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 }
4489}
4490
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004491std::list<NotifyArgs> SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper,
4492 int32_t x, int32_t y) {
4493 std::list<NotifyArgs> args;
4494 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4495 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4496 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
4497 return args;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004498}
4499
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004500std::list<NotifyArgs> SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper,
4501 int32_t x, int32_t y) {
4502 std::list<NotifyArgs> args;
4503 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4504 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
4505 return args;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506}
4507
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004508std::list<NotifyArgs> SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
4509 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004510}
4511
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004512std::list<NotifyArgs> SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper,
4513 int32_t pressure) {
4514 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515}
4516
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004517std::list<NotifyArgs> SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4518 int32_t toolMajor) {
4519 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520}
4521
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004522std::list<NotifyArgs> SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper,
4523 int32_t distance) {
4524 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525}
4526
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004527std::list<NotifyArgs> SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper,
4528 int32_t tiltX, int32_t tiltY) {
4529 std::list<NotifyArgs> args;
4530 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4531 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
4532 return args;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004533}
4534
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004535std::list<NotifyArgs> SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper,
4536 int32_t code, int32_t value) {
4537 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538}
4539
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004540std::list<NotifyArgs> SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
4541 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542}
4543
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 prepareButtons();
4546 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004547 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004548
Josep del Río2d8c79a2023-01-23 19:33:50 +00004549 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550}
4551
Michael Wrightd02c5b62014-02-10 15:10:22 -08004552TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 prepareButtons();
4554 prepareAxes(POSITION);
4555 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004556 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004558 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559}
4560
4561TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004563 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564 prepareButtons();
4565 prepareAxes(POSITION);
4566 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004567 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004568
4569 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004570 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571
4572 // Virtual key is down.
4573 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4574 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4575 processDown(mapper, x, y);
4576 processSync(mapper);
4577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4578
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004579 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580
4581 // Virtual key is up.
4582 processUp(mapper);
4583 processSync(mapper);
4584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4585
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004586 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587}
4588
4589TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004591 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004592 prepareButtons();
4593 prepareAxes(POSITION);
4594 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004595 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596
4597 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004598 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599
4600 // Virtual key is down.
4601 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4602 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4603 processDown(mapper, x, y);
4604 processSync(mapper);
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4606
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004607 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608
4609 // Virtual key is up.
4610 processUp(mapper);
4611 processSync(mapper);
4612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4613
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004614 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615}
4616
4617TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004618 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004619 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 prepareButtons();
4621 prepareAxes(POSITION);
4622 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004623 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004624
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004626 ASSERT_TRUE(
4627 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004628 ASSERT_TRUE(flags[0]);
4629 ASSERT_FALSE(flags[1]);
4630}
4631
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004632TEST_F(SingleTouchInputMapperTest, DeviceTypeChange_RecalculatesRawToDisplayTransform) {
4633 prepareDisplay(ui::ROTATION_0);
4634 prepareAxes(POSITION);
4635 addConfigurationProperty("touch.deviceType", "touchScreen");
4636 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
4637
4638 const int32_t x = 900;
4639 const int32_t y = 75;
4640 std::list<NotifyArgs> args;
4641 args += processDown(mapper, x, y);
4642 args += processSync(mapper);
4643
4644 // Assert that motion event is received in display coordinate space for deviceType touchScreen.
4645 ASSERT_THAT(args,
4646 ElementsAre(VariantWith<NotifyMotionArgs>(
4647 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4648 WithCoords(toDisplayX(x), toDisplayY(y))))));
4649
4650 // Add device type association after the device was created.
4651 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
4652 // Send update to the mapper.
4653 std::list<NotifyArgs> unused =
4654 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4655 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
4656
4657 args.clear();
4658 args += processDown(mapper, x, y);
4659 args += processSync(mapper);
4660
4661 // Assert that motion event is received in raw coordinate space for deviceType touchNavigation.
4662 ASSERT_THAT(args,
4663 ElementsAre(VariantWith<NotifyMotionArgs>(
4664 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4665 WithCoords(x - RAW_X_MIN, y - RAW_Y_MIN)))));
4666}
4667
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004670 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004671 prepareButtons();
4672 prepareAxes(POSITION);
4673 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004674 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675
arthurhungdcef2dc2020-08-11 14:47:50 +08004676 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004677
4678 NotifyKeyArgs args;
4679
4680 // Press virtual key.
4681 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4682 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4683 processDown(mapper, x, y);
4684 processSync(mapper);
4685
4686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4687 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4688 ASSERT_EQ(DEVICE_ID, args.deviceId);
4689 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4690 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4691 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4692 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4693 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4694 ASSERT_EQ(KEY_HOME, args.scanCode);
4695 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4696 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4697
4698 // Release virtual key.
4699 processUp(mapper);
4700 processSync(mapper);
4701
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4703 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4704 ASSERT_EQ(DEVICE_ID, args.deviceId);
4705 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4706 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4707 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4708 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4709 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4710 ASSERT_EQ(KEY_HOME, args.scanCode);
4711 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4712 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4713
4714 // Should not have sent any motions.
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4716}
4717
4718TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004720 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721 prepareButtons();
4722 prepareAxes(POSITION);
4723 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004724 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725
arthurhungdcef2dc2020-08-11 14:47:50 +08004726 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004727
4728 NotifyKeyArgs keyArgs;
4729
4730 // Press virtual key.
4731 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4732 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4733 processDown(mapper, x, y);
4734 processSync(mapper);
4735
4736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4737 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4738 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4739 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4740 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4741 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4742 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4743 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4744 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4746 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4747
4748 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4749 // into the display area.
4750 y -= 100;
4751 processMove(mapper, x, y);
4752 processSync(mapper);
4753
4754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4755 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4756 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4757 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4758 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4759 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4760 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4761 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4762 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4763 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4764 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4765 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4766
4767 NotifyMotionArgs motionArgs;
4768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4769 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4770 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4771 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4772 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4773 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4774 ASSERT_EQ(0, motionArgs.flags);
4775 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4776 ASSERT_EQ(0, motionArgs.buttonState);
4777 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004778 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004780 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4782 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4783 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4784 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4785 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4786
4787 // Keep moving out of bounds. Should generate a pointer move.
4788 y -= 50;
4789 processMove(mapper, x, y);
4790 processSync(mapper);
4791
4792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4793 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4794 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4795 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4796 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4798 ASSERT_EQ(0, motionArgs.flags);
4799 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4800 ASSERT_EQ(0, motionArgs.buttonState);
4801 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004802 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004804 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4806 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4807 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4808 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4809 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4810
4811 // Release out of bounds. Should generate a pointer up.
4812 processUp(mapper);
4813 processSync(mapper);
4814
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4817 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4818 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4819 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4820 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4821 ASSERT_EQ(0, motionArgs.flags);
4822 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4823 ASSERT_EQ(0, motionArgs.buttonState);
4824 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004825 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004827 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4829 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4830 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4831 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4832 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4833
4834 // Should not have sent any more keys or motions.
4835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4837}
4838
4839TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004840 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004841 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842 prepareButtons();
4843 prepareAxes(POSITION);
4844 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004845 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846
arthurhungdcef2dc2020-08-11 14:47:50 +08004847 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848
4849 NotifyMotionArgs motionArgs;
4850
4851 // Initially go down out of bounds.
4852 int32_t x = -10;
4853 int32_t y = -10;
4854 processDown(mapper, x, y);
4855 processSync(mapper);
4856
4857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4858
4859 // Move into the display area. Should generate a pointer down.
4860 x = 50;
4861 y = 75;
4862 processMove(mapper, x, y);
4863 processSync(mapper);
4864
4865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4866 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4867 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4868 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4869 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4870 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4871 ASSERT_EQ(0, motionArgs.flags);
4872 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4873 ASSERT_EQ(0, motionArgs.buttonState);
4874 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004875 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004876 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004877 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4879 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4880 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4881 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4882 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4883
4884 // Release. Should generate a pointer up.
4885 processUp(mapper);
4886 processSync(mapper);
4887
4888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4889 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4890 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4891 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4892 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4893 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4894 ASSERT_EQ(0, motionArgs.flags);
4895 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4896 ASSERT_EQ(0, motionArgs.buttonState);
4897 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004898 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004899 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004900 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4902 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4903 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4904 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4906
4907 // Should not have sent any more keys or motions.
4908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4910}
4911
Santos Cordonfa5cf462017-04-05 10:37:00 -07004912TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004913 addConfigurationProperty("touch.deviceType", "touchScreen");
4914 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4915
Michael Wrighta9cf4192022-12-01 23:46:39 +00004916 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004917 prepareButtons();
4918 prepareAxes(POSITION);
4919 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004920 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004921
arthurhungdcef2dc2020-08-11 14:47:50 +08004922 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004923
4924 NotifyMotionArgs motionArgs;
4925
4926 // Down.
4927 int32_t x = 100;
4928 int32_t y = 125;
4929 processDown(mapper, x, y);
4930 processSync(mapper);
4931
4932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4933 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4934 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4935 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4936 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4937 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4938 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4939 ASSERT_EQ(0, motionArgs.flags);
4940 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4941 ASSERT_EQ(0, motionArgs.buttonState);
4942 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004943 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004944 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004945 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4947 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4948 1, 0, 0, 0, 0, 0, 0, 0));
4949 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4950 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4951 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4952
4953 // Move.
4954 x += 50;
4955 y += 75;
4956 processMove(mapper, x, y);
4957 processSync(mapper);
4958
4959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4960 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4961 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4962 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4963 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4964 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4965 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4966 ASSERT_EQ(0, motionArgs.flags);
4967 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4968 ASSERT_EQ(0, motionArgs.buttonState);
4969 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004970 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004971 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004972 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4974 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4975 1, 0, 0, 0, 0, 0, 0, 0));
4976 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4977 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4978 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4979
4980 // Up.
4981 processUp(mapper);
4982 processSync(mapper);
4983
4984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4985 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4986 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4987 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4988 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4989 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4990 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4991 ASSERT_EQ(0, motionArgs.flags);
4992 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4993 ASSERT_EQ(0, motionArgs.buttonState);
4994 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004995 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004996 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004997 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4999 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5000 1, 0, 0, 0, 0, 0, 0, 0));
5001 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5002 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5003 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5004
5005 // Should not have sent any more keys or motions.
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5008}
5009
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005012 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013 prepareButtons();
5014 prepareAxes(POSITION);
5015 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005016 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017
arthurhungdcef2dc2020-08-11 14:47:50 +08005018 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005019
5020 NotifyMotionArgs motionArgs;
5021
5022 // Down.
5023 int32_t x = 100;
5024 int32_t y = 125;
5025 processDown(mapper, x, y);
5026 processSync(mapper);
5027
5028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5029 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5030 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5031 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5032 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5034 ASSERT_EQ(0, motionArgs.flags);
5035 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5036 ASSERT_EQ(0, motionArgs.buttonState);
5037 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005038 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005040 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5042 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5043 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5044 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5045 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5046
5047 // Move.
5048 x += 50;
5049 y += 75;
5050 processMove(mapper, x, y);
5051 processSync(mapper);
5052
5053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5054 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5055 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5056 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5057 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5059 ASSERT_EQ(0, motionArgs.flags);
5060 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5061 ASSERT_EQ(0, motionArgs.buttonState);
5062 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005063 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005065 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5067 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5068 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5069 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5070 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5071
5072 // Up.
5073 processUp(mapper);
5074 processSync(mapper);
5075
5076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5077 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5078 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5079 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5080 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5081 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5082 ASSERT_EQ(0, motionArgs.flags);
5083 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5084 ASSERT_EQ(0, motionArgs.buttonState);
5085 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005086 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005088 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5090 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5091 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5092 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5093 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5094
5095 // Should not have sent any more keys or motions.
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5098}
5099
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005100TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005101 addConfigurationProperty("touch.deviceType", "touchScreen");
5102 prepareButtons();
5103 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005104 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5105 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005106 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107
5108 NotifyMotionArgs args;
5109
5110 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005111 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005112 processDown(mapper, toRawX(50), toRawY(75));
5113 processSync(mapper);
5114
5115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5116 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5117 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5118
5119 processUp(mapper);
5120 processSync(mapper);
5121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5122}
5123
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005124TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125 addConfigurationProperty("touch.deviceType", "touchScreen");
5126 prepareButtons();
5127 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005128 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5129 // orientation-aware are affected by display rotation.
5130 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005131 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005132
5133 NotifyMotionArgs args;
5134
5135 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005136 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005137 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005138 processDown(mapper, toRawX(50), toRawY(75));
5139 processSync(mapper);
5140
5141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5142 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5143 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5144
5145 processUp(mapper);
5146 processSync(mapper);
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5148
5149 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005150 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005151 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005152 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005153 processSync(mapper);
5154
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5156 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5157 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5158
5159 processUp(mapper);
5160 processSync(mapper);
5161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5162
5163 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005164 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005165 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005166 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5167 processSync(mapper);
5168
5169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5170 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5171 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5172
5173 processUp(mapper);
5174 processSync(mapper);
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5176
5177 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005178 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005179 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005180 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181 processSync(mapper);
5182
5183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5184 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5185 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5186
5187 processUp(mapper);
5188 processSync(mapper);
5189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5190}
5191
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005192TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5193 addConfigurationProperty("touch.deviceType", "touchScreen");
5194 prepareButtons();
5195 prepareAxes(POSITION);
5196 addConfigurationProperty("touch.orientationAware", "1");
5197 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5198 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005199 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005200 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005201 NotifyMotionArgs args;
5202
5203 // Orientation 0.
5204 processDown(mapper, toRawX(50), toRawY(75));
5205 processSync(mapper);
5206
5207 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5208 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5209 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5210
5211 processUp(mapper);
5212 processSync(mapper);
5213 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5214}
5215
5216TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5217 addConfigurationProperty("touch.deviceType", "touchScreen");
5218 prepareButtons();
5219 prepareAxes(POSITION);
5220 addConfigurationProperty("touch.orientationAware", "1");
5221 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5222 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005223 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005224 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005225 NotifyMotionArgs args;
5226
5227 // Orientation 90.
5228 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5229 processSync(mapper);
5230
5231 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5232 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5233 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5234
5235 processUp(mapper);
5236 processSync(mapper);
5237 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5238}
5239
5240TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5241 addConfigurationProperty("touch.deviceType", "touchScreen");
5242 prepareButtons();
5243 prepareAxes(POSITION);
5244 addConfigurationProperty("touch.orientationAware", "1");
5245 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5246 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005247 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005248 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005249 NotifyMotionArgs args;
5250
5251 // Orientation 180.
5252 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5253 processSync(mapper);
5254
5255 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5256 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5257 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5258
5259 processUp(mapper);
5260 processSync(mapper);
5261 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5262}
5263
5264TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5265 addConfigurationProperty("touch.deviceType", "touchScreen");
5266 prepareButtons();
5267 prepareAxes(POSITION);
5268 addConfigurationProperty("touch.orientationAware", "1");
5269 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5270 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005271 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005272 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005273 NotifyMotionArgs args;
5274
5275 // Orientation 270.
5276 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5277 processSync(mapper);
5278
5279 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5280 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5281 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5282
5283 processUp(mapper);
5284 processSync(mapper);
5285 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5286}
5287
5288TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5289 addConfigurationProperty("touch.deviceType", "touchScreen");
5290 prepareButtons();
5291 prepareAxes(POSITION);
5292 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5293 // orientation-aware are affected by display rotation.
5294 addConfigurationProperty("touch.orientationAware", "0");
5295 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005296 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005297
5298 NotifyMotionArgs args;
5299
5300 // Orientation 90, Rotation 0.
5301 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005302 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005303 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5304 processSync(mapper);
5305
5306 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5307 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5308 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5309
5310 processUp(mapper);
5311 processSync(mapper);
5312 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5313
5314 // Orientation 90, Rotation 90.
5315 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005316 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005317 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005318 processSync(mapper);
5319
5320 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5321 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5322 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5323
5324 processUp(mapper);
5325 processSync(mapper);
5326 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5327
5328 // Orientation 90, Rotation 180.
5329 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005330 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005331 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5332 processSync(mapper);
5333
5334 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5335 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5336 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5337
5338 processUp(mapper);
5339 processSync(mapper);
5340 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5341
5342 // Orientation 90, Rotation 270.
5343 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005344 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005345 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 -07005346 processSync(mapper);
5347
5348 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5349 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5350 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5351
5352 processUp(mapper);
5353 processSync(mapper);
5354 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5355}
5356
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005357TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5358 addConfigurationProperty("touch.deviceType", "touchScreen");
5359 prepareButtons();
5360 prepareAxes(POSITION);
5361 addConfigurationProperty("touch.orientationAware", "1");
5362 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005363 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005364
5365 // Set a physical frame in the display viewport.
5366 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5367 viewport->physicalLeft = 20;
5368 viewport->physicalTop = 600;
5369 viewport->physicalRight = 30;
5370 viewport->physicalBottom = 610;
5371 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005372 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005373
5374 // Start the touch.
5375 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5376 processSync(mapper);
5377
5378 // Expect all input starting outside the physical frame to be ignored.
5379 const std::array<Point, 6> outsidePoints = {
5380 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5381 for (const auto& p : outsidePoints) {
5382 processMove(mapper, toRawX(p.x), toRawY(p.y));
5383 processSync(mapper);
5384 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5385 }
5386
5387 // Move the touch into the physical frame.
5388 processMove(mapper, toRawX(25), toRawY(605));
5389 processSync(mapper);
5390 NotifyMotionArgs args;
5391 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5392 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5393 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5394 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5395
5396 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5397 for (const auto& p : outsidePoints) {
5398 processMove(mapper, toRawX(p.x), toRawY(p.y));
5399 processSync(mapper);
5400 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5401 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5402 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5403 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5404 }
5405
5406 processUp(mapper);
5407 processSync(mapper);
5408 EXPECT_NO_FATAL_FAILURE(
5409 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5410}
5411
Harry Cutts1db43992023-06-19 17:05:07 +00005412TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
Harry Cutts1db43992023-06-19 17:05:07 +00005413 addConfigurationProperty("touch.deviceType", "pointer");
5414 prepareAxes(POSITION);
5415 prepareDisplay(ui::ROTATION_0);
5416 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5417
5418 // Set a physical frame in the display viewport.
5419 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5420 viewport->physicalLeft = 20;
5421 viewport->physicalTop = 600;
5422 viewport->physicalRight = 30;
5423 viewport->physicalBottom = 610;
5424 mFakePolicy->updateViewport(*viewport);
5425 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5426
5427 // Start the touch.
5428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5429 processSync(mapper);
5430
5431 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5432 // produced.
5433 const std::array<Point, 6> outsidePoints = {
5434 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5435 for (const auto& p : outsidePoints) {
5436 processMove(mapper, toRawX(p.x), toRawY(p.y));
5437 processSync(mapper);
5438 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5439 }
5440}
5441
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005444 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445 prepareButtons();
5446 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005447 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448
5449 // These calculations are based on the input device calibration documentation.
5450 int32_t rawX = 100;
5451 int32_t rawY = 200;
5452 int32_t rawPressure = 10;
5453 int32_t rawToolMajor = 12;
5454 int32_t rawDistance = 2;
5455 int32_t rawTiltX = 30;
5456 int32_t rawTiltY = 110;
5457
5458 float x = toDisplayX(rawX);
5459 float y = toDisplayY(rawY);
5460 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5461 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5462 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5463 float distance = float(rawDistance);
5464
5465 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5466 float tiltScale = M_PI / 180;
5467 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5468 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5469 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5470 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5471
5472 processDown(mapper, rawX, rawY);
5473 processPressure(mapper, rawPressure);
5474 processToolMajor(mapper, rawToolMajor);
5475 processDistance(mapper, rawDistance);
5476 processTilt(mapper, rawTiltX, rawTiltY);
5477 processSync(mapper);
5478
5479 NotifyMotionArgs args;
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5482 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5483 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
Prabir Pradhan9a53b552024-06-04 02:59:40 +00005484 ASSERT_EQ(args.flags,
5485 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION |
5486 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487}
5488
Jason Gerecke489fda82012-09-07 17:19:40 -07005489TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005490 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005491 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005492 prepareLocationCalibration();
5493 prepareButtons();
5494 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005495 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005496
5497 int32_t rawX = 100;
5498 int32_t rawY = 200;
5499
5500 float x = toDisplayX(toCookedX(rawX, rawY));
5501 float y = toDisplayY(toCookedY(rawX, rawY));
5502
5503 processDown(mapper, rawX, rawY);
5504 processSync(mapper);
5505
5506 NotifyMotionArgs args;
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5509 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5510}
5511
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005513 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005514 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005515 prepareButtons();
5516 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005517 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518
5519 NotifyMotionArgs motionArgs;
5520 NotifyKeyArgs keyArgs;
5521
5522 processDown(mapper, 100, 200);
5523 processSync(mapper);
5524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5525 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5526 ASSERT_EQ(0, motionArgs.buttonState);
5527
5528 // press BTN_LEFT, release BTN_LEFT
5529 processKey(mapper, BTN_LEFT, 1);
5530 processSync(mapper);
5531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5533 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5534
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5536 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5537 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5538
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 processKey(mapper, BTN_LEFT, 0);
5540 processSync(mapper);
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005542 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005547 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548
5549 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5550 processKey(mapper, BTN_RIGHT, 1);
5551 processKey(mapper, BTN_MIDDLE, 1);
5552 processSync(mapper);
5553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5554 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5555 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5556 motionArgs.buttonState);
5557
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5560 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5561
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5563 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5564 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5565 motionArgs.buttonState);
5566
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567 processKey(mapper, BTN_RIGHT, 0);
5568 processSync(mapper);
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005570 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005572
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005575 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576
5577 processKey(mapper, BTN_MIDDLE, 0);
5578 processSync(mapper);
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005580 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005581 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005582
5583 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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586
5587 // press BTN_BACK, release BTN_BACK
5588 processKey(mapper, BTN_BACK, 1);
5589 processSync(mapper);
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5591 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5592 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005593
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005596 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5597
5598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5599 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5600 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005601
5602 processKey(mapper, BTN_BACK, 0);
5603 processSync(mapper);
5604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005605 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005607
5608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005610 ASSERT_EQ(0, motionArgs.buttonState);
5611
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5613 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5614 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5615
5616 // press BTN_SIDE, release BTN_SIDE
5617 processKey(mapper, BTN_SIDE, 1);
5618 processSync(mapper);
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5620 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5621 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005622
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005625 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5626
5627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5628 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5629 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005630
5631 processKey(mapper, BTN_SIDE, 0);
5632 processSync(mapper);
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005634 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005639 ASSERT_EQ(0, motionArgs.buttonState);
5640
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5642 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5643 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5644
5645 // press BTN_FORWARD, release BTN_FORWARD
5646 processKey(mapper, BTN_FORWARD, 1);
5647 processSync(mapper);
5648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5649 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5650 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005651
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005653 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005654 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5655
5656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5657 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5658 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005659
5660 processKey(mapper, BTN_FORWARD, 0);
5661 processSync(mapper);
5662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005665
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005668 ASSERT_EQ(0, motionArgs.buttonState);
5669
Michael Wrightd02c5b62014-02-10 15:10:22 -08005670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5671 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5672 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5673
5674 // press BTN_EXTRA, release BTN_EXTRA
5675 processKey(mapper, BTN_EXTRA, 1);
5676 processSync(mapper);
5677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5678 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5679 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005680
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005683 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5684
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5686 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5687 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005688
5689 processKey(mapper, BTN_EXTRA, 0);
5690 processSync(mapper);
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005692 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005694
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005697 ASSERT_EQ(0, motionArgs.buttonState);
5698
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5700 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5701 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5702
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5704
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 // press BTN_STYLUS, release BTN_STYLUS
5706 processKey(mapper, BTN_STYLUS, 1);
5707 processSync(mapper);
5708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5709 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005710 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5711
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5713 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5714 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715
5716 processKey(mapper, BTN_STYLUS, 0);
5717 processSync(mapper);
5718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005719 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005721
5722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005724 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725
5726 // press BTN_STYLUS2, release BTN_STYLUS2
5727 processKey(mapper, BTN_STYLUS2, 1);
5728 processSync(mapper);
5729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005731 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5732
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5735 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736
5737 processKey(mapper, BTN_STYLUS2, 0);
5738 processSync(mapper);
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005740 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005742
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005744 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005745 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746
5747 // release touch
5748 processUp(mapper);
5749 processSync(mapper);
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5751 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5752 ASSERT_EQ(0, motionArgs.buttonState);
5753}
5754
5755TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005756 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005757 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758 prepareButtons();
5759 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005760 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005761
5762 NotifyMotionArgs motionArgs;
5763
5764 // default tool type is finger
5765 processDown(mapper, 100, 200);
5766 processSync(mapper);
5767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5768 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005769 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770
5771 // eraser
5772 processKey(mapper, BTN_TOOL_RUBBER, 1);
5773 processSync(mapper);
5774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005776 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005777
5778 // stylus
5779 processKey(mapper, BTN_TOOL_RUBBER, 0);
5780 processKey(mapper, BTN_TOOL_PEN, 1);
5781 processSync(mapper);
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005784 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005785
5786 // brush
5787 processKey(mapper, BTN_TOOL_PEN, 0);
5788 processKey(mapper, BTN_TOOL_BRUSH, 1);
5789 processSync(mapper);
5790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005792 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005793
5794 // pencil
5795 processKey(mapper, BTN_TOOL_BRUSH, 0);
5796 processKey(mapper, BTN_TOOL_PENCIL, 1);
5797 processSync(mapper);
5798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005800 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005801
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005802 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005803 processKey(mapper, BTN_TOOL_PENCIL, 0);
5804 processKey(mapper, BTN_TOOL_AIRBRUSH, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005809
5810 // mouse
5811 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5812 processKey(mapper, BTN_TOOL_MOUSE, 1);
5813 processSync(mapper);
5814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005816 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817
5818 // lens
5819 processKey(mapper, BTN_TOOL_MOUSE, 0);
5820 processKey(mapper, BTN_TOOL_LENS, 1);
5821 processSync(mapper);
5822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005824 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005825
5826 // double-tap
5827 processKey(mapper, BTN_TOOL_LENS, 0);
5828 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5829 processSync(mapper);
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005832 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005833
5834 // triple-tap
5835 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5836 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5837 processSync(mapper);
5838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005840 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005841
5842 // quad-tap
5843 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5844 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5845 processSync(mapper);
5846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005848 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005849
5850 // finger
5851 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5852 processKey(mapper, BTN_TOOL_FINGER, 1);
5853 processSync(mapper);
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005856 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005857
5858 // stylus trumps finger
5859 processKey(mapper, BTN_TOOL_PEN, 1);
5860 processSync(mapper);
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005863 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005864
5865 // eraser trumps stylus
5866 processKey(mapper, BTN_TOOL_RUBBER, 1);
5867 processSync(mapper);
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005870 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871
5872 // mouse trumps eraser
5873 processKey(mapper, BTN_TOOL_MOUSE, 1);
5874 processSync(mapper);
5875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005877 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005878
5879 // back to default tool type
5880 processKey(mapper, BTN_TOOL_MOUSE, 0);
5881 processKey(mapper, BTN_TOOL_RUBBER, 0);
5882 processKey(mapper, BTN_TOOL_PEN, 0);
5883 processKey(mapper, BTN_TOOL_FINGER, 0);
5884 processSync(mapper);
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005887 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888}
5889
5890TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005892 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893 prepareButtons();
5894 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005895 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005896 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897
5898 NotifyMotionArgs motionArgs;
5899
5900 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5901 processKey(mapper, BTN_TOOL_FINGER, 1);
5902 processMove(mapper, 100, 200);
5903 processSync(mapper);
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5905 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5907 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5908
5909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5910 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5912 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5913
5914 // move a little
5915 processMove(mapper, 150, 250);
5916 processSync(mapper);
5917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5918 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5920 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5921
5922 // down when BTN_TOUCH is pressed, pressure defaults to 1
5923 processKey(mapper, BTN_TOUCH, 1);
5924 processSync(mapper);
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5928 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5929
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5933 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5934
5935 // up when BTN_TOUCH is released, hover restored
5936 processKey(mapper, BTN_TOUCH, 0);
5937 processSync(mapper);
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5941 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5942
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5946 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5947
5948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5949 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5951 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5952
5953 // exit hover when pointer goes away
5954 processKey(mapper, BTN_TOOL_FINGER, 0);
5955 processSync(mapper);
5956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5959 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5960}
5961
5962TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005964 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 prepareButtons();
5966 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005967 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005968
5969 NotifyMotionArgs motionArgs;
5970
5971 // initially hovering because pressure is 0
5972 processDown(mapper, 100, 200);
5973 processPressure(mapper, 0);
5974 processSync(mapper);
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5976 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5978 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5979
5980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5981 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5983 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5984
5985 // move a little
5986 processMove(mapper, 150, 250);
5987 processSync(mapper);
5988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5989 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5991 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5992
5993 // down when pressure is non-zero
5994 processPressure(mapper, RAW_PRESSURE_MAX);
5995 processSync(mapper);
5996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5999 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6000
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6002 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6004 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6005
6006 // up when pressure becomes 0, hover restored
6007 processPressure(mapper, 0);
6008 processSync(mapper);
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6012 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6013
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6015 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6017 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6022 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6023
6024 // exit hover when pointer goes away
6025 processUp(mapper);
6026 processSync(mapper);
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6030 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6031}
6032
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006033TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6034 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006035 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006036 prepareButtons();
6037 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006038 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006039
6040 // Touch down.
6041 processDown(mapper, 100, 200);
6042 processPressure(mapper, 1);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6045 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6046
6047 // Reset the mapper. This should cancel the ongoing gesture.
6048 resetMapper(mapper, ARBITRARY_TIME);
6049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6050 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6051
6052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6053}
6054
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006055TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6056 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006057 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006058 prepareButtons();
6059 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006060 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006061
6062 // Set the initial state for the touch pointer.
6063 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6064 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6065 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6066 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6067
6068 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006069 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6070 // does not generate any events.
6071 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006072
6073 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6074 // the recreated touch state to generate a down event.
6075 processSync(mapper);
6076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6077 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6078
6079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6080}
6081
lilinnan687e58f2022-07-19 16:00:50 +08006082TEST_F(SingleTouchInputMapperTest,
6083 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6084 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006085 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006086 prepareButtons();
6087 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006088 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006089 NotifyMotionArgs motionArgs;
6090
6091 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006092 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006093 processSync(mapper);
6094
6095 // We should receive a down event
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6097 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6098
6099 // Change display id
6100 clearViewports();
6101 prepareSecondaryDisplay(ViewportType::INTERNAL);
6102
6103 // We should receive a cancel event
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6105 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6106 // Then receive reset called
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6108}
6109
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006110TEST_F(SingleTouchInputMapperTest,
6111 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6112 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006113 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006114 prepareButtons();
6115 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006116 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6118 NotifyMotionArgs motionArgs;
6119
6120 // Start a new gesture.
6121 processDown(mapper, 100, 200);
6122 processSync(mapper);
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6124 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6125
6126 // Make the viewport inactive. This will put the device in disabled mode.
6127 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6128 viewport->isActive = false;
6129 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006130 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006131
6132 // We should receive a cancel event for the ongoing gesture.
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6134 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6135 // Then we should be notified that the device was reset.
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6137
6138 // No events are generated while the viewport is inactive.
6139 processMove(mapper, 101, 201);
6140 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006141 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006142 processSync(mapper);
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6144
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006145 // Start a new gesture while the viewport is still inactive.
6146 processDown(mapper, 300, 400);
6147 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6148 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6149 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6150 processSync(mapper);
6151
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006152 // Make the viewport active again. The device should resume processing events.
6153 viewport->isActive = true;
6154 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006155 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006156
6157 // The device is reset because it changes back to direct mode, without generating any events.
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6160
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006161 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006162 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6164 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006165
6166 // No more events.
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6169}
6170
Prabir Pradhan211ba622022-10-31 21:09:21 +00006171TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6172 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006173 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006174 prepareButtons();
6175 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006176 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6178
6179 // Press a stylus button.
6180 processKey(mapper, BTN_STYLUS, 1);
6181 processSync(mapper);
6182
6183 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6184 processDown(mapper, 100, 200);
6185 processSync(mapper);
6186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6187 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6188 WithCoords(toDisplayX(100), toDisplayY(200)),
6189 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6191 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6192 WithCoords(toDisplayX(100), toDisplayY(200)),
6193 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6194
6195 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6196 // the button has not actually been released, since there will be no pointers through which the
6197 // button state can be reported. The event is generated at the location of the pointer before
6198 // it went up.
6199 processUp(mapper);
6200 processSync(mapper);
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6202 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6203 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6205 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6206 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6207}
6208
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006209TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6210 addConfigurationProperty("touch.deviceType", "touchScreen");
6211 prepareDisplay(ui::ROTATION_0);
6212 prepareButtons();
6213 prepareAxes(POSITION);
6214
6215 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6216
Arpit Singha8c236b2023-04-25 13:56:05 +00006217 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6219
6220 // Press a stylus button.
6221 processKey(mapper, BTN_STYLUS, 1);
6222 processSync(mapper);
6223
6224 // Start a touch gesture and ensure that the stylus button is not reported.
6225 processDown(mapper, 100, 200);
6226 processSync(mapper);
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6228 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6229
6230 // Release and press the stylus button again.
6231 processKey(mapper, BTN_STYLUS, 0);
6232 processSync(mapper);
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6234 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6235 processKey(mapper, BTN_STYLUS, 1);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6238 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6239
6240 // Release the touch gesture.
6241 processUp(mapper);
6242 processSync(mapper);
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6244 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6245
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6247}
6248
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006249TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6250 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6251 prepareDisplay(ui::ROTATION_0);
6252 prepareButtons();
6253 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006254 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6256
Prabir Pradhan9cf8f322024-06-18 20:22:42 +00006257 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006258}
6259
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006260TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6261 // Initialize the device without setting device source to touch navigation.
6262 addConfigurationProperty("touch.deviceType", "touchScreen");
6263 prepareDisplay(ui::ROTATION_0);
6264 prepareButtons();
6265 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006266 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006267
6268 // Ensure that the device is created as a touchscreen, not touch navigation.
6269 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6270
6271 // Add device type association after the device was created.
6272 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6273
6274 // Send update to the mapper.
6275 std::list<NotifyArgs> unused2 =
6276 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006277 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006278
6279 // Check whether device type update was successful.
Prabir Pradhan9cf8f322024-06-18 20:22:42 +00006280 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION | AINPUT_SOURCE_TOUCHPAD, mDevice->getSources());
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006281}
6282
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006283TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6284 // Initialize the device without setting device source to touch navigation.
6285 addConfigurationProperty("touch.deviceType", "touchScreen");
6286 prepareDisplay(ui::ROTATION_0);
6287 prepareButtons();
6288 prepareAxes(POSITION);
6289 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6290
6291 // Set a physical frame in the display viewport.
6292 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6293 viewport->physicalLeft = 0;
6294 viewport->physicalTop = 0;
6295 viewport->physicalRight = DISPLAY_WIDTH / 2;
6296 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6297 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006298 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006299
Arpit Singha8c236b2023-04-25 13:56:05 +00006300 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006301
6302 // Hovering inside the physical frame produces events.
6303 processKey(mapper, BTN_TOOL_PEN, 1);
6304 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6307 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6309 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6310
6311 // Leaving the physical frame ends the hovering gesture.
6312 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6313 processSync(mapper);
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6315 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6316
6317 // Moving outside the physical frame does not produce events.
6318 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6319 processSync(mapper);
6320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6321
6322 // Re-entering the physical frame produces events.
6323 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6326 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6328 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6329}
6330
Prabir Pradhan5632d622021-09-06 07:57:20 -07006331// --- TouchDisplayProjectionTest ---
6332
6333class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6334public:
6335 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6336 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6337 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006338 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6339 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6340 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006341 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006342 auto rotatedWidth = naturalDisplayWidth;
6343 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006344 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006345 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006346 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006347 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006348 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006349 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006350 inverseRotationFlags = ui::Transform::ROT_180;
6351 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006352 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006353 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006354 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006355 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006356 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006357 inverseRotationFlags = ui::Transform::ROT_0;
6358 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006359 }
6360
Prabir Pradhana9df3162022-12-05 23:57:27 +00006361 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006362 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6363
6364 std::optional<DisplayViewport> internalViewport =
6365 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6366 DisplayViewport& v = *internalViewport;
6367 v.displayId = DISPLAY_ID;
6368 v.orientation = orientation;
6369
6370 v.logicalLeft = 0;
6371 v.logicalTop = 0;
6372 v.logicalRight = 100;
6373 v.logicalBottom = 100;
6374
6375 v.physicalLeft = rotatedPhysicalDisplay.left;
6376 v.physicalTop = rotatedPhysicalDisplay.top;
6377 v.physicalRight = rotatedPhysicalDisplay.right;
6378 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6379
Prabir Pradhana9df3162022-12-05 23:57:27 +00006380 v.deviceWidth = rotatedWidth;
6381 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006382
6383 v.isActive = true;
6384 v.uniqueId = UNIQUE_ID;
6385 v.type = ViewportType::INTERNAL;
6386 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006387 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006388 }
6389
6390 void assertReceivedMove(const Point& point) {
6391 NotifyMotionArgs motionArgs;
6392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006394 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006395 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6396 1, 0, 0, 0, 0, 0, 0, 0));
6397 }
6398};
6399
6400TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6401 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006402 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006403
6404 prepareButtons();
6405 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006406 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006407
6408 NotifyMotionArgs motionArgs;
6409
6410 // Configure the DisplayViewport such that the logical display maps to a subsection of
6411 // the display panel called the physical display. Here, the physical display is bounded by the
6412 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6413 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6414 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6415 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6416
Michael Wrighta9cf4192022-12-01 23:46:39 +00006417 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006418 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6419
6420 // Touches outside the physical display should be ignored, and should not generate any
6421 // events. Ensure touches at the following points that lie outside of the physical display
6422 // area do not generate any events.
6423 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6424 processDown(mapper, toRawX(point.x), toRawY(point.y));
6425 processSync(mapper);
6426 processUp(mapper);
6427 processSync(mapper);
6428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6429 << "Unexpected event generated for touch outside physical display at point: "
6430 << point.x << ", " << point.y;
6431 }
6432 }
6433}
6434
6435TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6436 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006437 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006438
6439 prepareButtons();
6440 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006441 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006442
6443 NotifyMotionArgs motionArgs;
6444
6445 // Configure the DisplayViewport such that the logical display maps to a subsection of
6446 // the display panel called the physical display. Here, the physical display is bounded by the
6447 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6448 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6449
Michael Wrighta9cf4192022-12-01 23:46:39 +00006450 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006451 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6452
6453 // Touches that start outside the physical display should be ignored until it enters the
6454 // physical display bounds, at which point it should generate a down event. Start a touch at
6455 // the point (5, 100), which is outside the physical display bounds.
6456 static const Point kOutsidePoint{5, 100};
6457 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6458 processSync(mapper);
6459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6460
6461 // Move the touch into the physical display area. This should generate a pointer down.
6462 processMove(mapper, toRawX(11), toRawY(21));
6463 processSync(mapper);
6464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6465 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006466 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006467 ASSERT_NO_FATAL_FAILURE(
6468 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6469
6470 // Move the touch inside the physical display area. This should generate a pointer move.
6471 processMove(mapper, toRawX(69), toRawY(159));
6472 processSync(mapper);
6473 assertReceivedMove({69, 159});
6474
6475 // Move outside the physical display area. Since the pointer is already down, this should
6476 // now continue generating events.
6477 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6478 processSync(mapper);
6479 assertReceivedMove(kOutsidePoint);
6480
6481 // Release. This should generate a pointer up.
6482 processUp(mapper);
6483 processSync(mapper);
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6486 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6487 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6488
6489 // Ensure no more events were generated.
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6492 }
6493}
6494
Prabir Pradhana9df3162022-12-05 23:57:27 +00006495// --- TouchscreenPrecisionTests ---
6496
6497// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6498// in various orientations and with different display rotations. We configure the touchscreen to
6499// have a higher resolution than that of the display by an integer scale factor in each axis so that
6500// we can enforce that coordinates match precisely as expected.
6501class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6502 public ::testing::WithParamInterface<ui::Rotation> {
6503public:
6504 void SetUp() override {
6505 SingleTouchInputMapperTest::SetUp();
6506
6507 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6508 // four times the resolution of the display in the Y axis.
6509 prepareButtons();
6510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006511 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6512 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006513 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006514 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6515 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006516 }
6517
6518 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6519 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6520 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6521 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6522
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006523 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6524 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6525
6526 static const int32_t PRECISION_RAW_X_FLAT = 16;
6527 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6528
6529 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6530 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6531
Prabir Pradhana9df3162022-12-05 23:57:27 +00006532 static const std::array<Point, 4> kRawCorners;
6533};
6534
6535const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6536 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6537 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6538 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6539 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6540}};
6541
6542// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6543// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6544// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6545TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6546 enum class Orientation {
6547 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6548 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6549 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6550 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6551 ftl_last = ORIENTATION_270,
6552 };
6553 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6554 Orientation::ORIENTATION_270;
6555 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6556 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6557 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6558 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6559 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6560 };
6561
6562 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6563
6564 // Configure the touchscreen as being installed in the one of the four different orientations
6565 // relative to the display.
6566 addConfigurationProperty("touch.deviceType", "touchScreen");
6567 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6568 prepareDisplay(ui::ROTATION_0);
6569
Arpit Singha8c236b2023-04-25 13:56:05 +00006570 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006571
6572 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6573 // orientations of either 90 or 270) this means the display's natural resolution will be
6574 // flipped.
6575 const bool displayRotated =
6576 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6577 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6578 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6579 const Rect physicalFrame{0, 0, width, height};
6580 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6581
6582 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6583 const float expectedPrecisionX = displayRotated ? 4 : 2;
6584 const float expectedPrecisionY = displayRotated ? 2 : 4;
6585
6586 // Test all four corners.
6587 for (int i = 0; i < 4; i++) {
6588 const auto& raw = kRawCorners[i];
6589 processDown(mapper, raw.x, raw.y);
6590 processSync(mapper);
6591 const auto& expected = expectedPoints[i];
6592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6593 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6594 WithCoords(expected.x, expected.y),
6595 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6596 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6597 << "with touchscreen orientation "
6598 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6599 << expected.x << ", " << expected.y << ").";
6600 processUp(mapper);
6601 processSync(mapper);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6603 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6604 WithCoords(expected.x, expected.y))));
6605 }
6606}
6607
Prabir Pradhan82687402022-12-06 01:32:53 +00006608TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6609 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6610 kMappedCorners = {
6611 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6612 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6613 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6614 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6615 };
6616
6617 const ui::Rotation displayRotation = GetParam();
6618
6619 addConfigurationProperty("touch.deviceType", "touchScreen");
6620 prepareDisplay(displayRotation);
6621
Arpit Singha8c236b2023-04-25 13:56:05 +00006622 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006623
6624 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6625
6626 // Test all four corners.
6627 for (int i = 0; i < 4; i++) {
6628 const auto& expected = expectedPoints[i];
6629 const auto& raw = kRawCorners[i];
6630 processDown(mapper, raw.x, raw.y);
6631 processSync(mapper);
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6633 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6634 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6635 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6636 << "with display rotation " << ui::toCString(displayRotation)
6637 << ", expected point (" << expected.x << ", " << expected.y << ").";
6638 processUp(mapper);
6639 processSync(mapper);
6640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6641 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6642 WithCoords(expected.x, expected.y))));
6643 }
6644}
6645
Prabir Pradhan3e798762022-12-02 21:02:11 +00006646TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6647 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6648 kMappedCorners = {
6649 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6650 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6651 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6652 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6653 };
6654
6655 const ui::Rotation displayRotation = GetParam();
6656
6657 addConfigurationProperty("touch.deviceType", "touchScreen");
6658 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6659
Arpit Singha8c236b2023-04-25 13:56:05 +00006660 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006661
6662 // Ori 270, so width and height swapped
6663 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6664 prepareDisplay(displayRotation);
6665 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6666
6667 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6668
6669 // Test all four corners.
6670 for (int i = 0; i < 4; i++) {
6671 const auto& expected = expectedPoints[i];
6672 const auto& raw = kRawCorners[i];
6673 processDown(mapper, raw.x, raw.y);
6674 processSync(mapper);
6675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6676 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6677 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6678 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6679 << "with display rotation " << ui::toCString(displayRotation)
6680 << ", expected point (" << expected.x << ", " << expected.y << ").";
6681 processUp(mapper);
6682 processSync(mapper);
6683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6684 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6685 WithCoords(expected.x, expected.y))));
6686 }
6687}
6688
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006689TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6690 const ui::Rotation displayRotation = GetParam();
6691
6692 addConfigurationProperty("touch.deviceType", "touchScreen");
6693 prepareDisplay(displayRotation);
6694
6695 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006696 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006697
6698 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6699 // MotionRanges use display pixels as their units
6700 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6701 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6702
6703 // The MotionRanges should be oriented in the rotated display's coordinate space
6704 const bool displayRotated =
6705 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6706
6707 constexpr float MAX_X = 479.5;
6708 constexpr float MAX_Y = 799.75;
6709 EXPECT_EQ(xRange->min, 0.f);
6710 EXPECT_EQ(yRange->min, 0.f);
6711 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6712 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6713
6714 EXPECT_EQ(xRange->flat, 8.f);
6715 EXPECT_EQ(yRange->flat, 8.f);
6716
6717 EXPECT_EQ(xRange->fuzz, 2.f);
6718 EXPECT_EQ(yRange->fuzz, 2.f);
6719
6720 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6721 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6722}
6723
Prabir Pradhana9df3162022-12-05 23:57:27 +00006724// Run the precision tests for all rotations.
6725INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6726 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6727 ui::ROTATION_270),
6728 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6729 return ftl::enum_string(testParamInfo.param);
6730 });
6731
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006732// --- ExternalStylusFusionTest ---
6733
6734class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6735public:
6736 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6737 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006738 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006739 prepareButtons();
6740 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006741 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006742
6743 mStylusState.when = ARBITRARY_TIME;
6744 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006745 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006746 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006747 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006748 processExternalStylusState(mapper);
6749 return mapper;
6750 }
6751
6752 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6753 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6754 for (const NotifyArgs& args : generatedArgs) {
6755 mFakeListener->notify(args);
6756 }
6757 // Loop the reader to flush the input listener queue.
6758 mReader->loopOnce();
6759 return generatedArgs;
6760 }
6761
6762protected:
6763 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006764
6765 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6766 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006767 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006768
6769 // The first pointer is withheld.
6770 processDown(mapper, 100, 200);
6771 processSync(mapper);
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6773 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6774 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6775
6776 // The external stylus reports pressure. The withheld finger pointer is released as a
6777 // stylus.
6778 mStylusState.pressure = 1.f;
6779 processExternalStylusState(mapper);
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6781 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6782 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6783
6784 // Subsequent pointer events are not withheld.
6785 processMove(mapper, 101, 201);
6786 processSync(mapper);
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6788 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6789
6790 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6792 }
6793
6794 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6795 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6796
6797 // Releasing the touch pointer ends the gesture.
6798 processUp(mapper);
6799 processSync(mapper);
6800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006801 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006802 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006803
6804 mStylusState.pressure = 0.f;
6805 processExternalStylusState(mapper);
6806 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6808 }
6809
6810 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006811 // When stylus fusion is not successful, events should be reported with the original source.
6812 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006813 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006814 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006815
6816 // The first pointer is withheld when an external stylus is connected,
6817 // and a timeout is requested.
6818 processDown(mapper, 100, 200);
6819 processSync(mapper);
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6821 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6822 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6823
6824 // If the timeout expires early, it is requested again.
6825 handleTimeout(mapper, ARBITRARY_TIME + 1);
6826 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6827 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6828
6829 // When the timeout expires, the withheld touch is released as a finger pointer.
6830 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6832 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6833
6834 // Subsequent pointer events are not withheld.
6835 processMove(mapper, 101, 201);
6836 processSync(mapper);
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6838 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6839 processUp(mapper);
6840 processSync(mapper);
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6842 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6843
6844 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6846 }
6847
6848private:
6849 InputDeviceInfo mExternalStylusDeviceInfo{};
6850};
6851
6852TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6853 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006854 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006855}
6856
6857TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6858 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6859 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6860}
6861
6862TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6863 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6864 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6865}
6866
6867// Test a successful stylus fusion gesture where the pressure is reported by the external
6868// before the touch is reported by the touchscreen.
6869TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6870 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006871 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006872
6873 // The external stylus reports pressure first. It is ignored for now.
6874 mStylusState.pressure = 1.f;
6875 processExternalStylusState(mapper);
6876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6877 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6878
6879 // When the touch goes down afterwards, it is reported as a stylus pointer.
6880 processDown(mapper, 100, 200);
6881 processSync(mapper);
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6883 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6884 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6885
6886 processMove(mapper, 101, 201);
6887 processSync(mapper);
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6889 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6890 processUp(mapper);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6893 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6894
6895 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6897}
6898
6899TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6900 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6901
6902 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6903 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6904
6905 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6906 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6907 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6908 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6909}
6910
6911TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6912 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006913 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006914
6915 mStylusState.pressure = 0.8f;
6916 processExternalStylusState(mapper);
6917 processDown(mapper, 100, 200);
6918 processSync(mapper);
6919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6920 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6921 WithPressure(0.8f))));
6922 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6923
6924 // The external stylus reports a pressure change. We wait for some time for a touch event.
6925 mStylusState.pressure = 0.6f;
6926 processExternalStylusState(mapper);
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6928 ASSERT_NO_FATAL_FAILURE(
6929 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6930
6931 // If a touch is reported within the timeout, it reports the updated pressure.
6932 processMove(mapper, 101, 201);
6933 processSync(mapper);
6934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6935 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6936 WithPressure(0.6f))));
6937 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6938
6939 // There is another pressure change.
6940 mStylusState.pressure = 0.5f;
6941 processExternalStylusState(mapper);
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6943 ASSERT_NO_FATAL_FAILURE(
6944 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6945
6946 // If a touch is not reported within the timeout, a move event is generated to report
6947 // the new pressure.
6948 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6950 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6951 WithPressure(0.5f))));
6952
6953 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6954 // repeated indefinitely.
6955 mStylusState.pressure = 0.0f;
6956 processExternalStylusState(mapper);
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6958 ASSERT_NO_FATAL_FAILURE(
6959 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6960 processMove(mapper, 102, 202);
6961 processSync(mapper);
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6963 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6964 WithPressure(0.5f))));
6965 processMove(mapper, 103, 203);
6966 processSync(mapper);
6967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6968 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6969 WithPressure(0.5f))));
6970
6971 processUp(mapper);
6972 processSync(mapper);
6973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006974 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006975 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006976
6977 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6979}
6980
6981TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6982 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006983 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006984
6985 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006986 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006987 processExternalStylusState(mapper);
6988 processDown(mapper, 100, 200);
6989 processSync(mapper);
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6991 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006992 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006993 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6994
6995 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006996 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006997 processExternalStylusState(mapper);
6998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6999 ASSERT_NO_FATAL_FAILURE(
7000 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7001
7002 // If a touch is reported within the timeout, it reports the updated pressure.
7003 processMove(mapper, 101, 201);
7004 processSync(mapper);
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7006 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007007 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007008 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7009
7010 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007011 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007012 processExternalStylusState(mapper);
7013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7014 ASSERT_NO_FATAL_FAILURE(
7015 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7016
7017 // If a touch is not reported within the timeout, a move event is generated to report
7018 // the new tool type.
7019 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7021 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007022 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007023
7024 processUp(mapper);
7025 processSync(mapper);
7026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7027 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007028 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007029
7030 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7032}
7033
7034TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7035 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007036 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007037
7038 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7039
7040 // The external stylus reports a button change. We wait for some time for a touch event.
7041 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7042 processExternalStylusState(mapper);
7043 ASSERT_NO_FATAL_FAILURE(
7044 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7045
7046 // If a touch is reported within the timeout, it reports the updated button state.
7047 processMove(mapper, 101, 201);
7048 processSync(mapper);
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7050 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7051 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7053 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7054 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7055 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7056
7057 // The button is now released.
7058 mStylusState.buttons = 0;
7059 processExternalStylusState(mapper);
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7061 ASSERT_NO_FATAL_FAILURE(
7062 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7063
7064 // If a touch is not reported within the timeout, a move event is generated to report
7065 // the new button state.
7066 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7068 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7069 WithButtonState(0))));
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007071 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7072 WithButtonState(0))));
7073
7074 processUp(mapper);
7075 processSync(mapper);
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007077 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7078
7079 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7081}
7082
Michael Wrightd02c5b62014-02-10 15:10:22 -08007083// --- MultiTouchInputMapperTest ---
7084
7085class MultiTouchInputMapperTest : public TouchInputMapperTest {
7086protected:
7087 void prepareAxes(int axes);
7088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007089 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7090 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7091 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7092 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7093 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7094 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7095 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7096 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7097 void processId(MultiTouchInputMapper& mapper, int32_t id);
7098 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7099 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7100 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007101 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007102 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007103 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7104 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105};
7106
7107void MultiTouchInputMapperTest::prepareAxes(int axes) {
7108 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7110 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 }
7112 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007113 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7114 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007115 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007116 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7117 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007118 }
7119 }
7120 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007121 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7122 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007124 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007125 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007126 }
7127 }
7128 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007129 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7130 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 }
7132 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7134 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135 }
7136 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7138 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139 }
7140 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007141 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7142 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143 }
7144 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007145 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7146 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147 }
7148 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007149 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150 }
7151}
7152
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007153void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7154 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157}
7158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007159void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7160 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162}
7163
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007164void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7165 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167}
7168
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007169void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171}
7172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007173void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175}
7176
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007177void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7178 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180}
7181
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007182void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184}
7185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007186void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188}
7189
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007190void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007192}
7193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007194void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196}
7197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007198void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200}
7201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007202void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7203 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007205}
7206
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007207void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7208 int32_t value) {
7209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7211}
7212
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007213void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007215}
7216
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007217void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7218 nsecs_t readTime) {
7219 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007220}
7221
Michael Wrightd02c5b62014-02-10 15:10:22 -08007222TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007223 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007224 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007225 prepareAxes(POSITION);
7226 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007227 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007228
arthurhungdcef2dc2020-08-11 14:47:50 +08007229 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230
7231 NotifyMotionArgs motionArgs;
7232
7233 // Two fingers down at once.
7234 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7235 processPosition(mapper, x1, y1);
7236 processMTSync(mapper);
7237 processPosition(mapper, x2, y2);
7238 processMTSync(mapper);
7239 processSync(mapper);
7240
7241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7242 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7243 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7244 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7245 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7247 ASSERT_EQ(0, motionArgs.flags);
7248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7249 ASSERT_EQ(0, motionArgs.buttonState);
7250 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007251 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007252 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007253 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7255 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7256 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7257 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7259
7260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7261 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7262 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7263 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7264 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007265 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007266 ASSERT_EQ(0, motionArgs.flags);
7267 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7268 ASSERT_EQ(0, motionArgs.buttonState);
7269 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007270 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007271 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007272 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007273 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007274 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7276 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7278 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7279 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7280 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7281 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7282
7283 // Move.
7284 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7285 processPosition(mapper, x1, y1);
7286 processMTSync(mapper);
7287 processPosition(mapper, x2, y2);
7288 processMTSync(mapper);
7289 processSync(mapper);
7290
7291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7292 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7293 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7294 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7295 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7297 ASSERT_EQ(0, motionArgs.flags);
7298 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7299 ASSERT_EQ(0, motionArgs.buttonState);
7300 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007301 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007302 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007303 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007305 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7307 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7309 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7310 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7311 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7313
7314 // First finger up.
7315 x2 += 15; y2 -= 20;
7316 processPosition(mapper, x2, y2);
7317 processMTSync(mapper);
7318 processSync(mapper);
7319
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007325 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007326 ASSERT_EQ(0, motionArgs.flags);
7327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7328 ASSERT_EQ(0, motionArgs.buttonState);
7329 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007330 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007332 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007333 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007334 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7336 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7338 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7339 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7340 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7342
7343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7344 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7345 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7346 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7347 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7348 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7349 ASSERT_EQ(0, motionArgs.flags);
7350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7351 ASSERT_EQ(0, motionArgs.buttonState);
7352 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007353 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007354 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007355 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7357 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7358 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7359 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7360 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7361
7362 // Move.
7363 x2 += 20; y2 -= 25;
7364 processPosition(mapper, x2, y2);
7365 processMTSync(mapper);
7366 processSync(mapper);
7367
7368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7370 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7371 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7372 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7373 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7374 ASSERT_EQ(0, motionArgs.flags);
7375 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7376 ASSERT_EQ(0, motionArgs.buttonState);
7377 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007378 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007379 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007380 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007381 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7382 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7383 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7384 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7385 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7386
7387 // New finger down.
7388 int32_t x3 = 700, y3 = 300;
7389 processPosition(mapper, x2, y2);
7390 processMTSync(mapper);
7391 processPosition(mapper, x3, y3);
7392 processMTSync(mapper);
7393 processSync(mapper);
7394
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7397 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7398 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7399 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007400 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007401 ASSERT_EQ(0, motionArgs.flags);
7402 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7403 ASSERT_EQ(0, motionArgs.buttonState);
7404 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007405 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007406 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007407 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007408 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007409 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7411 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7413 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7414 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7415 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7416 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7417
7418 // Second finger up.
7419 x3 += 30; y3 -= 20;
7420 processPosition(mapper, x3, y3);
7421 processMTSync(mapper);
7422 processSync(mapper);
7423
7424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7425 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7426 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7427 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7428 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007429 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007430 ASSERT_EQ(0, motionArgs.flags);
7431 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7432 ASSERT_EQ(0, motionArgs.buttonState);
7433 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007434 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007435 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007436 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007437 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007438 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7440 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7442 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7443 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7444 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7445 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7446
7447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7448 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7449 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7450 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7451 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7453 ASSERT_EQ(0, motionArgs.flags);
7454 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7455 ASSERT_EQ(0, motionArgs.buttonState);
7456 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007457 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007458 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007459 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007460 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7461 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7462 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7463 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7464 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7465
7466 // Last finger up.
7467 processMTSync(mapper);
7468 processSync(mapper);
7469
7470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7471 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7472 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7473 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7474 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7475 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7476 ASSERT_EQ(0, motionArgs.flags);
7477 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7478 ASSERT_EQ(0, motionArgs.buttonState);
7479 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007480 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007481 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007482 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007483 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7484 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7485 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7486 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7487 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7488
7489 // Should not have sent any more keys or motions.
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7492}
7493
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007494TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7495 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007496 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007497
7498 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7499 /*fuzz*/ 0, /*resolution*/ 10);
7500 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7501 /*fuzz*/ 0, /*resolution*/ 11);
7502 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7503 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7504 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7505 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7507 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7508 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7509 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7510
Arpit Singha8c236b2023-04-25 13:56:05 +00007511 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007512
7513 // X and Y axes
7514 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7515 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7516 // Touch major and minor
7517 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7518 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7519 // Tool major and minor
7520 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7521 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7522}
7523
7524TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7525 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007526 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007527
7528 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7529 /*fuzz*/ 0, /*resolution*/ 10);
7530 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7531 /*fuzz*/ 0, /*resolution*/ 11);
7532
7533 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7534
Arpit Singha8c236b2023-04-25 13:56:05 +00007535 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007536
7537 // Touch major and minor
7538 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7539 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7540 // Tool major and minor
7541 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7542 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7543}
7544
Michael Wrightd02c5b62014-02-10 15:10:22 -08007545TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007547 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007548 prepareAxes(POSITION | ID);
7549 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007550 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007551
arthurhungdcef2dc2020-08-11 14:47:50 +08007552 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553
7554 NotifyMotionArgs motionArgs;
7555
7556 // Two fingers down at once.
7557 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7558 processPosition(mapper, x1, y1);
7559 processId(mapper, 1);
7560 processMTSync(mapper);
7561 processPosition(mapper, x2, y2);
7562 processId(mapper, 2);
7563 processMTSync(mapper);
7564 processSync(mapper);
7565
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7567 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007568 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007570 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7572 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7573
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007575 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007576 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007577 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007578 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007579 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007580 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7582 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7584 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7585
7586 // Move.
7587 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7588 processPosition(mapper, x1, y1);
7589 processId(mapper, 1);
7590 processMTSync(mapper);
7591 processPosition(mapper, x2, y2);
7592 processId(mapper, 2);
7593 processMTSync(mapper);
7594 processSync(mapper);
7595
7596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007598 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007599 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007600 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007601 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007602 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7604 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7606 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7607
7608 // First finger up.
7609 x2 += 15; y2 -= 20;
7610 processPosition(mapper, x2, y2);
7611 processId(mapper, 2);
7612 processMTSync(mapper);
7613 processSync(mapper);
7614
7615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007616 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007617 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007618 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007619 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007620 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007621 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7623 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7625 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7626
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007629 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007630 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007631 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7633 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7634
7635 // Move.
7636 x2 += 20; y2 -= 25;
7637 processPosition(mapper, x2, y2);
7638 processId(mapper, 2);
7639 processMTSync(mapper);
7640 processSync(mapper);
7641
7642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7643 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007644 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007645 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007646 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7648 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7649
7650 // New finger down.
7651 int32_t x3 = 700, y3 = 300;
7652 processPosition(mapper, x2, y2);
7653 processId(mapper, 2);
7654 processMTSync(mapper);
7655 processPosition(mapper, x3, y3);
7656 processId(mapper, 3);
7657 processMTSync(mapper);
7658 processSync(mapper);
7659
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007661 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007662 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007663 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007664 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007665 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007666 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7668 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7669 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7670 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7671
7672 // Second finger up.
7673 x3 += 30; y3 -= 20;
7674 processPosition(mapper, x3, y3);
7675 processId(mapper, 3);
7676 processMTSync(mapper);
7677 processSync(mapper);
7678
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007680 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007681 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007682 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007683 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007685 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7687 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7689 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7690
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007693 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007694 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007695 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7697 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7698
7699 // Last finger up.
7700 processMTSync(mapper);
7701 processSync(mapper);
7702
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7704 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007705 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007706 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007707 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7709 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7710
7711 // Should not have sent any more keys or motions.
7712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7714}
7715
7716TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007717 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007718 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007719 prepareAxes(POSITION | ID | SLOT);
7720 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007721 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007722
arthurhungdcef2dc2020-08-11 14:47:50 +08007723 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007724
7725 NotifyMotionArgs motionArgs;
7726
7727 // Two fingers down at once.
7728 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7729 processPosition(mapper, x1, y1);
7730 processId(mapper, 1);
7731 processSlot(mapper, 1);
7732 processPosition(mapper, x2, y2);
7733 processId(mapper, 2);
7734 processSync(mapper);
7735
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7737 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007738 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007739 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007740 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7742 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7743
7744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007745 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007746 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007747 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007748 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007749 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007750 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7752 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7754 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7755
7756 // Move.
7757 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7758 processSlot(mapper, 0);
7759 processPosition(mapper, x1, y1);
7760 processSlot(mapper, 1);
7761 processPosition(mapper, x2, y2);
7762 processSync(mapper);
7763
7764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007766 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007767 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007768 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007769 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007770 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7772 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7773 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7774 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7775
7776 // First finger up.
7777 x2 += 15; y2 -= 20;
7778 processSlot(mapper, 0);
7779 processId(mapper, -1);
7780 processSlot(mapper, 1);
7781 processPosition(mapper, x2, y2);
7782 processSync(mapper);
7783
7784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007785 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007786 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007788 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007790 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7792 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7794 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7795
7796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007798 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007800 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7802 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7803
7804 // Move.
7805 x2 += 20; y2 -= 25;
7806 processPosition(mapper, x2, y2);
7807 processSync(mapper);
7808
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007811 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007812 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007813 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7815 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7816
7817 // New finger down.
7818 int32_t x3 = 700, y3 = 300;
7819 processPosition(mapper, x2, y2);
7820 processSlot(mapper, 0);
7821 processId(mapper, 3);
7822 processPosition(mapper, x3, y3);
7823 processSync(mapper);
7824
7825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007826 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007827 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007828 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007829 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007830 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007831 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7833 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7835 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7836
7837 // Second finger up.
7838 x3 += 30; y3 -= 20;
7839 processSlot(mapper, 1);
7840 processId(mapper, -1);
7841 processSlot(mapper, 0);
7842 processPosition(mapper, x3, y3);
7843 processSync(mapper);
7844
7845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007846 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007847 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007849 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007850 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007851 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7853 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7855 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7856
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7858 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007859 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007861 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7863 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7864
7865 // Last finger up.
7866 processId(mapper, -1);
7867 processSync(mapper);
7868
7869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7870 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007871 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007872 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007873 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7875 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7876
7877 // Should not have sent any more keys or motions.
7878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7880}
7881
7882TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007883 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007884 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007886 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887
7888 // These calculations are based on the input device calibration documentation.
7889 int32_t rawX = 100;
7890 int32_t rawY = 200;
7891 int32_t rawTouchMajor = 7;
7892 int32_t rawTouchMinor = 6;
7893 int32_t rawToolMajor = 9;
7894 int32_t rawToolMinor = 8;
7895 int32_t rawPressure = 11;
7896 int32_t rawDistance = 0;
7897 int32_t rawOrientation = 3;
7898 int32_t id = 5;
7899
7900 float x = toDisplayX(rawX);
7901 float y = toDisplayY(rawY);
7902 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7903 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7904 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7905 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7906 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7907 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7908 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7909 float distance = float(rawDistance);
7910
7911 processPosition(mapper, rawX, rawY);
7912 processTouchMajor(mapper, rawTouchMajor);
7913 processTouchMinor(mapper, rawTouchMinor);
7914 processToolMajor(mapper, rawToolMajor);
7915 processToolMinor(mapper, rawToolMinor);
7916 processPressure(mapper, rawPressure);
7917 processOrientation(mapper, rawOrientation);
7918 processDistance(mapper, rawDistance);
7919 processId(mapper, id);
7920 processMTSync(mapper);
7921 processSync(mapper);
7922
7923 NotifyMotionArgs args;
7924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7925 ASSERT_EQ(0, args.pointerProperties[0].id);
7926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7927 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7928 orientation, distance));
Prabir Pradhan9a53b552024-06-04 02:59:40 +00007929 ASSERT_EQ(args.flags, AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007930}
7931
7932TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007933 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007934 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007935 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7936 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007937 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938
7939 // These calculations are based on the input device calibration documentation.
7940 int32_t rawX = 100;
7941 int32_t rawY = 200;
7942 int32_t rawTouchMajor = 140;
7943 int32_t rawTouchMinor = 120;
7944 int32_t rawToolMajor = 180;
7945 int32_t rawToolMinor = 160;
7946
7947 float x = toDisplayX(rawX);
7948 float y = toDisplayY(rawY);
7949 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7950 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7951 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7952 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7953 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7954
7955 processPosition(mapper, rawX, rawY);
7956 processTouchMajor(mapper, rawTouchMajor);
7957 processTouchMinor(mapper, rawTouchMinor);
7958 processToolMajor(mapper, rawToolMajor);
7959 processToolMinor(mapper, rawToolMinor);
7960 processMTSync(mapper);
7961 processSync(mapper);
7962
7963 NotifyMotionArgs args;
7964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7966 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7967}
7968
7969TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007971 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972 prepareAxes(POSITION | TOUCH | TOOL);
7973 addConfigurationProperty("touch.size.calibration", "diameter");
7974 addConfigurationProperty("touch.size.scale", "10");
7975 addConfigurationProperty("touch.size.bias", "160");
7976 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007977 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007978
7979 // These calculations are based on the input device calibration documentation.
7980 // Note: We only provide a single common touch/tool value because the device is assumed
7981 // not to emit separate values for each pointer (isSummed = 1).
7982 int32_t rawX = 100;
7983 int32_t rawY = 200;
7984 int32_t rawX2 = 150;
7985 int32_t rawY2 = 250;
7986 int32_t rawTouchMajor = 5;
7987 int32_t rawToolMajor = 8;
7988
7989 float x = toDisplayX(rawX);
7990 float y = toDisplayY(rawY);
7991 float x2 = toDisplayX(rawX2);
7992 float y2 = toDisplayY(rawY2);
7993 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7994 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7995 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7996
7997 processPosition(mapper, rawX, rawY);
7998 processTouchMajor(mapper, rawTouchMajor);
7999 processToolMajor(mapper, rawToolMajor);
8000 processMTSync(mapper);
8001 processPosition(mapper, rawX2, rawY2);
8002 processTouchMajor(mapper, rawTouchMajor);
8003 processToolMajor(mapper, rawToolMajor);
8004 processMTSync(mapper);
8005 processSync(mapper);
8006
8007 NotifyMotionArgs args;
8008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8009 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8010
8011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008012 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008013 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8015 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8017 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8018}
8019
8020TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008021 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008022 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 prepareAxes(POSITION | TOUCH | TOOL);
8024 addConfigurationProperty("touch.size.calibration", "area");
8025 addConfigurationProperty("touch.size.scale", "43");
8026 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008027 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028
8029 // These calculations are based on the input device calibration documentation.
8030 int32_t rawX = 100;
8031 int32_t rawY = 200;
8032 int32_t rawTouchMajor = 5;
8033 int32_t rawToolMajor = 8;
8034
8035 float x = toDisplayX(rawX);
8036 float y = toDisplayY(rawY);
8037 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8038 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8039 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8040
8041 processPosition(mapper, rawX, rawY);
8042 processTouchMajor(mapper, rawTouchMajor);
8043 processToolMajor(mapper, rawToolMajor);
8044 processMTSync(mapper);
8045 processSync(mapper);
8046
8047 NotifyMotionArgs args;
8048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8050 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8051}
8052
8053TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008055 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008056 prepareAxes(POSITION | PRESSURE);
8057 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8058 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008059 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008060
Michael Wrightaa449c92017-12-13 21:21:43 +00008061 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008062 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008063 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8064 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8065 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8066
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 // These calculations are based on the input device calibration documentation.
8068 int32_t rawX = 100;
8069 int32_t rawY = 200;
8070 int32_t rawPressure = 60;
8071
8072 float x = toDisplayX(rawX);
8073 float y = toDisplayY(rawY);
8074 float pressure = float(rawPressure) * 0.01f;
8075
8076 processPosition(mapper, rawX, rawY);
8077 processPressure(mapper, rawPressure);
8078 processMTSync(mapper);
8079 processSync(mapper);
8080
8081 NotifyMotionArgs args;
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8084 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8085}
8086
8087TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008088 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008089 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008091 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092
8093 NotifyMotionArgs motionArgs;
8094 NotifyKeyArgs keyArgs;
8095
8096 processId(mapper, 1);
8097 processPosition(mapper, 100, 200);
8098 processSync(mapper);
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8100 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8101 ASSERT_EQ(0, motionArgs.buttonState);
8102
8103 // press BTN_LEFT, release BTN_LEFT
8104 processKey(mapper, BTN_LEFT, 1);
8105 processSync(mapper);
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8108 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8109
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8112 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8113
Michael Wrightd02c5b62014-02-10 15:10:22 -08008114 processKey(mapper, BTN_LEFT, 0);
8115 processSync(mapper);
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008117 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008119
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008122 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123
8124 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8125 processKey(mapper, BTN_RIGHT, 1);
8126 processKey(mapper, BTN_MIDDLE, 1);
8127 processSync(mapper);
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8130 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8131 motionArgs.buttonState);
8132
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8135 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8136
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8139 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8140 motionArgs.buttonState);
8141
Michael Wrightd02c5b62014-02-10 15:10:22 -08008142 processKey(mapper, BTN_RIGHT, 0);
8143 processSync(mapper);
8144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008145 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008147
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008150 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151
8152 processKey(mapper, BTN_MIDDLE, 0);
8153 processSync(mapper);
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008157
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008160 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161
8162 // press BTN_BACK, release BTN_BACK
8163 processKey(mapper, BTN_BACK, 1);
8164 processSync(mapper);
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8166 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8167 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008168
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008171 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8172
8173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8174 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8175 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008176
8177 processKey(mapper, BTN_BACK, 0);
8178 processSync(mapper);
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008180 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008182
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008185 ASSERT_EQ(0, motionArgs.buttonState);
8186
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8188 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8189 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8190
8191 // press BTN_SIDE, release BTN_SIDE
8192 processKey(mapper, BTN_SIDE, 1);
8193 processSync(mapper);
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8195 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8196 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008197
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008200 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8201
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8203 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8204 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205
8206 processKey(mapper, BTN_SIDE, 0);
8207 processSync(mapper);
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008211
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008214 ASSERT_EQ(0, motionArgs.buttonState);
8215
Michael Wrightd02c5b62014-02-10 15:10:22 -08008216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8217 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8218 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8219
8220 // press BTN_FORWARD, release BTN_FORWARD
8221 processKey(mapper, BTN_FORWARD, 1);
8222 processSync(mapper);
8223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8224 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8225 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008226
Michael Wrightd02c5b62014-02-10 15:10:22 -08008227 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(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8230
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8233 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234
8235 processKey(mapper, BTN_FORWARD, 0);
8236 processSync(mapper);
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008240
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008243 ASSERT_EQ(0, motionArgs.buttonState);
8244
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8246 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8247 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8248
8249 // press BTN_EXTRA, release BTN_EXTRA
8250 processKey(mapper, BTN_EXTRA, 1);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8253 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8254 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008255
Michael Wrightd02c5b62014-02-10 15:10:22 -08008256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008258 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8259
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8262 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263
8264 processKey(mapper, BTN_EXTRA, 0);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008267 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008269
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008272 ASSERT_EQ(0, motionArgs.buttonState);
8273
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8275 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8276 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8277
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8279
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280 // press BTN_STYLUS, release BTN_STYLUS
8281 processKey(mapper, BTN_STYLUS, 1);
8282 processSync(mapper);
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008285 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8286
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8289 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290
8291 processKey(mapper, BTN_STYLUS, 0);
8292 processSync(mapper);
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008294 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008295 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008296
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008299 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300
8301 // press BTN_STYLUS2, release BTN_STYLUS2
8302 processKey(mapper, BTN_STYLUS2, 1);
8303 processSync(mapper);
8304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008306 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8307
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8309 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8310 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311
8312 processKey(mapper, BTN_STYLUS2, 0);
8313 processSync(mapper);
8314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008317
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008320 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321
8322 // release touch
8323 processId(mapper, -1);
8324 processSync(mapper);
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8327 ASSERT_EQ(0, motionArgs.buttonState);
8328}
8329
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008330TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8331 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008332 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008333 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008334 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008335
8336 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8337 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8338
8339 // Touch down.
8340 processId(mapper, 1);
8341 processPosition(mapper, 100, 200);
8342 processSync(mapper);
8343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8344 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8345
8346 // Press and release button mapped to the primary stylus button.
8347 processKey(mapper, BTN_A, 1);
8348 processSync(mapper);
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8350 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8351 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8353 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8354 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8355
8356 processKey(mapper, BTN_A, 0);
8357 processSync(mapper);
8358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8359 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8361 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8362
8363 // Press and release the HID usage mapped to the secondary stylus button.
8364 processHidUsage(mapper, 0xabcd, 1);
8365 processSync(mapper);
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8367 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8368 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8370 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8371 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8372
8373 processHidUsage(mapper, 0xabcd, 0);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8376 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8378 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8379
8380 // Release touch.
8381 processId(mapper, -1);
8382 processSync(mapper);
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8384 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8385}
8386
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008388 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008389 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008390 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008391 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008392
8393 NotifyMotionArgs motionArgs;
8394
8395 // default tool type is finger
8396 processId(mapper, 1);
8397 processPosition(mapper, 100, 200);
8398 processSync(mapper);
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8400 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008401 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008402
8403 // eraser
8404 processKey(mapper, BTN_TOOL_RUBBER, 1);
8405 processSync(mapper);
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008408 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008409
8410 // stylus
8411 processKey(mapper, BTN_TOOL_RUBBER, 0);
8412 processKey(mapper, BTN_TOOL_PEN, 1);
8413 processSync(mapper);
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8415 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008416 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008417
8418 // brush
8419 processKey(mapper, BTN_TOOL_PEN, 0);
8420 processKey(mapper, BTN_TOOL_BRUSH, 1);
8421 processSync(mapper);
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008424 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008425
8426 // pencil
8427 processKey(mapper, BTN_TOOL_BRUSH, 0);
8428 processKey(mapper, BTN_TOOL_PENCIL, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008434 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008435 processKey(mapper, BTN_TOOL_PENCIL, 0);
8436 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008440 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008441
8442 // mouse
8443 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8444 processKey(mapper, BTN_TOOL_MOUSE, 1);
8445 processSync(mapper);
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008448 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008449
8450 // lens
8451 processKey(mapper, BTN_TOOL_MOUSE, 0);
8452 processKey(mapper, BTN_TOOL_LENS, 1);
8453 processSync(mapper);
8454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008456 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008457
8458 // double-tap
8459 processKey(mapper, BTN_TOOL_LENS, 0);
8460 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
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 // triple-tap
8467 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8468 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8469 processSync(mapper);
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008472 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008473
8474 // quad-tap
8475 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8476 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8477 processSync(mapper);
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008480 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008481
8482 // finger
8483 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8484 processKey(mapper, BTN_TOOL_FINGER, 1);
8485 processSync(mapper);
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008488 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008489
8490 // stylus trumps finger
8491 processKey(mapper, BTN_TOOL_PEN, 1);
8492 processSync(mapper);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008495 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496
8497 // eraser trumps stylus
8498 processKey(mapper, BTN_TOOL_RUBBER, 1);
8499 processSync(mapper);
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008502 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008503
8504 // mouse trumps eraser
8505 processKey(mapper, BTN_TOOL_MOUSE, 1);
8506 processSync(mapper);
8507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008509 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008510
8511 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8512 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8513 processSync(mapper);
8514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8515 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008516 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008517
8518 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8519 processToolType(mapper, MT_TOOL_PEN);
8520 processSync(mapper);
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008523 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008524
8525 // back to default tool type
8526 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8527 processKey(mapper, BTN_TOOL_MOUSE, 0);
8528 processKey(mapper, BTN_TOOL_RUBBER, 0);
8529 processKey(mapper, BTN_TOOL_PEN, 0);
8530 processKey(mapper, BTN_TOOL_FINGER, 0);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008534 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535}
8536
8537TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008538 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008539 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008540 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008541 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
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 BTN_TOUCH not sent yet, pressure defaults to 0
8547 processId(mapper, 1);
8548 processPosition(mapper, 100, 200);
8549 processSync(mapper);
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8553 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8554
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8556 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8558 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8559
8560 // move a little
8561 processPosition(mapper, 150, 250);
8562 processSync(mapper);
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8566 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8567
8568 // down when BTN_TOUCH is pressed, pressure defaults to 1
8569 processKey(mapper, BTN_TOUCH, 1);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8575
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8579 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8580
8581 // up when BTN_TOUCH is released, hover restored
8582 processKey(mapper, BTN_TOUCH, 0);
8583 processSync(mapper);
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8585 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8587 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8588
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8593
8594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8597 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8598
8599 // exit hover when pointer goes away
8600 processId(mapper, -1);
8601 processSync(mapper);
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8603 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8604 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8605 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8606}
8607
8608TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008609 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008610 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008611 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008612 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008613
8614 NotifyMotionArgs motionArgs;
8615
8616 // initially hovering because pressure is 0
8617 processId(mapper, 1);
8618 processPosition(mapper, 100, 200);
8619 processPressure(mapper, 0);
8620 processSync(mapper);
8621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8622 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8624 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8625
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8627 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8629 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8630
8631 // move a little
8632 processPosition(mapper, 150, 250);
8633 processSync(mapper);
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8637 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8638
8639 // down when pressure becomes non-zero
8640 processPressure(mapper, RAW_PRESSURE_MAX);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8645 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8646
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8648 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8650 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8651
8652 // up when pressure becomes 0, hover restored
8653 processPressure(mapper, 0);
8654 processSync(mapper);
8655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8656 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8658 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8659
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8663 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8664
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8666 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8668 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8669
8670 // exit hover when pointer goes away
8671 processId(mapper, -1);
8672 processSync(mapper);
8673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8676 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8677}
8678
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008679/**
8680 * Set the input device port <--> display port associations, and check that the
8681 * events are routed to the display that matches the display port.
8682 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8683 */
8684TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008685 const std::string usb2 = "USB2";
8686 const uint8_t hdmi1 = 0;
8687 const uint8_t hdmi2 = 1;
8688 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008689 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008690
8691 addConfigurationProperty("touch.deviceType", "touchScreen");
8692 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008693 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008694
8695 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8696 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8697
8698 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8699 // for this input device is specified, and the matching viewport is not present,
8700 // the input device should be disabled (at the mapper level).
8701
8702 // Add viewport for display 2 on hdmi2
8703 prepareSecondaryDisplay(type, hdmi2);
8704 // Send a touch event
8705 processPosition(mapper, 100, 100);
8706 processSync(mapper);
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8708
8709 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008710 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008711 // Send a touch event again
8712 processPosition(mapper, 100, 100);
8713 processSync(mapper);
8714
8715 NotifyMotionArgs args;
8716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8717 ASSERT_EQ(DISPLAY_ID, args.displayId);
8718}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008719
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008720TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8721 addConfigurationProperty("touch.deviceType", "touchScreen");
8722 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008723 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008724
8725 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8726
Michael Wrighta9cf4192022-12-01 23:46:39 +00008727 prepareDisplay(ui::ROTATION_0);
8728 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008729
8730 // Send a touch event
8731 processPosition(mapper, 100, 100);
8732 processSync(mapper);
8733
8734 NotifyMotionArgs args;
8735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8736 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8737}
8738
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008739TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008740 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008741
Michael Wrighta9cf4192022-12-01 23:46:39 +00008742 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008743 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008744 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008745
Josep del Río2d8c79a2023-01-23 19:33:50 +00008746 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008747
8748 NotifyMotionArgs motionArgs;
8749 processPosition(mapper, 100, 100);
8750 processSync(mapper);
8751
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008754 ASSERT_EQ(ui::LogicalDisplayId::INVALID, motionArgs.displayId);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008755}
8756
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008757/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008758 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8759 */
8760TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8761 addConfigurationProperty("touch.deviceType", "touchScreen");
8762 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008763 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008764
Michael Wrighta9cf4192022-12-01 23:46:39 +00008765 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008766 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8767 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8768 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8769 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008770
8771 NotifyMotionArgs args;
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8773 ASSERT_EQ(26, args.readTime);
8774
Harry Cutts33476232023-01-30 19:57:29 +00008775 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8776 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8777 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008778
8779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8780 ASSERT_EQ(33, args.readTime);
8781}
8782
8783/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008784 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8785 * events should not be delivered to the listener.
8786 */
8787TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8788 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008789 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008790 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008791 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008792 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008793 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008794 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008795
8796 NotifyMotionArgs motionArgs;
8797 processPosition(mapper, 100, 100);
8798 processSync(mapper);
8799
8800 mFakeListener->assertNotifyMotionWasNotCalled();
8801}
8802
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008803/**
8804 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8805 * the touch mapper can process the events and the events can be delivered to the listener.
8806 */
8807TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8808 addConfigurationProperty("touch.deviceType", "touchScreen");
8809 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008810 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008811 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008812 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008813 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008814 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008815
8816 NotifyMotionArgs motionArgs;
8817 processPosition(mapper, 100, 100);
8818 processSync(mapper);
8819
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8821 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8822}
8823
Josh Thielene986aed2023-06-01 14:17:30 +00008824/**
8825 * When the viewport is deactivated (isActive transitions from true to false),
8826 * and touch.enableForInactiveViewport is false, touches prior to the transition
8827 * should be cancelled.
8828 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008829TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8830 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008831 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008832 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008833 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008834 std::optional<DisplayViewport> optionalDisplayViewport =
8835 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8836 ASSERT_TRUE(optionalDisplayViewport.has_value());
8837 DisplayViewport displayViewport = *optionalDisplayViewport;
8838
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008839 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008840 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008841 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008842
8843 // Finger down
8844 int32_t x = 100, y = 100;
8845 processPosition(mapper, x, y);
8846 processSync(mapper);
8847
8848 NotifyMotionArgs motionArgs;
8849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8850 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8851
8852 // Deactivate display viewport
8853 displayViewport.isActive = false;
8854 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008855 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008856
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008857 // The ongoing touch should be canceled immediately
8858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8859 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8860
8861 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008862 x += 10, y += 10;
8863 processPosition(mapper, x, y);
8864 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008866
8867 // Reactivate display viewport
8868 displayViewport.isActive = true;
8869 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008870 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008871
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008872 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008873 x += 10, y += 10;
8874 processPosition(mapper, x, y);
8875 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8877 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008878}
8879
Josh Thielene986aed2023-06-01 14:17:30 +00008880/**
8881 * When the viewport is deactivated (isActive transitions from true to false),
8882 * and touch.enableForInactiveViewport is true, touches prior to the transition
8883 * should not be cancelled.
8884 */
8885TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8886 addConfigurationProperty("touch.deviceType", "touchScreen");
8887 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8888 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8889 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8890 std::optional<DisplayViewport> optionalDisplayViewport =
8891 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8892 ASSERT_TRUE(optionalDisplayViewport.has_value());
8893 DisplayViewport displayViewport = *optionalDisplayViewport;
8894
8895 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8896 prepareAxes(POSITION);
8897 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8898
8899 // Finger down
8900 int32_t x = 100, y = 100;
8901 processPosition(mapper, x, y);
8902 processSync(mapper);
8903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8904 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8905
8906 // Deactivate display viewport
8907 displayViewport.isActive = false;
8908 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8909 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8910
8911 // The ongoing touch should not be canceled
8912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8913
8914 // Finger move is not ignored
8915 x += 10, y += 10;
8916 processPosition(mapper, x, y);
8917 processSync(mapper);
8918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8919 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8920
8921 // Reactivate display viewport
8922 displayViewport.isActive = true;
8923 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8924 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8925
8926 // Finger move continues and does not start new gesture
8927 x += 10, y += 10;
8928 processPosition(mapper, x, y);
8929 processSync(mapper);
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8931 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8932}
8933
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008934TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008935 prepareAxes(POSITION);
8936 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008937 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008938 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008939
8940 NotifyMotionArgs motionArgs;
8941 // Unrotated video frame
8942 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8943 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008944 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008945 processPosition(mapper, 100, 200);
8946 processSync(mapper);
8947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8948 ASSERT_EQ(frames, motionArgs.videoFrames);
8949
8950 // Subsequent touch events should not have any videoframes
8951 // This is implemented separately in FakeEventHub,
8952 // but that should match the behaviour of TouchVideoDevice.
8953 processPosition(mapper, 200, 200);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8956 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8957}
8958
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008959TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008960 prepareAxes(POSITION);
8961 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008962 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008963 // Unrotated video frame
8964 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8965 NotifyMotionArgs motionArgs;
8966
8967 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008968 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008969 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008970 clearViewports();
8971 prepareDisplay(orientation);
8972 std::vector<TouchVideoFrame> frames{frame};
8973 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8974 processPosition(mapper, 100, 200);
8975 processSync(mapper);
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8977 ASSERT_EQ(frames, motionArgs.videoFrames);
8978 }
8979}
8980
8981TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8982 prepareAxes(POSITION);
8983 addConfigurationProperty("touch.deviceType", "touchScreen");
8984 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8985 // orientation-aware are affected by display rotation.
8986 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008987 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008988 // Unrotated video frame
8989 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8990 NotifyMotionArgs motionArgs;
8991
8992 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008993 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008994 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008995 clearViewports();
8996 prepareDisplay(orientation);
8997 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008998 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008999 processPosition(mapper, 100, 200);
9000 processSync(mapper);
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009002 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9003 // compared to the display. This is so that when the window transform (which contains the
9004 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9005 // window's coordinate space.
9006 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009007 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009008
9009 // Release finger.
9010 processSync(mapper);
9011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009012 }
9013}
9014
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009015TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009016 prepareAxes(POSITION);
9017 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009018 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009019 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9020 // so mix these.
9021 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9022 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9023 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9024 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9025 NotifyMotionArgs motionArgs;
9026
Michael Wrighta9cf4192022-12-01 23:46:39 +00009027 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009028 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009029 processPosition(mapper, 100, 200);
9030 processSync(mapper);
9031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009032 ASSERT_EQ(frames, motionArgs.videoFrames);
9033}
9034
9035TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9036 prepareAxes(POSITION);
9037 addConfigurationProperty("touch.deviceType", "touchScreen");
9038 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9039 // orientation-aware are affected by display rotation.
9040 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009041 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009042 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9043 // so mix these.
9044 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9045 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9046 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9047 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9048 NotifyMotionArgs motionArgs;
9049
Michael Wrighta9cf4192022-12-01 23:46:39 +00009050 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009051 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9052 processPosition(mapper, 100, 200);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9056 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9057 // compared to the display. This is so that when the window transform (which contains the
9058 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9059 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009060 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009061 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009062 ASSERT_EQ(frames, motionArgs.videoFrames);
9063}
9064
Arthur Hung9da14732019-09-02 16:16:58 +08009065/**
9066 * If we had defined port associations, but the viewport is not ready, the touch device would be
9067 * expected to be disabled, and it should be enabled after the viewport has found.
9068 */
9069TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009070 constexpr uint8_t hdmi2 = 1;
9071 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009072 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009073
9074 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9075
9076 addConfigurationProperty("touch.deviceType", "touchScreen");
9077 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009078 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009079
9080 ASSERT_EQ(mDevice->isEnabled(), false);
9081
9082 // Add display on hdmi2, the device should be enabled and can receive touch event.
9083 prepareSecondaryDisplay(type, hdmi2);
9084 ASSERT_EQ(mDevice->isEnabled(), true);
9085
9086 // Send a touch event.
9087 processPosition(mapper, 100, 100);
9088 processSync(mapper);
9089
9090 NotifyMotionArgs args;
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9092 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9093}
9094
Arthur Hung421eb1c2020-01-16 00:09:42 +08009095TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009096 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009097 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009098 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009099 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009100
9101 NotifyMotionArgs motionArgs;
9102
9103 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9104 // finger down
9105 processId(mapper, 1);
9106 processPosition(mapper, x1, y1);
9107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9109 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009110 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009111
9112 // finger move
9113 processId(mapper, 1);
9114 processPosition(mapper, x2, y2);
9115 processSync(mapper);
9116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009118 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009119
9120 // finger up.
9121 processId(mapper, -1);
9122 processSync(mapper);
9123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9124 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009125 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009126
9127 // new finger down
9128 processId(mapper, 1);
9129 processPosition(mapper, x3, y3);
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);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009134}
9135
9136/**
arthurhungcc7f9802020-04-30 17:55:40 +08009137 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9138 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009139 */
arthurhungcc7f9802020-04-30 17:55:40 +08009140TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009141 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009142 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009143 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009144 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009145
9146 NotifyMotionArgs motionArgs;
9147
9148 // default tool type is finger
9149 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009150 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009151 processPosition(mapper, x1, y1);
9152 processSync(mapper);
9153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9154 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009155 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009156
9157 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9158 processToolType(mapper, MT_TOOL_PALM);
9159 processSync(mapper);
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9161 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9162
9163 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009164 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009165 processPosition(mapper, x2, y2);
9166 processSync(mapper);
9167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9168
9169 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009170 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009171 processSync(mapper);
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9173
9174 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009175 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009176 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009177 processPosition(mapper, x3, y3);
9178 processSync(mapper);
9179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9180 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009181 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009182}
9183
arthurhungbf89a482020-04-17 17:37:55 +08009184/**
arthurhungcc7f9802020-04-30 17:55:40 +08009185 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9186 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009187 */
arthurhungcc7f9802020-04-30 17:55:40 +08009188TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009189 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009190 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009191 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009192 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009193
9194 NotifyMotionArgs motionArgs;
9195
9196 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009197 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9198 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009199 processPosition(mapper, x1, y1);
9200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9202 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009203 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009204
9205 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009206 processSlot(mapper, SECOND_SLOT);
9207 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009208 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009209 processSync(mapper);
9210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009211 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009212 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009213
9214 // If the tool type of the first finger changes to MT_TOOL_PALM,
9215 // we expect to receive ACTION_POINTER_UP with cancel flag.
9216 processSlot(mapper, FIRST_SLOT);
9217 processId(mapper, FIRST_TRACKING_ID);
9218 processToolType(mapper, MT_TOOL_PALM);
9219 processSync(mapper);
9220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009221 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009222 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9223
9224 // The following MOVE events of second finger should be processed.
9225 processSlot(mapper, SECOND_SLOT);
9226 processId(mapper, SECOND_TRACKING_ID);
9227 processPosition(mapper, x2 + 1, y2 + 1);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009231 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009232
9233 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9234 // it. Second finger receive move.
9235 processSlot(mapper, FIRST_SLOT);
9236 processId(mapper, INVALID_TRACKING_ID);
9237 processSync(mapper);
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009240 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009241
9242 // Second finger keeps moving.
9243 processSlot(mapper, SECOND_SLOT);
9244 processId(mapper, SECOND_TRACKING_ID);
9245 processPosition(mapper, x2 + 2, y2 + 2);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009249 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009250
9251 // Second finger up.
9252 processId(mapper, INVALID_TRACKING_ID);
9253 processSync(mapper);
9254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9255 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9256 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9257}
9258
9259/**
9260 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9261 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9262 */
9263TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9264 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009265 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009266 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009267 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009268
9269 NotifyMotionArgs motionArgs;
9270
9271 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9272 // First finger down.
9273 processId(mapper, FIRST_TRACKING_ID);
9274 processPosition(mapper, x1, y1);
9275 processSync(mapper);
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009278 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009279
9280 // Second finger down.
9281 processSlot(mapper, SECOND_SLOT);
9282 processId(mapper, SECOND_TRACKING_ID);
9283 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009286 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009287 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009288
arthurhungcc7f9802020-04-30 17:55:40 +08009289 // If the tool type of the first finger changes to MT_TOOL_PALM,
9290 // we expect to receive ACTION_POINTER_UP with cancel flag.
9291 processSlot(mapper, FIRST_SLOT);
9292 processId(mapper, FIRST_TRACKING_ID);
9293 processToolType(mapper, MT_TOOL_PALM);
9294 processSync(mapper);
9295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009296 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009297 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9298
9299 // Second finger keeps moving.
9300 processSlot(mapper, SECOND_SLOT);
9301 processId(mapper, SECOND_TRACKING_ID);
9302 processPosition(mapper, x2 + 1, y2 + 1);
9303 processSync(mapper);
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9305 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9306
9307 // second finger becomes palm, receive cancel due to only 1 finger is active.
9308 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009309 processToolType(mapper, MT_TOOL_PALM);
9310 processSync(mapper);
9311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9312 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9313
arthurhungcc7f9802020-04-30 17:55:40 +08009314 // third finger down.
9315 processSlot(mapper, THIRD_SLOT);
9316 processId(mapper, THIRD_TRACKING_ID);
9317 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009318 processPosition(mapper, x3, y3);
9319 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9321 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009322 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009323 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009324
9325 // third finger move
9326 processId(mapper, THIRD_TRACKING_ID);
9327 processPosition(mapper, x3 + 1, y3 + 1);
9328 processSync(mapper);
9329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9331
9332 // first finger up, third finger receive move.
9333 processSlot(mapper, FIRST_SLOT);
9334 processId(mapper, INVALID_TRACKING_ID);
9335 processSync(mapper);
9336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009338 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009339
9340 // second finger up, third finger receive move.
9341 processSlot(mapper, SECOND_SLOT);
9342 processId(mapper, INVALID_TRACKING_ID);
9343 processSync(mapper);
9344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9345 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009346 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009347
9348 // third finger up.
9349 processSlot(mapper, THIRD_SLOT);
9350 processId(mapper, INVALID_TRACKING_ID);
9351 processSync(mapper);
9352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9354 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9355}
9356
9357/**
9358 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9359 * and the active finger could still be allowed to receive the events
9360 */
9361TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9362 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009363 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009364 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009365 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009366
9367 NotifyMotionArgs motionArgs;
9368
9369 // default tool type is finger
9370 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9371 processId(mapper, FIRST_TRACKING_ID);
9372 processPosition(mapper, x1, y1);
9373 processSync(mapper);
9374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9375 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009376 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009377
9378 // Second finger down.
9379 processSlot(mapper, SECOND_SLOT);
9380 processId(mapper, SECOND_TRACKING_ID);
9381 processPosition(mapper, x2, y2);
9382 processSync(mapper);
9383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009384 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009385 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009386
9387 // If the tool type of the second finger changes to MT_TOOL_PALM,
9388 // we expect to receive ACTION_POINTER_UP with cancel flag.
9389 processId(mapper, SECOND_TRACKING_ID);
9390 processToolType(mapper, MT_TOOL_PALM);
9391 processSync(mapper);
9392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009393 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009394 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9395
9396 // The following MOVE event should be processed.
9397 processSlot(mapper, FIRST_SLOT);
9398 processId(mapper, FIRST_TRACKING_ID);
9399 processPosition(mapper, x1 + 1, y1 + 1);
9400 processSync(mapper);
9401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009403 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009404
9405 // second finger up.
9406 processSlot(mapper, SECOND_SLOT);
9407 processId(mapper, INVALID_TRACKING_ID);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9411
9412 // first finger keep moving
9413 processSlot(mapper, FIRST_SLOT);
9414 processId(mapper, FIRST_TRACKING_ID);
9415 processPosition(mapper, x1 + 2, y1 + 2);
9416 processSync(mapper);
9417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9419
9420 // first finger up.
9421 processId(mapper, INVALID_TRACKING_ID);
9422 processSync(mapper);
9423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9424 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9425 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009426}
9427
Arthur Hung9ad18942021-06-19 02:04:46 +00009428/**
9429 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9430 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9431 * cause slot be valid again.
9432 */
9433TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9434 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009435 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009436 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009437 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009438
9439 NotifyMotionArgs motionArgs;
9440
9441 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9442 // First finger down.
9443 processId(mapper, FIRST_TRACKING_ID);
9444 processPosition(mapper, x1, y1);
9445 processPressure(mapper, RAW_PRESSURE_MAX);
9446 processSync(mapper);
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9448 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009449 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009450
9451 // First finger move.
9452 processId(mapper, FIRST_TRACKING_ID);
9453 processPosition(mapper, x1 + 1, y1 + 1);
9454 processPressure(mapper, RAW_PRESSURE_MAX);
9455 processSync(mapper);
9456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009458 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009459
9460 // Second finger down.
9461 processSlot(mapper, SECOND_SLOT);
9462 processId(mapper, SECOND_TRACKING_ID);
9463 processPosition(mapper, x2, y2);
9464 processPressure(mapper, RAW_PRESSURE_MAX);
9465 processSync(mapper);
9466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009467 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009468 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009469
9470 // second finger up with some unexpected data.
9471 processSlot(mapper, SECOND_SLOT);
9472 processId(mapper, INVALID_TRACKING_ID);
9473 processPosition(mapper, x2, y2);
9474 processSync(mapper);
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009476 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009477 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009478
9479 // first finger up with some unexpected data.
9480 processSlot(mapper, FIRST_SLOT);
9481 processId(mapper, INVALID_TRACKING_ID);
9482 processPosition(mapper, x2, y2);
9483 processPressure(mapper, RAW_PRESSURE_MAX);
9484 processSync(mapper);
9485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9486 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009487 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009488}
9489
Arpit Singh4b4a4572023-11-24 18:19:56 +00009490TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009491 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009492 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009493 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009494 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009495
9496 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009497 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009498 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009499 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009500 processPressure(mapper, RAW_PRESSURE_MAX);
9501 processSync(mapper);
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9503 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9504
9505 // Second finger down.
9506 processSlot(mapper, SECOND_SLOT);
9507 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009508 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009509 processPressure(mapper, RAW_PRESSURE_MAX);
9510 processSync(mapper);
9511 ASSERT_NO_FATAL_FAILURE(
9512 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9513
Arpit Singh4b4a4572023-11-24 18:19:56 +00009514 // Set MT Slot state to be repopulated for the required slots
9515 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9516 mtSlotValues[0] = FIRST_TRACKING_ID;
9517 mtSlotValues[1] = SECOND_TRACKING_ID;
9518 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9519
9520 mtSlotValues[0] = x1;
9521 mtSlotValues[1] = x2;
9522 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9523
9524 mtSlotValues[0] = y1;
9525 mtSlotValues[1] = y2;
9526 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9527
9528 mtSlotValues[0] = RAW_PRESSURE_MAX;
9529 mtSlotValues[1] = RAW_PRESSURE_MAX;
9530 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9531
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009532 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009533 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009534 resetMapper(mapper, ARBITRARY_TIME);
9535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9536 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009537
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009538 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9539 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009540 processPosition(mapper, 301, 302);
9541 processSync(mapper);
9542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9543 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9545 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009546
9547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9548}
9549
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009550TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009551 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009552 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009553 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009554 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009555
9556 // First finger touches down and releases.
9557 processId(mapper, FIRST_TRACKING_ID);
9558 processPosition(mapper, 100, 200);
9559 processPressure(mapper, RAW_PRESSURE_MAX);
9560 processSync(mapper);
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9562 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9563 processId(mapper, INVALID_TRACKING_ID);
9564 processSync(mapper);
9565 ASSERT_NO_FATAL_FAILURE(
9566 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9567
9568 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9569 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009570 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9572
9573 // Send an empty sync frame. Since there are no pointers, no events are generated.
9574 processSync(mapper);
9575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9576}
9577
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009578TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009579 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009580 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009581 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009582 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009584
9585 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9586 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9587 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9588 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9589 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9590
9591 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009592 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009593 processId(mapper, FIRST_TRACKING_ID);
9594 processToolType(mapper, MT_TOOL_PEN);
9595 processPosition(mapper, 100, 200);
9596 processPressure(mapper, RAW_PRESSURE_MAX);
9597 processSync(mapper);
9598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9599 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9600 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009601 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009602
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009603 // Now that we know the device supports styluses, ensure that the device is re-configured with
9604 // the stylus source.
9605 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9606 {
9607 const auto& devices = mReader->getInputDevices();
9608 auto deviceInfo =
9609 std::find_if(devices.begin(), devices.end(),
9610 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9611 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9612 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9613 }
9614
9615 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9617
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009618 processId(mapper, INVALID_TRACKING_ID);
9619 processSync(mapper);
9620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9621 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9622 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009623 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009624}
9625
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009626// --- MultiTouchInputMapperTest_ExternalDevice ---
9627
9628class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9629protected:
Chris Yea52ade12020-08-27 16:49:20 -07009630 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009631};
9632
9633/**
9634 * Expect fallback to internal viewport if device is external and external viewport is not present.
9635 */
9636TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9637 prepareAxes(POSITION);
9638 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009639 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009640 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009641
9642 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9643
9644 NotifyMotionArgs motionArgs;
9645
9646 // Expect the event to be sent to the internal viewport,
9647 // because an external viewport is not present.
9648 processPosition(mapper, 100, 100);
9649 processSync(mapper);
9650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009651 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, motionArgs.displayId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009652
9653 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009654 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009655 processPosition(mapper, 100, 100);
9656 processSync(mapper);
9657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9658 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9659}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009660
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00009661// TODO(b/281840344): Remove the test when the old touchpad stack is removed. It is currently
9662// unclear what the behavior of the touchpad logic in TouchInputMapper should do after the
9663// PointerChoreographer refactor.
9664TEST_F(MultiTouchInputMapperTest, DISABLED_Process_TouchpadPointer) {
Harry Cutts8722be92024-04-05 14:46:05 +00009665 // prepare device
Michael Wrighta9cf4192022-12-01 23:46:39 +00009666 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009667 prepareAxes(POSITION | ID | SLOT);
9668 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9669 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009670 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009671 // run uncaptured pointer tests - pushes out generic events
9672 // FINGER 0 DOWN
9673 processId(mapper, 3);
9674 processPosition(mapper, 100, 100);
9675 processKey(mapper, BTN_TOUCH, 1);
9676 processSync(mapper);
9677
9678 // start at (100,100), cursor should be at (0,0) * scale
9679 NotifyMotionArgs args;
9680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9681 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9682 ASSERT_NO_FATAL_FAILURE(
9683 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9684
9685 // FINGER 0 MOVE
9686 processPosition(mapper, 200, 200);
9687 processSync(mapper);
9688
9689 // compute scaling to help with touch position checking
9690 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9691 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9692 float scale =
9693 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9694
9695 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9699 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009700
9701 // BUTTON DOWN
9702 processKey(mapper, BTN_LEFT, 1);
9703 processSync(mapper);
9704
9705 // touchinputmapper design sends a move before button press
9706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9709 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9710
9711 // BUTTON UP
9712 processKey(mapper, BTN_LEFT, 0);
9713 processSync(mapper);
9714
9715 // touchinputmapper design sends a move after button release
9716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9719 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009720}
9721
Harry Cutts8722be92024-04-05 14:46:05 +00009722TEST_F(MultiTouchInputMapperTest, Touchpad_GetSources) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00009723 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009724 prepareAxes(POSITION | ID | SLOT);
9725 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Hiroki Sato25040232024-02-22 17:21:22 +09009726 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Arpit Singha8c236b2023-04-25 13:56:05 +00009727 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009728
Josep del Río2d8c79a2023-01-23 19:33:50 +00009729 // uncaptured touchpad should be a pointer device
9730 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009731}
9732
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009733// --- BluetoothMultiTouchInputMapperTest ---
9734
9735class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9736protected:
9737 void SetUp() override {
9738 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9739 }
9740};
9741
9742TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9743 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009744 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009745 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009746 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009747
9748 nsecs_t kernelEventTime = ARBITRARY_TIME;
9749 nsecs_t expectedEventTime = ARBITRARY_TIME;
9750 // Touch down.
9751 processId(mapper, FIRST_TRACKING_ID);
9752 processPosition(mapper, 100, 200);
9753 processPressure(mapper, RAW_PRESSURE_MAX);
9754 processSync(mapper, ARBITRARY_TIME);
9755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9756 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9757
9758 // Process several events that come in quick succession, according to their timestamps.
9759 for (int i = 0; i < 3; i++) {
9760 constexpr static nsecs_t delta = ms2ns(1);
9761 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9762 kernelEventTime += delta;
9763 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9764
9765 processPosition(mapper, 101 + i, 201 + i);
9766 processSync(mapper, kernelEventTime);
9767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9768 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9769 WithEventTime(expectedEventTime))));
9770 }
9771
9772 // Release the touch.
9773 processId(mapper, INVALID_TRACKING_ID);
9774 processPressure(mapper, RAW_PRESSURE_MIN);
9775 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9777 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9778 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9779}
9780
9781// --- MultiTouchPointerModeTest ---
9782
HQ Liue6983c72022-04-19 22:14:56 +00009783class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9784protected:
9785 float mPointerMovementScale;
9786 float mPointerXZoomScale;
9787 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9788 addConfigurationProperty("touch.deviceType", "pointer");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009789 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009790
9791 prepareAxes(POSITION);
9792 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9793 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9794 // needs to be disabled, and the pointer gesture needs to be enabled.
Hiroki Sato25040232024-02-22 17:21:22 +09009795 mFakePolicy->setPointerCapture(/*window=*/nullptr);
HQ Liue6983c72022-04-19 22:14:56 +00009796 mFakePolicy->setPointerGestureEnabled(true);
HQ Liue6983c72022-04-19 22:14:56 +00009797
9798 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9799 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9800 mPointerMovementScale =
9801 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9802 mPointerXZoomScale =
9803 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9804 }
9805
9806 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9807 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9808 /*flat*/ 0,
9809 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9810 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9811 /*flat*/ 0,
9812 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9813 }
9814};
9815
9816/**
9817 * Two fingers down on a pointer mode touch pad. The width
9818 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9819 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9820 * be greater than the both value to be freeform gesture, so that after two
9821 * fingers start to move downwards, the gesture should be swipe.
9822 */
9823TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9824 // The min freeform gesture width is 25units/mm x 30mm = 750
9825 // which is greater than fraction of the diagnal length of the touchpad (349).
9826 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +00009827 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009828 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009829 NotifyMotionArgs motionArgs;
9830
9831 // Two fingers down at once.
9832 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9833 // Pointer's initial position is used the [0,0] coordinate.
9834 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9835
9836 processId(mapper, FIRST_TRACKING_ID);
9837 processPosition(mapper, x1, y1);
9838 processMTSync(mapper);
9839 processId(mapper, SECOND_TRACKING_ID);
9840 processPosition(mapper, x2, y2);
9841 processMTSync(mapper);
9842 processSync(mapper);
9843
9844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009845 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009846 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009847 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009848 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009849 ASSERT_NO_FATAL_FAILURE(
9850 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9851
9852 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9853 // that there should be 1 pointer.
9854 int32_t movingDistance = 200;
9855 y1 += movingDistance;
9856 y2 += movingDistance;
9857
9858 processId(mapper, FIRST_TRACKING_ID);
9859 processPosition(mapper, x1, y1);
9860 processMTSync(mapper);
9861 processId(mapper, SECOND_TRACKING_ID);
9862 processPosition(mapper, x2, y2);
9863 processMTSync(mapper);
9864 processSync(mapper);
9865
9866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009867 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009869 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009870 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009871 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9872 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9873 0, 0, 0, 0));
9874}
9875
9876/**
9877 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9878 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9879 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9880 * value to be freeform gesture, so that after two fingers start to move downwards,
9881 * the gesture should be swipe.
9882 */
9883TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9884 // The min freeform gesture width is 5units/mm x 30mm = 150
9885 // which is greater than fraction of the diagnal length of the touchpad (349).
9886 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +00009887 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +00009888 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009889 NotifyMotionArgs motionArgs;
9890
9891 // Two fingers down at once.
9892 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9893 // Pointer's initial position is used the [0,0] coordinate.
9894 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9895
9896 processId(mapper, FIRST_TRACKING_ID);
9897 processPosition(mapper, x1, y1);
9898 processMTSync(mapper);
9899 processId(mapper, SECOND_TRACKING_ID);
9900 processPosition(mapper, x2, y2);
9901 processMTSync(mapper);
9902 processSync(mapper);
9903
9904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009905 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009906 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009907 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009908 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009909 ASSERT_NO_FATAL_FAILURE(
9910 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9911
9912 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9913 // and there should be 1 pointer.
9914 int32_t movingDistance = 200;
9915 y1 += movingDistance;
9916 y2 += movingDistance;
9917
9918 processId(mapper, FIRST_TRACKING_ID);
9919 processPosition(mapper, x1, y1);
9920 processMTSync(mapper);
9921 processId(mapper, SECOND_TRACKING_ID);
9922 processPosition(mapper, x2, y2);
9923 processMTSync(mapper);
9924 processSync(mapper);
9925
9926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009927 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009929 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009930 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009931 // New coordinate is the scaled relative coordinate from the initial coordinate.
9932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9933 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9934 0, 0, 0, 0));
9935}
9936
9937/**
9938 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9939 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9940 * freeform gestures after two fingers start to move downwards.
9941 */
9942TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +00009943 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009944 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009945
9946 NotifyMotionArgs motionArgs;
9947
9948 // Two fingers down at once. Wider than the max swipe width.
9949 // The gesture is expected to be PRESS, then transformed to FREEFORM
9950 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9951
9952 processId(mapper, FIRST_TRACKING_ID);
9953 processPosition(mapper, x1, y1);
9954 processMTSync(mapper);
9955 processId(mapper, SECOND_TRACKING_ID);
9956 processPosition(mapper, x2, y2);
9957 processMTSync(mapper);
9958 processSync(mapper);
9959
9960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009961 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009962 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009963 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009964 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009965 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9966 ASSERT_NO_FATAL_FAILURE(
9967 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9968
9969 int32_t movingDistance = 200;
9970
9971 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9972 // then two down events for two pointers.
9973 y1 += movingDistance;
9974 y2 += movingDistance;
9975
9976 processId(mapper, FIRST_TRACKING_ID);
9977 processPosition(mapper, x1, y1);
9978 processMTSync(mapper);
9979 processId(mapper, SECOND_TRACKING_ID);
9980 processPosition(mapper, x2, y2);
9981 processMTSync(mapper);
9982 processSync(mapper);
9983
9984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9985 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009986 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009987 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009989 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009990 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009991 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009993 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009994 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009995 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009996 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009997 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009998 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009999 // Two pointers' scaled relative coordinates from their initial centroid.
10000 // Initial y coordinates are 0 as y1 and y2 have the same value.
10001 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10002 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10003 // When pointers move, the new coordinates equal to the initial coordinates plus
10004 // scaled moving distance.
10005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10006 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10007 0, 0, 0, 0));
10008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10009 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10010 0, 0, 0, 0));
10011
10012 // Move two fingers down again, expect one MOVE motion event.
10013 y1 += movingDistance;
10014 y2 += movingDistance;
10015
10016 processId(mapper, FIRST_TRACKING_ID);
10017 processPosition(mapper, x1, y1);
10018 processMTSync(mapper);
10019 processId(mapper, SECOND_TRACKING_ID);
10020 processPosition(mapper, x2, y2);
10021 processMTSync(mapper);
10022 processSync(mapper);
10023
10024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010025 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010027 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010028 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10030 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10031 0, 0, 0, 0, 0));
10032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10033 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10034 0, 0, 0, 0, 0));
10035}
10036
Harry Cutts39b7ca22022-10-05 15:55:48 +000010037TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010038 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010039 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010040 NotifyMotionArgs motionArgs;
10041
10042 // Place two fingers down.
10043 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10044
10045 processId(mapper, FIRST_TRACKING_ID);
10046 processPosition(mapper, x1, y1);
10047 processMTSync(mapper);
10048 processId(mapper, SECOND_TRACKING_ID);
10049 processPosition(mapper, x2, y2);
10050 processMTSync(mapper);
10051 processSync(mapper);
10052
10053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010054 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10056 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10057 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10058 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10059
10060 // Move the two fingers down and to the left.
10061 int32_t movingDistance = 200;
10062 x1 -= movingDistance;
10063 y1 += movingDistance;
10064 x2 -= movingDistance;
10065 y2 += movingDistance;
10066
10067 processId(mapper, FIRST_TRACKING_ID);
10068 processPosition(mapper, x1, y1);
10069 processMTSync(mapper);
10070 processId(mapper, SECOND_TRACKING_ID);
10071 processPosition(mapper, x2, y2);
10072 processMTSync(mapper);
10073 processSync(mapper);
10074
10075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010076 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10078 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10079 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10080 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10081}
10082
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010083TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010084 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010085 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010086 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10088
10089 // Start a stylus gesture.
10090 processKey(mapper, BTN_TOOL_PEN, 1);
10091 processId(mapper, FIRST_TRACKING_ID);
10092 processPosition(mapper, 100, 200);
10093 processSync(mapper);
10094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10095 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10096 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010097 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010098 // TODO(b/257078296): Pointer mode generates extra event.
10099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10100 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10101 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010102 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10104
10105 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10106 // gesture should be disabled.
10107 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10108 viewport->isActive = false;
10109 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010110 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10112 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10113 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010114 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010115 // TODO(b/257078296): Pointer mode generates extra event.
10116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10117 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10118 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010119 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10121}
10122
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010123// --- JoystickInputMapperTest ---
10124
10125class JoystickInputMapperTest : public InputMapperTest {
10126protected:
10127 static const int32_t RAW_X_MIN;
10128 static const int32_t RAW_X_MAX;
10129 static const int32_t RAW_Y_MIN;
10130 static const int32_t RAW_Y_MAX;
10131
10132 void SetUp() override {
10133 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10134 }
10135 void prepareAxes() {
10136 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10138 }
10139
10140 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10142 }
10143
10144 void processSync(JoystickInputMapper& mapper) {
10145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10146 }
10147
Michael Wrighta9cf4192022-12-01 23:46:39 +000010148 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010149 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10150 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10151 NO_PORT, ViewportType::VIRTUAL);
10152 }
10153};
10154
10155const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10156const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10157const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10158const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10159
10160TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10161 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010162 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010163
10164 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10165
Michael Wrighta9cf4192022-12-01 23:46:39 +000010166 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010167
10168 // Send an axis event
10169 processAxis(mapper, ABS_X, 100);
10170 processSync(mapper);
10171
10172 NotifyMotionArgs args;
10173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10174 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10175
10176 // Send another axis event
10177 processAxis(mapper, ABS_Y, 100);
10178 processSync(mapper);
10179
10180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10181 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10182}
10183
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010184// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010185
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010186class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010187protected:
10188 static const char* DEVICE_NAME;
10189 static const char* DEVICE_LOCATION;
10190 static const int32_t DEVICE_ID;
10191 static const int32_t DEVICE_GENERATION;
10192 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010193 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010194 static const int32_t EVENTHUB_ID;
10195
10196 std::shared_ptr<FakeEventHub> mFakeEventHub;
10197 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010198 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010199 std::unique_ptr<InstrumentedInputReader> mReader;
10200 std::shared_ptr<InputDevice> mDevice;
10201
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010202 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010203 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010204 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010205 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010206 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010207 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010208 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10209 }
10210
10211 void SetUp() override { SetUp(DEVICE_CLASSES); }
10212
10213 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010214 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010215 mFakePolicy.clear();
10216 }
10217
Chris Yee2b1e5c2021-03-10 22:45:12 -080010218 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10219 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010220 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010221 InputDeviceIdentifier identifier;
10222 identifier.name = name;
10223 identifier.location = location;
10224 std::shared_ptr<InputDevice> device =
10225 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10226 identifier);
10227 mReader->pushNextDevice(device);
10228 mFakeEventHub->addDevice(eventHubId, name, classes);
10229 mReader->loopOnce();
10230 return device;
10231 }
10232
10233 template <class T, typename... Args>
10234 T& addControllerAndConfigure(Args... args) {
10235 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10236
10237 return controller;
10238 }
10239};
10240
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010241const char* PeripheralControllerTest::DEVICE_NAME = "device";
10242const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10243const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10244const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10245const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010246const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10247 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010248const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010249
10250// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010251class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010252protected:
10253 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010254 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010255 }
10256};
10257
10258TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010259 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010260
Harry Cuttsa5b71292022-11-28 12:56:17 +000010261 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10262 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10263 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010264}
10265
10266TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010267 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010268
Harry Cuttsa5b71292022-11-28 12:56:17 +000010269 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10270 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10271 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010272}
10273
10274// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010275class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010276protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010277 void SetUp() override {
10278 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10279 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010280};
10281
Chris Ye85758332021-05-16 23:05:17 -070010282TEST_F(LightControllerTest, MonoLight) {
10283 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010284 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010285 .maxBrightness = 255,
10286 .flags = InputLightClass::BRIGHTNESS,
10287 .path = ""};
10288 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010289
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010290 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010291 InputDeviceInfo info;
10292 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010293 std::vector<InputDeviceLightInfo> lights = info.getLights();
10294 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010295 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10296 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10297
10298 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10299 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10300}
10301
DingYong99f2c3c2023-12-20 15:46:06 +080010302TEST_F(LightControllerTest, MonoKeyboardMuteLight) {
10303 RawLightInfo infoMono = {.id = 1,
10304 .name = "mono_keyboard_mute",
10305 .maxBrightness = 255,
10306 .flags = InputLightClass::BRIGHTNESS |
10307 InputLightClass::KEYBOARD_MIC_MUTE,
10308 .path = ""};
10309 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10310
10311 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10312 std::list<NotifyArgs> unused =
10313 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10314 /*changes=*/{});
10315
10316 InputDeviceInfo info;
10317 controller.populateDeviceInfo(&info);
10318 std::vector<InputDeviceLightInfo> lights = info.getLights();
10319 ASSERT_EQ(1U, lights.size());
10320 ASSERT_EQ(InputDeviceLightType::KEYBOARD_MIC_MUTE, lights[0].type);
10321 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10322}
10323
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010324TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10325 RawLightInfo infoMono = {.id = 1,
10326 .name = "mono_keyboard_backlight",
10327 .maxBrightness = 255,
10328 .flags = InputLightClass::BRIGHTNESS |
10329 InputLightClass::KEYBOARD_BACKLIGHT,
10330 .path = ""};
10331 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10332
10333 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10334 InputDeviceInfo info;
10335 controller.populateDeviceInfo(&info);
10336 std::vector<InputDeviceLightInfo> lights = info.getLights();
10337 ASSERT_EQ(1U, lights.size());
10338 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10339 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010340
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010341 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10342 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010343}
10344
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010345TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10346 RawLightInfo infoMono = {.id = 1,
10347 .name = "mono_light",
10348 .maxBrightness = 255,
10349 .flags = InputLightClass::BRIGHTNESS,
10350 .path = ""};
10351 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10352 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10353 "0,100,200");
10354
10355 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10356 std::list<NotifyArgs> unused =
10357 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10358 /*changes=*/{});
10359
10360 InputDeviceInfo info;
10361 controller.populateDeviceInfo(&info);
10362 std::vector<InputDeviceLightInfo> lights = info.getLights();
10363 ASSERT_EQ(1U, lights.size());
10364 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10365}
10366
10367TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10368 RawLightInfo infoMono = {.id = 1,
10369 .name = "mono_keyboard_backlight",
10370 .maxBrightness = 255,
10371 .flags = InputLightClass::BRIGHTNESS |
10372 InputLightClass::KEYBOARD_BACKLIGHT,
10373 .path = ""};
10374 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10375
10376 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10377 std::list<NotifyArgs> unused =
10378 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10379 /*changes=*/{});
10380
10381 InputDeviceInfo info;
10382 controller.populateDeviceInfo(&info);
10383 std::vector<InputDeviceLightInfo> lights = info.getLights();
10384 ASSERT_EQ(1U, lights.size());
10385 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10386}
10387
10388TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10389 RawLightInfo infoMono = {.id = 1,
10390 .name = "mono_keyboard_backlight",
10391 .maxBrightness = 255,
10392 .flags = InputLightClass::BRIGHTNESS |
10393 InputLightClass::KEYBOARD_BACKLIGHT,
10394 .path = ""};
10395 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10396 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10397 "0,100,200");
10398
10399 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10400 std::list<NotifyArgs> unused =
10401 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10402 /*changes=*/{});
10403
10404 InputDeviceInfo info;
10405 controller.populateDeviceInfo(&info);
10406 std::vector<InputDeviceLightInfo> lights = info.getLights();
10407 ASSERT_EQ(1U, lights.size());
10408 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10409 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10410 ASSERT_EQ(BrightnessLevel(0), *it);
10411 std::advance(it, 1);
10412 ASSERT_EQ(BrightnessLevel(100), *it);
10413 std::advance(it, 1);
10414 ASSERT_EQ(BrightnessLevel(200), *it);
10415}
10416
10417TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10418 RawLightInfo infoMono = {.id = 1,
10419 .name = "mono_keyboard_backlight",
10420 .maxBrightness = 255,
10421 .flags = InputLightClass::BRIGHTNESS |
10422 InputLightClass::KEYBOARD_BACKLIGHT,
10423 .path = ""};
10424 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10425 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10426 "0,100,200,300,400,500");
10427
10428 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10429 std::list<NotifyArgs> unused =
10430 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10431 /*changes=*/{});
10432
10433 InputDeviceInfo info;
10434 controller.populateDeviceInfo(&info);
10435 std::vector<InputDeviceLightInfo> lights = info.getLights();
10436 ASSERT_EQ(1U, lights.size());
10437 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10438}
10439
Chris Yee2b1e5c2021-03-10 22:45:12 -080010440TEST_F(LightControllerTest, RGBLight) {
10441 RawLightInfo infoRed = {.id = 1,
10442 .name = "red",
10443 .maxBrightness = 255,
10444 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10445 .path = ""};
10446 RawLightInfo infoGreen = {.id = 2,
10447 .name = "green",
10448 .maxBrightness = 255,
10449 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10450 .path = ""};
10451 RawLightInfo infoBlue = {.id = 3,
10452 .name = "blue",
10453 .maxBrightness = 255,
10454 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10455 .path = ""};
10456 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10457 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10458 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10459
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010460 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010461 InputDeviceInfo info;
10462 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010463 std::vector<InputDeviceLightInfo> lights = info.getLights();
10464 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010465 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10466 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10467 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10468
10469 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10470 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10471}
10472
10473TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10474 RawLightInfo infoRed = {.id = 1,
10475 .name = "red_keyboard_backlight",
10476 .maxBrightness = 255,
10477 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10478 InputLightClass::KEYBOARD_BACKLIGHT,
10479 .path = ""};
10480 RawLightInfo infoGreen = {.id = 2,
10481 .name = "green_keyboard_backlight",
10482 .maxBrightness = 255,
10483 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10484 InputLightClass::KEYBOARD_BACKLIGHT,
10485 .path = ""};
10486 RawLightInfo infoBlue = {.id = 3,
10487 .name = "blue_keyboard_backlight",
10488 .maxBrightness = 255,
10489 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10490 InputLightClass::KEYBOARD_BACKLIGHT,
10491 .path = ""};
10492 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10493 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10494 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10495
10496 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10497 InputDeviceInfo info;
10498 controller.populateDeviceInfo(&info);
10499 std::vector<InputDeviceLightInfo> lights = info.getLights();
10500 ASSERT_EQ(1U, lights.size());
10501 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10502 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10503 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10504
10505 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10506 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10507}
10508
10509TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10510 RawLightInfo infoRed = {.id = 1,
10511 .name = "red",
10512 .maxBrightness = 255,
10513 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10514 .path = ""};
10515 RawLightInfo infoGreen = {.id = 2,
10516 .name = "green",
10517 .maxBrightness = 255,
10518 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10519 .path = ""};
10520 RawLightInfo infoBlue = {.id = 3,
10521 .name = "blue",
10522 .maxBrightness = 255,
10523 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10524 .path = ""};
10525 RawLightInfo infoGlobal = {.id = 3,
10526 .name = "global_keyboard_backlight",
10527 .maxBrightness = 255,
10528 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10529 InputLightClass::KEYBOARD_BACKLIGHT,
10530 .path = ""};
10531 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10532 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10533 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10534 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10535
10536 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10537 InputDeviceInfo info;
10538 controller.populateDeviceInfo(&info);
10539 std::vector<InputDeviceLightInfo> lights = info.getLights();
10540 ASSERT_EQ(1U, lights.size());
10541 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10542 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10543 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010544
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010545 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10546 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010547}
10548
10549TEST_F(LightControllerTest, MultiColorRGBLight) {
10550 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010551 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010552 .maxBrightness = 255,
10553 .flags = InputLightClass::BRIGHTNESS |
10554 InputLightClass::MULTI_INTENSITY |
10555 InputLightClass::MULTI_INDEX,
10556 .path = ""};
10557
10558 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10559
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010560 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010561 InputDeviceInfo info;
10562 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010563 std::vector<InputDeviceLightInfo> lights = info.getLights();
10564 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010565 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10566 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10567 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10568
10569 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10570 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10571}
10572
10573TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10574 RawLightInfo infoColor = {.id = 1,
10575 .name = "multi_color_keyboard_backlight",
10576 .maxBrightness = 255,
10577 .flags = InputLightClass::BRIGHTNESS |
10578 InputLightClass::MULTI_INTENSITY |
10579 InputLightClass::MULTI_INDEX |
10580 InputLightClass::KEYBOARD_BACKLIGHT,
10581 .path = ""};
10582
10583 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10584
10585 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10586 InputDeviceInfo info;
10587 controller.populateDeviceInfo(&info);
10588 std::vector<InputDeviceLightInfo> lights = info.getLights();
10589 ASSERT_EQ(1U, lights.size());
10590 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10591 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10592 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010593
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010594 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10595 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010596}
10597
Josep del Rioa1046a82023-08-24 19:57:27 +000010598TEST_F(LightControllerTest, SonyPlayerIdLight) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010599 RawLightInfo info1 = {.id = 1,
Josep del Rioa1046a82023-08-24 19:57:27 +000010600 .name = "sony1",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010601 .maxBrightness = 255,
10602 .flags = InputLightClass::BRIGHTNESS,
10603 .path = ""};
10604 RawLightInfo info2 = {.id = 2,
Josep del Rioa1046a82023-08-24 19:57:27 +000010605 .name = "sony2",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010606 .maxBrightness = 255,
10607 .flags = InputLightClass::BRIGHTNESS,
10608 .path = ""};
10609 RawLightInfo info3 = {.id = 3,
Josep del Rioa1046a82023-08-24 19:57:27 +000010610 .name = "sony3",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010611 .maxBrightness = 255,
10612 .flags = InputLightClass::BRIGHTNESS,
10613 .path = ""};
10614 RawLightInfo info4 = {.id = 4,
Josep del Rioa1046a82023-08-24 19:57:27 +000010615 .name = "sony4",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010616 .maxBrightness = 255,
10617 .flags = InputLightClass::BRIGHTNESS,
10618 .path = ""};
10619 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10620 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10621 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10622 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10623
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010624 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010625 InputDeviceInfo info;
10626 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010627 std::vector<InputDeviceLightInfo> lights = info.getLights();
10628 ASSERT_EQ(1U, lights.size());
Josep del Rioa1046a82023-08-24 19:57:27 +000010629 ASSERT_STREQ("sony", lights[0].name.c_str());
10630 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
10631 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10632 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10633
10634 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10635 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10636 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
10637 ASSERT_STREQ("sony", lights[0].name.c_str());
10638}
10639
10640TEST_F(LightControllerTest, PlayerIdLight) {
10641 RawLightInfo info1 = {.id = 1,
10642 .name = "player-1",
10643 .maxBrightness = 255,
10644 .flags = InputLightClass::BRIGHTNESS,
10645 .path = ""};
10646 RawLightInfo info2 = {.id = 2,
10647 .name = "player-2",
10648 .maxBrightness = 255,
10649 .flags = InputLightClass::BRIGHTNESS,
10650 .path = ""};
10651 RawLightInfo info3 = {.id = 3,
10652 .name = "player-3",
10653 .maxBrightness = 255,
10654 .flags = InputLightClass::BRIGHTNESS,
10655 .path = ""};
10656 RawLightInfo info4 = {.id = 4,
10657 .name = "player-4",
10658 .maxBrightness = 255,
10659 .flags = InputLightClass::BRIGHTNESS,
10660 .path = ""};
10661 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10662 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10663 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10664 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10665
10666 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10667 InputDeviceInfo info;
10668 controller.populateDeviceInfo(&info);
10669 std::vector<InputDeviceLightInfo> lights = info.getLights();
10670 ASSERT_EQ(1U, lights.size());
10671 ASSERT_STREQ("player", lights[0].name.c_str());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010672 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010673 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10674 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010675
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010676 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10677 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10678 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010679}
10680
Michael Wrightd02c5b62014-02-10 15:10:22 -080010681} // namespace android