blob: 835f8b89c3f0a37dbfe2a5cc59c5782a45278040 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000034#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TestInputListener.h>
36#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090040#include <com_android_input_flags.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000041#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050043#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000044#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070046#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000047#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000048#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000049#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000050#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000051#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000052#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000053#include "input/DisplayViewport.h"
54#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010055
Michael Wrightd02c5b62014-02-10 15:10:22 -080056namespace android {
57
Dominik Laskowski2f01d772022-03-23 16:01:29 -070058using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000059using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070060using std::chrono_literals::operator""ms;
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -080061using std::chrono_literals::operator""s;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070062
Michael Wrightd02c5b62014-02-10 15:10:22 -080063// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000067static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t DISPLAY_WIDTH = 480;
69static constexpr int32_t DISPLAY_HEIGHT = 800;
70static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
71static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
72static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070073static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070074static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080075
arthurhungcc7f9802020-04-30 17:55:40 +080076static constexpr int32_t FIRST_SLOT = 0;
77static constexpr int32_t SECOND_SLOT = 1;
78static constexpr int32_t THIRD_SLOT = 2;
79static constexpr int32_t INVALID_TRACKING_ID = -1;
80static constexpr int32_t FIRST_TRACKING_ID = 0;
81static constexpr int32_t SECOND_TRACKING_ID = 1;
82static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080083static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
84static constexpr int32_t LIGHT_COLOR = 0x7F448866;
85static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080086
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080087static constexpr int32_t ACTION_POINTER_0_DOWN =
88 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_0_UP =
90 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_DOWN =
92 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93static constexpr int32_t ACTION_POINTER_1_UP =
94 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
95
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000096static constexpr uint32_t STYLUS_FUSION_SOURCE =
97 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
98
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000099// Minimum timestamp separation between subsequent input events from a Bluetooth device.
100static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000101
Byoungho Jungda10dd32023-10-06 17:03:45 +0900102namespace input_flags = com::android::input::flags;
103
Michael Wrightd02c5b62014-02-10 15:10:22 -0800104template<typename T>
105static inline T min(T a, T b) {
106 return a < b ? a : b;
107}
108
109static inline float avg(float x, float y) {
110 return (x + y) / 2;
111}
112
Chris Ye3fdbfef2021-01-06 18:45:18 -0800113// Mapping for light color name and the light color
114const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
115 {"green", LightColor::GREEN},
116 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800117
Michael Wrighta9cf4192022-12-01 23:46:39 +0000118static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700119 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000120 case ui::ROTATION_90:
121 return ui::ROTATION_270;
122 case ui::ROTATION_270:
123 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700124 default:
125 return orientation;
126 }
127}
128
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800129static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
130 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000131 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800132
133 const InputDeviceInfo::MotionRange* motionRange =
134 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
135 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
136}
137
138static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
139 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000140 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800141
142 const InputDeviceInfo::MotionRange* motionRange =
143 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
144 ASSERT_EQ(nullptr, motionRange);
145}
146
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700147[[maybe_unused]] static void dumpReader(InputReader& reader) {
148 std::string dump;
149 reader.dump(dump);
150 std::istringstream iss(dump);
151 for (std::string line; std::getline(iss, line);) {
152 ALOGE("%s", line.c_str());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -0800153 std::this_thread::sleep_for(1ms);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700154 }
155}
156
Michael Wrightd02c5b62014-02-10 15:10:22 -0800157// --- FakeInputMapper ---
158
159class FakeInputMapper : public InputMapper {
160 uint32_t mSources;
161 int32_t mKeyboardType;
162 int32_t mMetaState;
163 KeyedVector<int32_t, int32_t> mKeyCodeStates;
164 KeyedVector<int32_t, int32_t> mScanCodeStates;
165 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100166 // fake mapping which would normally come from keyCharacterMap
167 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800168 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700169 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700171 std::mutex mLock;
172 std::condition_variable mStateChangedCondition;
173 bool mConfigureWasCalled GUARDED_BY(mLock);
174 bool mResetWasCalled GUARDED_BY(mLock);
175 bool mProcessWasCalled GUARDED_BY(mLock);
176 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177
Arthur Hungc23540e2018-11-29 20:42:11 +0800178 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800179public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000180 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
181 uint32_t sources)
182 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800183 mSources(sources),
184 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800185 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800186 mConfigureWasCalled(false),
187 mResetWasCalled(false),
188 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800189
Chris Yea52ade12020-08-27 16:49:20 -0700190 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800191
192 void setKeyboardType(int32_t keyboardType) {
193 mKeyboardType = keyboardType;
194 }
195
196 void setMetaState(int32_t metaState) {
197 mMetaState = metaState;
198 }
199
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700200 // Sets the return value for the `process` call.
201 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
202 mProcessResult.clear();
203 for (auto notifyArg : notifyArgs) {
204 mProcessResult.push_back(notifyArg);
205 }
206 }
207
Michael Wrightd02c5b62014-02-10 15:10:22 -0800208 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 std::unique_lock<std::mutex> lock(mLock);
210 base::ScopedLockAssertion assumeLocked(mLock);
211 const bool configureCalled =
212 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
213 return mConfigureWasCalled;
214 });
215 if (!configureCalled) {
216 FAIL() << "Expected configure() to have been called.";
217 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 mConfigureWasCalled = false;
219 }
220
221 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700222 std::unique_lock<std::mutex> lock(mLock);
223 base::ScopedLockAssertion assumeLocked(mLock);
224 const bool resetCalled =
225 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
226 return mResetWasCalled;
227 });
228 if (!resetCalled) {
229 FAIL() << "Expected reset() to have been called.";
230 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 mResetWasCalled = false;
232 }
233
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000234 void assertResetWasNotCalled() {
235 std::scoped_lock lock(mLock);
236 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
237 }
238
Yi Kong9b14ac62018-07-17 13:48:38 -0700239 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700240 std::unique_lock<std::mutex> lock(mLock);
241 base::ScopedLockAssertion assumeLocked(mLock);
242 const bool processCalled =
243 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
244 return mProcessWasCalled;
245 });
246 if (!processCalled) {
247 FAIL() << "Expected process() to have been called.";
248 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800249 if (outLastEvent) {
250 *outLastEvent = mLastEvent;
251 }
252 mProcessWasCalled = false;
253 }
254
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000255 void assertProcessWasNotCalled() {
256 std::scoped_lock lock(mLock);
257 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
258 }
259
Michael Wrightd02c5b62014-02-10 15:10:22 -0800260 void setKeyCodeState(int32_t keyCode, int32_t state) {
261 mKeyCodeStates.replaceValueFor(keyCode, state);
262 }
263
264 void setScanCodeState(int32_t scanCode, int32_t state) {
265 mScanCodeStates.replaceValueFor(scanCode, state);
266 }
267
268 void setSwitchState(int32_t switchCode, int32_t state) {
269 mSwitchStates.replaceValueFor(switchCode, state);
270 }
271
272 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800273 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800274 }
275
Philip Junker4af3b3d2021-12-14 10:36:55 +0100276 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
277 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
278 }
279
Michael Wrightd02c5b62014-02-10 15:10:22 -0800280private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100281 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282
Harry Cuttsd02ea102023-03-17 18:21:30 +0000283 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800284 InputMapper::populateDeviceInfo(deviceInfo);
285
286 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000287 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288 }
289 }
290
Arpit Singhed6c3de2023-04-05 19:24:37 +0000291 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000292 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700293 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800294 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800295
296 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800297 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000298 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000299 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800300 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700301
302 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700303 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800304 }
305
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700306 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700307 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700309 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700310 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311 }
312
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700313 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700314 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800315 mLastEvent = *rawEvent;
316 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700317 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700318 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319 }
320
Chris Yea52ade12020-08-27 16:49:20 -0700321 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
323 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
324 }
325
Philip Junker4af3b3d2021-12-14 10:36:55 +0100326 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
327 auto it = mKeyCodeMapping.find(locationKeyCode);
328 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
329 }
330
Chris Yea52ade12020-08-27 16:49:20 -0700331 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800332 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
333 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
334 }
335
Chris Yea52ade12020-08-27 16:49:20 -0700336 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800337 ssize_t index = mSwitchStates.indexOfKey(switchCode);
338 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
339 }
340
Chris Yea52ade12020-08-27 16:49:20 -0700341 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700342 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700343 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700344 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
346 if (keyCodes[i] == mSupportedKeyCodes[j]) {
347 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800348 }
349 }
350 }
Chris Yea52ade12020-08-27 16:49:20 -0700351 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800352 return result;
353 }
354
355 virtual int32_t getMetaState() {
356 return mMetaState;
357 }
358
359 virtual void fadePointer() {
360 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800361
362 virtual std::optional<int32_t> getAssociatedDisplay() {
363 if (mViewport) {
364 return std::make_optional(mViewport->displayId);
365 }
366 return std::nullopt;
367 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800368};
369
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700370// --- InputReaderPolicyTest ---
371class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700372protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700373 sp<FakeInputReaderPolicy> mFakePolicy;
374
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700375 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700376 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700377};
378
379/**
380 * Check that empty set of viewports is an acceptable configuration.
381 * Also try to get internal viewport two different ways - by type and by uniqueId.
382 *
383 * There will be confusion if two viewports with empty uniqueId and identical type are present.
384 * Such configuration is not currently allowed.
385 */
386TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700387 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700388
389 // We didn't add any viewports yet, so there shouldn't be any.
390 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100391 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700392 ASSERT_FALSE(internalViewport);
393
394 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000395 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000396 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700397
398 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700399 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700400 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100401 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700402
403 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100404 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700405 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700406 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700407
408 mFakePolicy->clearViewports();
409 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700410 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700411 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100412 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700413 ASSERT_FALSE(internalViewport);
414}
415
416TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
417 const std::string internalUniqueId = "local:0";
418 const std::string externalUniqueId = "local:1";
419 const std::string virtualUniqueId1 = "virtual:2";
420 const std::string virtualUniqueId2 = "virtual:3";
421 constexpr int32_t virtualDisplayId1 = 2;
422 constexpr int32_t virtualDisplayId2 = 3;
423
424 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000425 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000426 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000427 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700428 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000429 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000430 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000431 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 // Add an virtual viewport
433 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000434 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000435 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700436 // Add another virtual viewport
437 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000438 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000439 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700440
441 // Check matching by type for internal
442 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100443 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700444 ASSERT_TRUE(internalViewport);
445 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
446
447 // Check matching by type for external
448 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100449 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700450 ASSERT_TRUE(externalViewport);
451 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
452
453 // Check matching by uniqueId for virtual viewport #1
454 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700455 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700456 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100457 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700458 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
459 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
460
461 // Check matching by uniqueId for virtual viewport #2
462 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700463 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700464 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100465 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700466 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
467 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
468}
469
470
471/**
472 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
473 * that lookup works by checking display id.
474 * Check that 2 viewports of each kind is possible, for all existing viewport types.
475 */
476TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
477 const std::string uniqueId1 = "uniqueId1";
478 const std::string uniqueId2 = "uniqueId2";
479 constexpr int32_t displayId1 = 2;
480 constexpr int32_t displayId2 = 3;
481
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100482 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
483 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700484 for (const ViewportType& type : types) {
485 mFakePolicy->clearViewports();
486 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000487 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000488 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700489 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000490 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000491 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700492
493 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700494 std::optional<DisplayViewport> viewport1 =
495 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700496 ASSERT_TRUE(viewport1);
497 ASSERT_EQ(displayId1, viewport1->displayId);
498 ASSERT_EQ(type, viewport1->type);
499
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700500 std::optional<DisplayViewport> viewport2 =
501 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700502 ASSERT_TRUE(viewport2);
503 ASSERT_EQ(displayId2, viewport2->displayId);
504 ASSERT_EQ(type, viewport2->type);
505
506 // When there are multiple viewports of the same kind, and uniqueId is not specified
507 // in the call to getDisplayViewport, then that situation is not supported.
508 // The viewports can be stored in any order, so we cannot rely on the order, since that
509 // is just implementation detail.
510 // However, we can check that it still returns *a* viewport, we just cannot assert
511 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700512 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700513 ASSERT_TRUE(someViewport);
514 }
515}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800516
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700517/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000518 * When we have multiple internal displays make sure we always return the default display when
519 * querying by type.
520 */
521TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
522 const std::string uniqueId1 = "uniqueId1";
523 const std::string uniqueId2 = "uniqueId2";
524 constexpr int32_t nonDefaultDisplayId = 2;
525 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
526 "Test display ID should not be ADISPLAY_ID_DEFAULT");
527
528 // Add the default display first and ensure it gets returned.
529 mFakePolicy->clearViewports();
530 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000531 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000532 ViewportType::INTERNAL);
533 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000534 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000535 ViewportType::INTERNAL);
536
537 std::optional<DisplayViewport> viewport =
538 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
539 ASSERT_TRUE(viewport);
540 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
541 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
542
543 // Add the default display second to make sure order doesn't matter.
544 mFakePolicy->clearViewports();
545 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000546 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000547 ViewportType::INTERNAL);
548 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000549 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000550 ViewportType::INTERNAL);
551
552 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
553 ASSERT_TRUE(viewport);
554 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
555 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
556}
557
558/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700559 * Check getDisplayViewportByPort
560 */
561TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100562 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700563 const std::string uniqueId1 = "uniqueId1";
564 const std::string uniqueId2 = "uniqueId2";
565 constexpr int32_t displayId1 = 1;
566 constexpr int32_t displayId2 = 2;
567 const uint8_t hdmi1 = 0;
568 const uint8_t hdmi2 = 1;
569 const uint8_t hdmi3 = 2;
570
571 mFakePolicy->clearViewports();
572 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000573 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000574 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700575 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000576 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000577 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700578
579 // Check that correct display viewport was returned by comparing the display ports.
580 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
581 ASSERT_TRUE(hdmi1Viewport);
582 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
583 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
584
585 // Check that we can still get the same viewport using the uniqueId
586 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
587 ASSERT_TRUE(hdmi1Viewport);
588 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
589 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
590 ASSERT_EQ(type, hdmi1Viewport->type);
591
592 // Check that we cannot find a port with "HDMI2", because we never added one
593 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
594 ASSERT_FALSE(hdmi2Viewport);
595}
596
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597// --- InputReaderTest ---
598
599class InputReaderTest : public testing::Test {
600protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700601 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700603 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000604 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800605
Chris Yea52ade12020-08-27 16:49:20 -0700606 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700607 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700608 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700609 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610
Prabir Pradhan28efc192019-11-05 01:10:04 +0000611 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700612 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800613 }
614
Chris Yea52ade12020-08-27 16:49:20 -0700615 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700616 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 }
619
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700620 void addDevice(int32_t eventHubId, const std::string& name,
621 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800622 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623
624 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800625 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800626 }
627 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000628 mReader->loopOnce();
629 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700630 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700632 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800633 }
634
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800635 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700636 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000637 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700638 }
639
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800640 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700641 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000642 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700643 }
644
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800645 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700646 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700647 ftl::Flags<InputDeviceClass> classes,
648 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800649 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800650 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000651 FakeInputMapper& mapper =
652 device->addMapper<FakeInputMapper>(eventHubId,
653 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800654 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800655 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800656 return mapper;
657 }
658};
659
Chris Ye98d3f532020-10-01 21:48:59 -0700660TEST_F(InputReaderTest, PolicyGetInputDevices) {
661 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700662 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700663 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664
665 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700666 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800667 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800668 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100669 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800670 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
671 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000672 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800673}
674
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000675TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
676 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
677 mFakeEventHub->setSysfsRootPath(1, "xyz");
678
679 // Should also have received a notification describing the new input device.
680 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
681 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
682 ASSERT_EQ(0U, inputDevice.getLights().size());
683
684 RawLightInfo infoMonolight = {.id = 123,
685 .name = "mono_keyboard_backlight",
686 .maxBrightness = 255,
687 .flags = InputLightClass::BRIGHTNESS,
688 .path = ""};
689 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
690 mReader->sysfsNodeChanged("xyz");
691 mReader->loopOnce();
692
693 // Should also have received a notification describing the new recreated input device.
694 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
695 inputDevice = mFakePolicy->getInputDevices()[0];
696 ASSERT_EQ(1U, inputDevice.getLights().size());
697}
698
Chris Yee7310032020-09-22 15:36:28 -0700699TEST_F(InputReaderTest, GetMergedInputDevices) {
700 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
701 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
702 // Add two subdevices to device
703 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
704 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000705 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
706 AINPUT_SOURCE_KEYBOARD);
707 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
708 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700709
710 // Push same device instance for next device to be added, so they'll have same identifier.
711 mReader->pushNextDevice(device);
712 mReader->pushNextDevice(device);
713 ASSERT_NO_FATAL_FAILURE(
714 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
715 ASSERT_NO_FATAL_FAILURE(
716 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
717
718 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000719 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700720}
721
Chris Yee14523a2020-12-19 13:46:00 -0800722TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
723 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
724 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
725 // Add two subdevices to device
726 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
727 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000728 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
729 AINPUT_SOURCE_KEYBOARD);
730 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
731 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800732
733 // Push same device instance for next device to be added, so they'll have same identifier.
734 mReader->pushNextDevice(device);
735 mReader->pushNextDevice(device);
736 // Sensor device is initially disabled
737 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
738 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
739 nullptr));
740 // Device is disabled because the only sub device is a sensor device and disabled initially.
741 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
742 ASSERT_FALSE(device->isEnabled());
743 ASSERT_NO_FATAL_FAILURE(
744 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
745 // The merged device is enabled if any sub device is enabled
746 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
747 ASSERT_TRUE(device->isEnabled());
748}
749
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700750TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800751 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700752 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800753 constexpr int32_t eventHubId = 1;
754 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700755 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000756 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
757 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800758 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800759 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700760
Yi Kong9b14ac62018-07-17 13:48:38 -0700761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700762
763 NotifyDeviceResetArgs resetArgs;
764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700765 ASSERT_EQ(deviceId, resetArgs.deviceId);
766
767 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800768 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000769 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700770
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700772 ASSERT_EQ(deviceId, resetArgs.deviceId);
773 ASSERT_EQ(device->isEnabled(), false);
774
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800775 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000776 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700779 ASSERT_EQ(device->isEnabled(), false);
780
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800781 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000782 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700784 ASSERT_EQ(deviceId, resetArgs.deviceId);
785 ASSERT_EQ(device->isEnabled(), true);
786}
787
Michael Wrightd02c5b62014-02-10 15:10:22 -0800788TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800789 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700790 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800791 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800792 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800793 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800794 AINPUT_SOURCE_KEYBOARD, nullptr);
795 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796
797 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
798 AINPUT_SOURCE_ANY, AKEYCODE_A))
799 << "Should return unknown when the device id is >= 0 but unknown.";
800
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800801 ASSERT_EQ(AKEY_STATE_UNKNOWN,
802 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
803 << "Should return unknown when the device id is valid but the sources are not "
804 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800806 ASSERT_EQ(AKEY_STATE_DOWN,
807 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
808 AKEYCODE_A))
809 << "Should return value provided by mapper when device id is valid and the device "
810 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811
812 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
813 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
814 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
815
816 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
817 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
818 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
819}
820
Philip Junker4af3b3d2021-12-14 10:36:55 +0100821TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
822 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
823 constexpr int32_t eventHubId = 1;
824 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
825 InputDeviceClass::KEYBOARD,
826 AINPUT_SOURCE_KEYBOARD, nullptr);
827 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
828
829 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
830 << "Should return unknown when the device with the specified id is not found.";
831
832 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
833 << "Should return correct mapping when device id is valid and mapping exists.";
834
835 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
836 << "Should return the location key code when device id is valid and there's no "
837 "mapping.";
838}
839
840TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
841 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
842 constexpr int32_t eventHubId = 1;
843 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
844 InputDeviceClass::JOYSTICK,
845 AINPUT_SOURCE_GAMEPAD, nullptr);
846 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
847
848 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
849 << "Should return unknown when the device id is valid but there is no keyboard mapper";
850}
851
Michael Wrightd02c5b62014-02-10 15:10:22 -0800852TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800853 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700854 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800855 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800856 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800857 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800858 AINPUT_SOURCE_KEYBOARD, nullptr);
859 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
861 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
862 AINPUT_SOURCE_ANY, KEY_A))
863 << "Should return unknown when the device id is >= 0 but unknown.";
864
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800865 ASSERT_EQ(AKEY_STATE_UNKNOWN,
866 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
867 << "Should return unknown when the device id is valid but the sources are not "
868 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800869
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800870 ASSERT_EQ(AKEY_STATE_DOWN,
871 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
872 KEY_A))
873 << "Should return value provided by mapper when device id is valid and the device "
874 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875
876 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
877 AINPUT_SOURCE_TRACKBALL, KEY_A))
878 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
879
880 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
881 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
882 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
883}
884
885TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800886 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700887 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800888 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800889 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800890 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800891 AINPUT_SOURCE_KEYBOARD, nullptr);
892 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893
894 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
895 AINPUT_SOURCE_ANY, SW_LID))
896 << "Should return unknown when the device id is >= 0 but unknown.";
897
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800898 ASSERT_EQ(AKEY_STATE_UNKNOWN,
899 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
900 << "Should return unknown when the device id is valid but the sources are not "
901 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800903 ASSERT_EQ(AKEY_STATE_DOWN,
904 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
905 SW_LID))
906 << "Should return value provided by mapper when device id is valid and the device "
907 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908
909 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
910 AINPUT_SOURCE_TRACKBALL, SW_LID))
911 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
912
913 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
914 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
915 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
916}
917
918TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800919 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700920 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800921 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800922 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800923 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800924 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100925
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800926 mapper.addSupportedKeyCode(AKEYCODE_A);
927 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800928
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700929 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 uint8_t flags[4] = { 0, 0, 0, 1 };
931
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700932 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 << "Should return false when device id is >= 0 but unknown.";
934 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
935
936 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700937 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800938 << "Should return false when device id is valid but the sources are not supported by "
939 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
941
942 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700943 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800944 keyCodes, flags))
945 << "Should return value provided by mapper when device id is valid and the device "
946 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800947 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
948
949 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700950 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
951 << "Should return false when the device id is < 0 but the sources are not supported by "
952 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
954
955 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700956 ASSERT_TRUE(
957 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
958 << "Should return value provided by mapper when device id is < 0 and one of the "
959 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800960 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
961}
962
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000963TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800964 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700965 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800966
967 NotifyConfigurationChangedArgs args;
968
969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
970 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
971}
972
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000973TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800974 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700975 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000976 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800977 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000978 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800979 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800980 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800981 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800982
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000983 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000984 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800985 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
986
987 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800988 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000989 ASSERT_EQ(when, event.when);
990 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800991 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800992 ASSERT_EQ(EV_KEY, event.type);
993 ASSERT_EQ(KEY_A, event.code);
994 ASSERT_EQ(1, event.value);
995}
996
Garfield Tan1c7bc862020-01-28 13:24:04 -0800997TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800998 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700999 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001000 constexpr int32_t eventHubId = 1;
1001 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001002 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001003 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1004 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001005 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001006 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001007
1008 NotifyDeviceResetArgs resetArgs;
1009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001010 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001011
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001012 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001013 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001015 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001016 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001017
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001018 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001019 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001021 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001022 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001023
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001024 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001025 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001027 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001028 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001029}
1030
Garfield Tan1c7bc862020-01-28 13:24:04 -08001031TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1032 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001033 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001034 constexpr int32_t eventHubId = 1;
1035 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1036 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001037 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1038 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001039 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001040 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1041
1042 NotifyDeviceResetArgs resetArgs;
1043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1044 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1045}
1046
Arthur Hungc23540e2018-11-29 20:42:11 +08001047TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001048 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001049 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001050 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001051 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001052 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1053 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001054 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1055 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001056 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001057
1058 const uint8_t hdmi1 = 1;
1059
1060 // Associated touch screen with second display.
1061 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1062
1063 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001064 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001065 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001066 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001067 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001068 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001069 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001070 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001071 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001072
1073 // Add the device, and make sure all of the callbacks are triggered.
1074 // The device is added after the input port associations are processed since
1075 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001076 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001079 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001080
Arthur Hung2c9a3342019-07-23 14:18:59 +08001081 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001082 ASSERT_EQ(deviceId, device->getId());
1083 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1084 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001085
1086 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001087 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001088 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001089 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001090}
1091
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001092TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1093 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001094 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001095 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1096 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1097 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001098 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1099 AINPUT_SOURCE_KEYBOARD);
1100 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1101 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001102 mReader->pushNextDevice(device);
1103 mReader->pushNextDevice(device);
1104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1105 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1106
1107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1108
1109 NotifyDeviceResetArgs resetArgs;
1110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1111 ASSERT_EQ(deviceId, resetArgs.deviceId);
1112 ASSERT_TRUE(device->isEnabled());
1113 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1114 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1115
1116 disableDevice(deviceId);
1117 mReader->loopOnce();
1118
1119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1120 ASSERT_EQ(deviceId, resetArgs.deviceId);
1121 ASSERT_FALSE(device->isEnabled());
1122 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1123 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1124
1125 enableDevice(deviceId);
1126 mReader->loopOnce();
1127
1128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1129 ASSERT_EQ(deviceId, resetArgs.deviceId);
1130 ASSERT_TRUE(device->isEnabled());
1131 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1132 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1133}
1134
1135TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1136 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001137 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001138 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1139 // Add two subdevices to device
1140 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1141 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001142 device->addMapper<FakeInputMapper>(eventHubIds[0],
1143 mFakePolicy->getReaderConfiguration(),
1144 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001145 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001146 device->addMapper<FakeInputMapper>(eventHubIds[1],
1147 mFakePolicy->getReaderConfiguration(),
1148 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001149 mReader->pushNextDevice(device);
1150 mReader->pushNextDevice(device);
1151 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1152 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1153
1154 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1155 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1156
1157 ASSERT_EQ(AKEY_STATE_DOWN,
1158 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1159 ASSERT_EQ(AKEY_STATE_DOWN,
1160 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1161 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1162 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1163}
1164
Prabir Pradhan7e186182020-11-10 13:56:45 -08001165TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1166 NotifyPointerCaptureChangedArgs args;
1167
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001168 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001169 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001170 mReader->loopOnce();
1171 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001172 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1173 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001174
1175 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001176 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001177 mReader->loopOnce();
1178 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001179 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001180
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001181 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001182 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001183 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001184 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001185 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001186}
1187
Chris Ye87143712020-11-10 05:05:58 +00001188class FakeVibratorInputMapper : public FakeInputMapper {
1189public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001190 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1191 const InputReaderConfiguration& readerConfig, uint32_t sources)
1192 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001193
1194 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1195};
1196
1197TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1198 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001199 ftl::Flags<InputDeviceClass> deviceClass =
1200 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001201 constexpr int32_t eventHubId = 1;
1202 const char* DEVICE_LOCATION = "BLUETOOTH";
1203 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1204 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001205 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1206 mFakePolicy->getReaderConfiguration(),
1207 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001208 mReader->pushNextDevice(device);
1209
1210 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1211 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1212
1213 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1214 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1215}
1216
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001217// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001218
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001219class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001220public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001221 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001222
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001223 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001224
Andy Chenf9f1a022022-08-29 20:07:10 -04001225 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1226
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1228
1229 void dump(std::string& dump) override {}
1230
1231 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1232 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001233 }
1234
Chris Yee2b1e5c2021-03-10 22:45:12 -08001235 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1236 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001237 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001238
1239 bool setLightColor(int32_t lightId, int32_t color) override {
1240 getDeviceContext().setLightBrightness(lightId, color >> 24);
1241 return true;
1242 }
1243
1244 std::optional<int32_t> getLightColor(int32_t lightId) override {
1245 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1246 if (!result.has_value()) {
1247 return std::nullopt;
1248 }
1249 return result.value() << 24;
1250 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001251
1252 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1253
1254 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1255
1256private:
1257 InputDeviceContext& mDeviceContext;
1258 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1259 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001260 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001261};
1262
Chris Yee2b1e5c2021-03-10 22:45:12 -08001263TEST_F(InputReaderTest, BatteryGetCapacity) {
1264 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001265 ftl::Flags<InputDeviceClass> deviceClass =
1266 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001267 constexpr int32_t eventHubId = 1;
1268 const char* DEVICE_LOCATION = "BLUETOOTH";
1269 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001270 FakePeripheralController& controller =
1271 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001272 mReader->pushNextDevice(device);
1273
1274 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1275
Harry Cuttsa5b71292022-11-28 12:56:17 +00001276 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1277 FakeEventHub::BATTERY_CAPACITY);
1278 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001279}
1280
1281TEST_F(InputReaderTest, BatteryGetStatus) {
1282 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001283 ftl::Flags<InputDeviceClass> deviceClass =
1284 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001285 constexpr int32_t eventHubId = 1;
1286 const char* DEVICE_LOCATION = "BLUETOOTH";
1287 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001288 FakePeripheralController& controller =
1289 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001290 mReader->pushNextDevice(device);
1291
1292 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1293
Harry Cuttsa5b71292022-11-28 12:56:17 +00001294 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1295 FakeEventHub::BATTERY_STATUS);
1296 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001297}
1298
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001299TEST_F(InputReaderTest, BatteryGetDevicePath) {
1300 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1301 ftl::Flags<InputDeviceClass> deviceClass =
1302 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1303 constexpr int32_t eventHubId = 1;
1304 const char* DEVICE_LOCATION = "BLUETOOTH";
1305 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1306 device->addController<FakePeripheralController>(eventHubId);
1307 mReader->pushNextDevice(device);
1308
1309 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1310
Harry Cuttsa5b71292022-11-28 12:56:17 +00001311 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001312}
1313
Chris Ye3fdbfef2021-01-06 18:45:18 -08001314TEST_F(InputReaderTest, LightGetColor) {
1315 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001316 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001317 constexpr int32_t eventHubId = 1;
1318 const char* DEVICE_LOCATION = "BLUETOOTH";
1319 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001320 FakePeripheralController& controller =
1321 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001322 mReader->pushNextDevice(device);
1323 RawLightInfo info = {.id = 1,
1324 .name = "Mono",
1325 .maxBrightness = 255,
1326 .flags = InputLightClass::BRIGHTNESS,
1327 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001328 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1329 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001330
1331 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001332
Harry Cutts33476232023-01-30 19:57:29 +00001333 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1334 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1335 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1336 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001337}
1338
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001339// --- InputReaderIntegrationTest ---
1340
1341// These tests create and interact with the InputReader only through its interface.
1342// The InputReader is started during SetUp(), which starts its processing in its own
1343// thread. The tests use linux uinput to emulate input devices.
1344// NOTE: Interacting with the physical device while these tests are running may cause
1345// the tests to fail.
1346class InputReaderIntegrationTest : public testing::Test {
1347protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001348 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001349 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001350 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001351
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001352 std::shared_ptr<FakePointerController> mFakePointerController;
1353
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001354 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1355 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1356
Chris Yea52ade12020-08-27 16:49:20 -07001357 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001358#if !defined(__ANDROID__)
1359 GTEST_SKIP();
1360#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001361 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001362 mFakePointerController = std::make_shared<FakePointerController>();
1363 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001364
Arpit Singh440bf652023-08-09 09:23:43 +00001365 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001366 }
1367
Chris Yea52ade12020-08-27 16:49:20 -07001368 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001369#if !defined(__ANDROID__)
1370 return;
1371#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001372 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001373 mReader.reset();
1374 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001375 mFakePolicy.clear();
1376 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001377
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001378 std::optional<InputDeviceInfo> waitForDevice(const std::string& deviceName) {
1379 std::chrono::time_point start = std::chrono::steady_clock::now();
1380 while (true) {
1381 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1382 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1383 [&deviceName](const InputDeviceInfo& info) {
1384 return info.getIdentifier().name == deviceName;
1385 });
1386 if (it != inputDevices.end()) {
1387 return std::make_optional(*it);
1388 }
1389 std::this_thread::sleep_for(1ms);
1390 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
1391 if (elapsed > 5s) {
1392 return {};
1393 }
1394 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001395 }
Arpit Singh440bf652023-08-09 09:23:43 +00001396
1397 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001398 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1399 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001400
1401 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1402 *mTestListener);
1403 ASSERT_EQ(mReader->start(), OK);
1404
1405 // Since this test is run on a real device, all the input devices connected
1406 // to the test device will show up in mReader. We wait for those input devices to
1407 // show up before beginning the tests.
1408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1410 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1411 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001412};
1413
1414TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1415 // An invalid input device that is only used for this test.
1416 class InvalidUinputDevice : public UinputDevice {
1417 public:
Harry Cutts33476232023-01-30 19:57:29 +00001418 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001419
1420 private:
1421 void configureDevice(int fd, uinput_user_dev* device) override {}
1422 };
1423
1424 const size_t numDevices = mFakePolicy->getInputDevices().size();
1425
1426 // UinputDevice does not set any event or key bits, so InputReader should not
1427 // consider it as a valid device.
1428 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1429 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1431 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1432
1433 invalidDevice.reset();
1434 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1435 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1436 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1437}
1438
1439TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1440 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1441
1442 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1443 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1445 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1446
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001447 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001448 ASSERT_TRUE(device.has_value());
1449 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1450 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1451 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001452
1453 keyboard.reset();
1454 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1455 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1456 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1457}
1458
1459TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1460 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1461 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1462
1463 NotifyConfigurationChangedArgs configChangedArgs;
1464 ASSERT_NO_FATAL_FAILURE(
1465 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001466 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001467 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1468
1469 NotifyKeyArgs keyArgs;
1470 keyboard->pressAndReleaseHomeKey();
1471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1472 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001473 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001474 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001475 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001476 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001477 prevTimestamp = keyArgs.eventTime;
1478
1479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1480 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001481 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001482 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001483 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001484}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001485
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001486TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1487 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1488 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1489
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001490 const auto device = waitForDevice(stylus->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001491 ASSERT_TRUE(device.has_value());
1492
Prabir Pradhana3621852022-10-14 18:57:23 +00001493 // An external stylus with buttons should also be recognized as a keyboard.
1494 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001495 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1496 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1497
1498 const auto DOWN =
1499 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1500 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1501
1502 stylus->pressAndReleaseKey(BTN_STYLUS);
1503 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1504 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1505 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1506 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1507
1508 stylus->pressAndReleaseKey(BTN_STYLUS2);
1509 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1510 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1511 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1512 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1513
1514 stylus->pressAndReleaseKey(BTN_STYLUS3);
1515 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1516 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1518 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1519}
1520
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001521TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1522 std::unique_ptr<UinputKeyboard> keyboard =
1523 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1524 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1525 KEY_R, KEY_T, KEY_Y,
1526 BTN_STYLUS, BTN_STYLUS2,
1527 BTN_STYLUS3});
1528 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1529
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001530 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001531 ASSERT_TRUE(device.has_value());
1532
1533 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1534 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1535 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1536 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1537}
1538
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001539TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1540 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1541 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1542 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1543 createUinputDevice<UinputKeyboardWithHidUsage>(
1544 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1545 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1546
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001547 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001548 ASSERT_TRUE(device.has_value());
1549
1550 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1551 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1552
1553 // If a device supports reporting HID usage codes, it shouldn't automatically support
1554 // stylus keys.
1555 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1556 uint8_t outFlags[] = {0};
1557 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1558 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1559}
1560
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001561/**
1562 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1563 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1564 * are passed to the listener.
1565 */
1566static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1567TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1568 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1569 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1570 NotifyKeyArgs keyArgs;
1571
1572 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1573 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1574 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1575 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1576
1577 controller->pressAndReleaseKey(BTN_GEAR_UP);
1578 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1579 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1580 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1581}
1582
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001583// --- TouchIntegrationTest ---
1584
Arpit Singh440bf652023-08-09 09:23:43 +00001585class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001586protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001587 const std::string UNIQUE_ID = "local:0";
1588
Chris Yea52ade12020-08-27 16:49:20 -07001589 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001590#if !defined(__ANDROID__)
1591 GTEST_SKIP();
1592#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001593 InputReaderIntegrationTest::SetUp();
1594 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001595 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1596 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001597
1598 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1599 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1600 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001601 const auto info = waitForDevice(mDevice->getName());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001602 ASSERT_TRUE(info);
1603 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001604 }
1605
1606 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001607 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001608 std::optional<uint8_t> physicalPort,
1609 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001610 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001611 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001612 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001613 }
1614
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001615 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1616 NotifyMotionArgs args;
1617 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1618 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001619 ASSERT_EQ(points.size(), args.getPointerCount());
1620 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001621 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1622 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1623 }
1624 }
1625
Arthur Hungaab25622020-01-16 11:22:11 +08001626 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001627 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001628};
1629
Arpit Singh440bf652023-08-09 09:23:43 +00001630enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1631
1632class TouchIntegrationTest : public BaseTouchIntegrationTest,
1633 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1634protected:
1635 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1636 const std::string INPUT_PORT = "uinput_touch/input0";
1637
1638 void SetUp() override {
1639#if !defined(__ANDROID__)
1640 GTEST_SKIP();
1641#endif
1642 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1643 BaseTouchIntegrationTest::SetUp();
1644 return;
1645 }
1646
1647 // setup policy with a input-port or UniqueId association to the display
1648 bool isInputPortAssociation =
1649 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1650
1651 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1652 if (isInputPortAssociation) {
1653 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1654 } else {
1655 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1656 }
1657 mFakePointerController = std::make_shared<FakePointerController>();
1658 mFakePolicy->setPointerController(mFakePointerController);
1659
1660 InputReaderIntegrationTest::setupInputReader();
1661
1662 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1663 INPUT_PORT);
1664 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1665
1666 // Add a display linked to a physical port or UniqueId.
1667 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1668 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1669 ViewportType::INTERNAL);
1670 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1671 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001672 const auto info = waitForDevice(mDevice->getName());
Arpit Singh440bf652023-08-09 09:23:43 +00001673 ASSERT_TRUE(info);
1674 mDeviceInfo = *info;
1675 }
1676};
1677
1678TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001679 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1680 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1681 // presses).
1682 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1683 mDeviceInfo.getSources());
1684}
1685
Arpit Singh440bf652023-08-09 09:23:43 +00001686TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001687 NotifyMotionArgs args;
1688 const Point centerPoint = mDevice->getCenterPoint();
1689
1690 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001691 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001692 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001693 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001694 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1695 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1696
1697 // ACTION_MOVE
1698 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001699 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001700 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1702
1703 // ACTION_UP
1704 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001705 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001706 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1707 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1708}
1709
Arpit Singh440bf652023-08-09 09:23:43 +00001710TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001711 NotifyMotionArgs args;
1712 const Point centerPoint = mDevice->getCenterPoint();
1713
1714 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001715 mDevice->sendSlot(FIRST_SLOT);
1716 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001717 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001718 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001719 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1720 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1721
1722 // ACTION_POINTER_DOWN (Second slot)
1723 const Point secondPoint = centerPoint + Point(100, 100);
1724 mDevice->sendSlot(SECOND_SLOT);
1725 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001726 mDevice->sendDown(secondPoint);
1727 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001728 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001729 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001730
1731 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001732 mDevice->sendMove(secondPoint + Point(1, 1));
1733 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001734 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1736
1737 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001738 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001739 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001740 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001741 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001742
1743 // ACTION_UP
1744 mDevice->sendSlot(FIRST_SLOT);
1745 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001746 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001747 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1748 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1749}
1750
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001751/**
1752 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1753 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1754 * data?
1755 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1756 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1757 * for Pointer 0 only is generated after.
1758 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1759 * events, we will not miss any information.
1760 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1761 * event generated afterwards that contains the newest movement of pointer 0.
1762 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1763 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1764 * losing information about non-palm pointers.
1765 */
Arpit Singh440bf652023-08-09 09:23:43 +00001766TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001767 NotifyMotionArgs args;
1768 const Point centerPoint = mDevice->getCenterPoint();
1769
1770 // ACTION_DOWN
1771 mDevice->sendSlot(FIRST_SLOT);
1772 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1773 mDevice->sendDown(centerPoint);
1774 mDevice->sendSync();
1775 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1776
1777 // ACTION_POINTER_DOWN (Second slot)
1778 const Point secondPoint = centerPoint + Point(100, 100);
1779 mDevice->sendSlot(SECOND_SLOT);
1780 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1781 mDevice->sendDown(secondPoint);
1782 mDevice->sendSync();
1783 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1784
1785 // ACTION_MOVE (First slot)
1786 mDevice->sendSlot(FIRST_SLOT);
1787 mDevice->sendMove(centerPoint + Point(5, 5));
1788 // ACTION_POINTER_UP (Second slot)
1789 mDevice->sendSlot(SECOND_SLOT);
1790 mDevice->sendPointerUp();
1791 // Send a single sync for the above 2 pointer updates
1792 mDevice->sendSync();
1793
1794 // First, we should get POINTER_UP for the second pointer
1795 assertReceivedMotion(ACTION_POINTER_1_UP,
1796 {/*first pointer */ centerPoint + Point(5, 5),
1797 /*second pointer*/ secondPoint});
1798
1799 // Next, the MOVE event for the first pointer
1800 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1801}
1802
1803/**
1804 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1805 * move, and then it will go up, all in the same frame.
1806 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1807 * gets sent to the listener.
1808 */
Arpit Singh440bf652023-08-09 09:23:43 +00001809TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001810 NotifyMotionArgs args;
1811 const Point centerPoint = mDevice->getCenterPoint();
1812
1813 // ACTION_DOWN
1814 mDevice->sendSlot(FIRST_SLOT);
1815 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1816 mDevice->sendDown(centerPoint);
1817 mDevice->sendSync();
1818 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1819
1820 // ACTION_POINTER_DOWN (Second slot)
1821 const Point secondPoint = centerPoint + Point(100, 100);
1822 mDevice->sendSlot(SECOND_SLOT);
1823 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1824 mDevice->sendDown(secondPoint);
1825 mDevice->sendSync();
1826 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1827
1828 // ACTION_MOVE (First slot)
1829 mDevice->sendSlot(FIRST_SLOT);
1830 mDevice->sendMove(centerPoint + Point(5, 5));
1831 // ACTION_POINTER_UP (Second slot)
1832 mDevice->sendSlot(SECOND_SLOT);
1833 mDevice->sendMove(secondPoint + Point(6, 6));
1834 mDevice->sendPointerUp();
1835 // Send a single sync for the above 2 pointer updates
1836 mDevice->sendSync();
1837
1838 // First, we should get POINTER_UP for the second pointer
1839 // The movement of the second pointer during the liftoff frame is ignored.
1840 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1841 assertReceivedMotion(ACTION_POINTER_1_UP,
1842 {/*first pointer */ centerPoint + Point(5, 5),
1843 /*second pointer*/ secondPoint});
1844
1845 // Next, the MOVE event for the first pointer
1846 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1847}
1848
Arpit Singh440bf652023-08-09 09:23:43 +00001849TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001850 NotifyMotionArgs args;
1851 const Point centerPoint = mDevice->getCenterPoint();
1852
1853 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001854 mDevice->sendSlot(FIRST_SLOT);
1855 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001856 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001857 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001858 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1859 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1860
arthurhungcc7f9802020-04-30 17:55:40 +08001861 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001862 const Point secondPoint = centerPoint + Point(100, 100);
1863 mDevice->sendSlot(SECOND_SLOT);
1864 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1865 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001866 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001867 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001868 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001869
arthurhungcc7f9802020-04-30 17:55:40 +08001870 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001871 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001872 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001873 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1875
arthurhungcc7f9802020-04-30 17:55:40 +08001876 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1877 // a palm event.
1878 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001879 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001880 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001881 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001882 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001883 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001884
arthurhungcc7f9802020-04-30 17:55:40 +08001885 // Send up to second slot, expect first slot send moving.
1886 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001887 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001888 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1889 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001890
arthurhungcc7f9802020-04-30 17:55:40 +08001891 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001892 mDevice->sendSlot(FIRST_SLOT);
1893 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001894 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001895
arthurhungcc7f9802020-04-30 17:55:40 +08001896 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1897 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001898}
1899
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001900/**
1901 * Some drivers historically have reported axis values outside of the range specified in the
1902 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1903 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1904 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1905 * and no units for pressure (resolution) is specified by the evdev documentation.
1906 */
1907TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1908 const Point centerPoint = mDevice->getCenterPoint();
1909
1910 // Down with pressure outside the reported range
1911 mDevice->sendSlot(FIRST_SLOT);
1912 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1913 mDevice->sendDown(centerPoint);
1914 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1915 mDevice->sendSync();
1916 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1917 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1918
1919 // Move to a point outside the reported range
1920 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1921 mDevice->sendSync();
1922 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1923 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1924
1925 // Up
1926 mDevice->sendUp();
1927 mDevice->sendSync();
1928 ASSERT_NO_FATAL_FAILURE(
1929 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1930}
1931
Arpit Singh440bf652023-08-09 09:23:43 +00001932TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001933 const Point centerPoint = mDevice->getCenterPoint();
1934
1935 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1936 mDevice->sendSlot(FIRST_SLOT);
1937 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1938 mDevice->sendToolType(MT_TOOL_PEN);
1939 mDevice->sendDown(centerPoint);
1940 mDevice->sendSync();
1941 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1942 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001943 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001944
1945 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1946
1947 // Release the stylus touch.
1948 mDevice->sendUp();
1949 mDevice->sendSync();
1950 ASSERT_NO_FATAL_FAILURE(
1951 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1952
1953 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1954
1955 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1956 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1957 mDevice->sendToolType(MT_TOOL_FINGER);
1958 mDevice->sendDown(centerPoint);
1959 mDevice->sendSync();
1960 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1961 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001962 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001963
1964 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1965
1966 mDevice->sendUp();
1967 mDevice->sendSync();
1968 ASSERT_NO_FATAL_FAILURE(
1969 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1970
1971 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1972 // The policy should be notified of the stylus presence.
1973 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1974 mDevice->sendToolType(MT_TOOL_PEN);
1975 mDevice->sendMove(centerPoint);
1976 mDevice->sendSync();
1977 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1978 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001979 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001980
1981 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1982}
1983
Arpit Singh440bf652023-08-09 09:23:43 +00001984TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001985 const Point centerPoint = mDevice->getCenterPoint();
1986
1987 // Down
1988 mDevice->sendSlot(FIRST_SLOT);
1989 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1990 mDevice->sendDown(centerPoint);
1991 mDevice->sendSync();
1992 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1993 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1994
1995 // Move
1996 mDevice->sendMove(centerPoint + Point(1, 1));
1997 mDevice->sendSync();
1998 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1999 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2000
2001 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2002 auto externalStylus = createUinputDevice<UinputExternalStylus>();
2003 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2004 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002005 const auto stylusInfo = waitForDevice(externalStylus->getName());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002006 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002007
2008 // Move
2009 mDevice->sendMove(centerPoint + Point(2, 2));
2010 mDevice->sendSync();
2011 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2012 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2013
2014 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2015 externalStylus.reset();
2016 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2017 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2018 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2019
2020 // Up
2021 mDevice->sendUp();
2022 mDevice->sendSync();
2023 ASSERT_NO_FATAL_FAILURE(
2024 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2025
2026 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2027}
2028
Arpit Singh440bf652023-08-09 09:23:43 +00002029INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2030 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2031 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2032 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2033
Prabir Pradhan124ea442022-10-28 20:27:44 +00002034// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002035
Prabir Pradhan124ea442022-10-28 20:27:44 +00002036// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2037// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2038// stylus.
2039template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002040class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002041protected:
2042 void SetUp() override {
2043#if !defined(__ANDROID__)
2044 GTEST_SKIP();
2045#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002046 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002047 mTouchscreen = mDevice.get();
2048 mTouchscreenInfo = mDeviceInfo;
2049
2050 setUpStylusDevice();
2051 }
2052
2053 UinputStylusDevice* mStylus{nullptr};
2054 InputDeviceInfo mStylusInfo{};
2055
2056 UinputTouchScreen* mTouchscreen{nullptr};
2057 InputDeviceInfo mTouchscreenInfo{};
2058
2059private:
2060 // When we are attempting to test stylus button events that are sent from the touchscreen,
2061 // use the same Uinput device for the touchscreen and the stylus.
2062 template <typename T = UinputStylusDevice>
2063 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2064 mStylus = mDevice.get();
2065 mStylusInfo = mDeviceInfo;
2066 }
2067
2068 // When we are attempting to stylus buttons from an external stylus being merged with touches
2069 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2070 template <typename T = UinputStylusDevice>
2071 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2072 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2073 mStylus = mStylusDeviceLifecycleTracker.get();
2074 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002076 const auto info = waitForDevice(mStylus->getName());
Prabir Pradhan124ea442022-10-28 20:27:44 +00002077 ASSERT_TRUE(info);
2078 mStylusInfo = *info;
2079 }
2080
2081 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2082
2083 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002084 using BaseTouchIntegrationTest::mDevice;
2085 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002086};
2087
2088using StylusButtonIntegrationTestTypes =
2089 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2090TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2091
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002092TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002093 const auto stylusId = TestFixture::mStylusInfo.getId();
2094
2095 TestFixture::mStylus->pressKey(BTN_STYLUS);
2096 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2097 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2098 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2099
2100 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2101 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002102 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002103 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002104}
2105
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002106TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002107 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2108 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2109 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002110
2111 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002112 TestFixture::mStylus->pressKey(BTN_STYLUS);
2113 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002114 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002115 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002116
2117 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002118 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2119 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2120 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2121 TestFixture::mTouchscreen->sendDown(centerPoint);
2122 TestFixture::mTouchscreen->sendSync();
2123 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002124 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002125 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002126 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2127 WithDeviceId(touchscreenId))));
2128 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002129 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002130 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002131 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2132 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002133
Prabir Pradhan124ea442022-10-28 20:27:44 +00002134 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2135 TestFixture::mTouchscreen->sendSync();
2136 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002137 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002138 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002139 WithDeviceId(touchscreenId))));
2140 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002141 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002142 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002143 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002144
2145 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002146 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2147 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002148 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002149 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002150}
2151
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002152TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002153 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2154 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2155 const auto stylusId = TestFixture::mStylusInfo.getId();
2156 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002157 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002158
2159 // Press the stylus button.
2160 TestFixture::mStylus->pressKey(BTN_STYLUS);
2161 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2162 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2163 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2164
2165 // Start hovering with the stylus.
2166 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2167 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2168 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2169 TestFixture::mTouchscreen->sendMove(centerPoint);
2170 TestFixture::mTouchscreen->sendSync();
2171 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2172 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2173 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2174 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2175 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2176 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2177 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2178 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2179 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2180
2181 // Touch down with the stylus.
2182 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2183 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2184 TestFixture::mTouchscreen->sendDown(centerPoint);
2185 TestFixture::mTouchscreen->sendSync();
2186 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2187 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2188 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2189
2190 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2191 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2192 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2193
2194 // Stop touching with the stylus, and start hovering.
2195 TestFixture::mTouchscreen->sendUp();
2196 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2197 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2198 TestFixture::mTouchscreen->sendMove(centerPoint);
2199 TestFixture::mTouchscreen->sendSync();
2200 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2201 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2202 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2203 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2204 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2205 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2206 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2207 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2208 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2209
2210 // Stop hovering.
2211 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2212 TestFixture::mTouchscreen->sendSync();
2213 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2214 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2215 WithButtonState(0))));
2216 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2217 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2218 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2219 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2220
2221 // Release the stylus button.
2222 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2223 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2224 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2225 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2226}
2227
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002228TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002229 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2230 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2231 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002232
2233 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002234 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2235 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2236 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2237 TestFixture::mTouchscreen->sendDown(centerPoint);
2238 TestFixture::mTouchscreen->sendSync();
2239 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002240 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002241 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002242 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002243
2244 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002245 TestFixture::mStylus->pressKey(BTN_STYLUS);
2246 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002247 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002248 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2249 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002250 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002251 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002252 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2253 WithDeviceId(touchscreenId))));
2254 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002255 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002256 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002257 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2258 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002259
Prabir Pradhan124ea442022-10-28 20:27:44 +00002260 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2261 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002262 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002263 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2264 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002265 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002266 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002267 WithDeviceId(touchscreenId))));
2268 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002269 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002270 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002271 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002272
2273 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002274 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2275 TestFixture::mTouchscreen->sendSync();
2276 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002277 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002278 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002279 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002280}
2281
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002282TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002283 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2284 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002285 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002286
2287 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2288 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2289 const auto stylusId = TestFixture::mStylusInfo.getId();
2290
2291 // Start a stylus gesture. By the time this event is processed, the configuration change that
2292 // was requested is guaranteed to be completed.
2293 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2294 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2295 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2296 TestFixture::mTouchscreen->sendDown(centerPoint);
2297 TestFixture::mTouchscreen->sendSync();
2298 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2299 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002300 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002301 WithDeviceId(touchscreenId))));
2302
2303 // Press and release a stylus button. Each change only generates a MOVE motion event.
2304 // Key events are unaffected.
2305 TestFixture::mStylus->pressKey(BTN_STYLUS);
2306 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2307 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2308 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2309 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2310 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002311 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002312 WithDeviceId(touchscreenId))));
2313
2314 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2315 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2316 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2317 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2318 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2319 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002320 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002321 WithDeviceId(touchscreenId))));
2322
2323 // Finish the stylus gesture.
2324 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2325 TestFixture::mTouchscreen->sendSync();
2326 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2327 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002328 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002329 WithDeviceId(touchscreenId))));
2330}
2331
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002332// --- ExternalStylusIntegrationTest ---
2333
2334// Verify the behavior of an external stylus. An external stylus can report pressure or button
2335// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2336// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002337using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002338
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002339TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2340 // Create an external stylus capable of reporting pressure data that
2341 // should be fused with a touch pointer.
2342 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2343 createUinputDevice<UinputExternalStylusWithPressure>();
2344 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2345 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002346 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002347 ASSERT_TRUE(stylusInfo);
2348
2349 // Connecting an external stylus changes the source of the touchscreen.
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002350 const auto deviceInfo = waitForDevice(mDevice->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002351 ASSERT_TRUE(deviceInfo);
2352 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2353}
2354
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002355TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002356 const Point centerPoint = mDevice->getCenterPoint();
2357
2358 // Create an external stylus capable of reporting pressure data that
2359 // should be fused with a touch pointer.
2360 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2361 createUinputDevice<UinputExternalStylusWithPressure>();
2362 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2363 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002364 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002365 ASSERT_TRUE(stylusInfo);
2366
2367 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2368
2369 const auto touchscreenId = mDeviceInfo.getId();
2370
2371 // Set a pressure value on the stylus. It doesn't generate any events.
2372 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2373 stylus->setPressure(100);
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2375
2376 // Start a finger gesture, and ensure it shows up as stylus gesture
2377 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002378 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002379 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002380 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002381 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002382 mDevice->sendSync();
2383 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002384 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2385 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2386 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002387
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002388 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2389 // event with the updated pressure.
2390 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002392 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2393 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2394 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002395
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002396 // The external stylus did not generate any events.
2397 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2399}
2400
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002401TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002402 const Point centerPoint = mDevice->getCenterPoint();
2403
2404 // Create an external stylus capable of reporting pressure data that
2405 // should be fused with a touch pointer.
2406 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2407 createUinputDevice<UinputExternalStylusWithPressure>();
2408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2409 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002410 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002411 ASSERT_TRUE(stylusInfo);
2412
2413 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2414
2415 const auto touchscreenId = mDeviceInfo.getId();
2416
2417 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2418 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002419 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2420 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002421 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002423
2424 // Start a finger gesture. The touch device will withhold generating any touches for
2425 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2426 mDevice->sendSlot(FIRST_SLOT);
2427 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2428 mDevice->sendToolType(MT_TOOL_FINGER);
2429 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002430 const auto syncTime = std::chrono::system_clock::now();
2431 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2432 // is generated in that period, there will be a race condition between the event being generated
2433 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2434 // will reduce the liklihood of the race condition occurring.
2435 const auto waitUntilTimeForNoEvent =
2436 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002437 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002438 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002439
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002440 // Since the external stylus did not report a pressure value within the timeout,
2441 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002442 const auto waitUntilTimeForEvent = syncTime +
2443 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2444 ASSERT_NO_FATAL_FAILURE(
2445 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2446 AMOTION_EVENT_ACTION_DOWN),
2447 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2448 AINPUT_SOURCE_STYLUS),
2449 WithToolType(ToolType::FINGER),
2450 WithDeviceId(touchscreenId),
2451 WithPressure(1.f)),
2452 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002453
2454 // Change the pressure on the external stylus. Since the pressure was not present at the start
2455 // of the gesture, it is ignored for now.
2456 stylus->setPressure(200);
2457 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2458
2459 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002460 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2461 mDevice->sendSync();
2462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2463 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002464 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002465 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002466
2467 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2468 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2469 mDevice->sendToolType(MT_TOOL_FINGER);
2470 mDevice->sendDown(centerPoint);
2471 mDevice->sendSync();
2472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002473 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2474 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2475 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002476
2477 // The external stylus did not generate any events.
2478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2479 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002480}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002481
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002482TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002483 const Point centerPoint = mDevice->getCenterPoint();
2484
2485 // Create an external stylus device that does not support pressure. It should not affect any
2486 // touch pointers.
2487 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2488 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002490 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002491 ASSERT_TRUE(stylusInfo);
2492
2493 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2494
2495 const auto touchscreenId = mDeviceInfo.getId();
2496
2497 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2498 // pressure data from the external stylus.
2499 mDevice->sendSlot(FIRST_SLOT);
2500 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2501 mDevice->sendToolType(MT_TOOL_FINGER);
2502 mDevice->sendDown(centerPoint);
2503 auto waitUntil = std::chrono::system_clock::now() +
2504 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2505 mDevice->sendSync();
2506 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002507 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2508 AMOTION_EVENT_ACTION_DOWN),
2509 WithToolType(ToolType::FINGER),
2510 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2511 AINPUT_SOURCE_STYLUS),
2512 WithButtonState(0),
2513 WithDeviceId(touchscreenId),
2514 WithPressure(1.f)),
2515 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002516
2517 // The external stylus did not generate any events.
2518 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2519 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2520}
2521
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522// --- InputDeviceTest ---
2523class InputDeviceTest : public testing::Test {
2524protected:
2525 static const char* DEVICE_NAME;
2526 static const char* DEVICE_LOCATION;
2527 static const int32_t DEVICE_ID;
2528 static const int32_t DEVICE_GENERATION;
2529 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002530 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002531 static const int32_t EVENTHUB_ID;
2532 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2533
2534 std::shared_ptr<FakeEventHub> mFakeEventHub;
2535 sp<FakeInputReaderPolicy> mFakePolicy;
2536 std::unique_ptr<TestInputListener> mFakeListener;
2537 std::unique_ptr<InstrumentedInputReader> mReader;
2538 std::shared_ptr<InputDevice> mDevice;
2539
2540 void SetUp() override {
2541 mFakeEventHub = std::make_unique<FakeEventHub>();
2542 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2543 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002544 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002546 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002547 identifier.name = DEVICE_NAME;
2548 identifier.location = DEVICE_LOCATION;
2549 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2550 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2551 identifier);
2552 mReader->pushNextDevice(mDevice);
2553 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002554 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002555 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002556
2557 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002558 mFakeListener.reset();
2559 mFakePolicy.clear();
2560 }
2561};
2562
2563const char* InputDeviceTest::DEVICE_NAME = "device";
2564const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2565const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2566const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002568const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2569 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002570const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002571const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2572
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002573TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002575 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2576 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002577}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002578
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2580 ASSERT_EQ(mDevice->isEnabled(), false);
2581}
2582
2583TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2584 // Configuration.
2585 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002586 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002587
2588 // Reset.
2589 unused += mDevice->reset(ARBITRARY_TIME);
2590
2591 NotifyDeviceResetArgs resetArgs;
2592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2593 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2594 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2595
2596 // Metadata.
2597 ASSERT_TRUE(mDevice->isIgnored());
2598 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2599
2600 InputDeviceInfo info = mDevice->getDeviceInfo();
2601 ASSERT_EQ(DEVICE_ID, info.getId());
2602 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2603 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2604 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2605
2606 // State queries.
2607 ASSERT_EQ(0, mDevice->getMetaState());
2608
2609 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2610 << "Ignored device should return unknown key code state.";
2611 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2612 << "Ignored device should return unknown scan code state.";
2613 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2614 << "Ignored device should return unknown switch state.";
2615
2616 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2617 uint8_t flags[2] = { 0, 1 };
2618 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2619 << "Ignored device should never mark any key codes.";
2620 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2621 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2622}
2623
2624TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2625 // Configuration.
2626 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2627
2628 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002629 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2630 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002631 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2632 mapper1.setMetaState(AMETA_ALT_ON);
2633 mapper1.addSupportedKeyCode(AKEYCODE_A);
2634 mapper1.addSupportedKeyCode(AKEYCODE_B);
2635 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2636 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2637 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2638 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2639 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2640
2641 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002642 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2643 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 mapper2.setMetaState(AMETA_SHIFT_ON);
2645
2646 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002647 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648
Harry Cuttsf13161a2023-03-08 14:15:49 +00002649 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2650 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002651 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002652 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002653
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002654 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2655 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656
2657 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002658 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002659 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2660 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661
2662 NotifyDeviceResetArgs resetArgs;
2663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2664 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2665 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2666
2667 // Metadata.
2668 ASSERT_FALSE(mDevice->isIgnored());
2669 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2670
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002671 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002672 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002673 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2675 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2676
2677 // State queries.
2678 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2679 << "Should query mappers and combine meta states.";
2680
2681 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2682 << "Should return unknown key code state when source not supported.";
2683 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2684 << "Should return unknown scan code state when source not supported.";
2685 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2686 << "Should return unknown switch state when source not supported.";
2687
2688 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2689 << "Should query mapper when source is supported.";
2690 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2691 << "Should query mapper when source is supported.";
2692 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2693 << "Should query mapper when source is supported.";
2694
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002695 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002697 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698 << "Should do nothing when source is unsupported.";
2699 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2700 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2701 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2702 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2703
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002704 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002705 << "Should query mapper when source is supported.";
2706 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2707 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2708 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2709 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2710
2711 // Event handling.
2712 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002713 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002714 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002716 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2717 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002718}
2719
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002720TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2721 // Set some behavior to force the configuration to be update.
2722 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2723 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2724 AINPUT_SOURCE_KEYBOARD);
2725
2726 std::list<NotifyArgs> unused =
2727 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2728 /*changes=*/{});
2729
2730 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2731}
2732
2733TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2734 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2735 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2736 AINPUT_SOURCE_KEYBOARD);
2737
2738 std::list<NotifyArgs> unused =
2739 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2740 /*changes=*/{});
2741
2742 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2743}
2744
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002745TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2746 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2747 FakeInputMapper& mapper =
2748 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2749 AINPUT_SOURCE_KEYBOARD);
2750 NotifyMotionArgs args1;
2751 NotifySwitchArgs args2;
2752 NotifyKeyArgs args3;
2753 mapper.setProcessResult({args1, args2, args3});
2754
2755 InputReaderConfiguration config;
2756 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2757
2758 RawEvent event;
2759 event.deviceId = EVENTHUB_ID;
2760 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2761
2762 for (auto& arg : notifyArgs) {
2763 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2764 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2765 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2766 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2767 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2768 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2769 }
2770 }
2771}
2772
2773TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2774 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2775 FakeInputMapper& mapper =
2776 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2777 AINPUT_SOURCE_KEYBOARD);
2778 NotifyMotionArgs args;
2779 mapper.setProcessResult({args});
2780
2781 InputReaderConfiguration config;
2782 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2783
2784 RawEvent event;
2785 event.deviceId = EVENTHUB_ID;
2786 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2787
2788 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2789 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2790}
2791
2792TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2793 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2794 FakeInputMapper& mapper =
2795 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2796 AINPUT_SOURCE_KEYBOARD);
2797 NotifyMotionArgs args;
2798 args.policyFlags = POLICY_FLAG_WAKE;
2799 mapper.setProcessResult({args});
2800
2801 InputReaderConfiguration config;
2802 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2803
2804 RawEvent event;
2805 event.deviceId = EVENTHUB_ID;
2806 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2807
2808 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2809 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2810}
2811
Arthur Hung2c9a3342019-07-23 14:18:59 +08002812// A single input device is associated with a specific display. Check that:
2813// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002814// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002815TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002816 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2817 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002818
2819 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002820 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002821 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2822 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002823
2824 // Device should be enabled by default.
2825 ASSERT_TRUE(mDevice->isEnabled());
2826
2827 // Prepare associated info.
2828 constexpr uint8_t hdmi = 1;
2829 const std::string UNIQUE_ID = "local:1";
2830
2831 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002832 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002833 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002834 // Device should be disabled because it is associated with a specific display via
2835 // input port <-> display port association, but the corresponding display is not found
2836 ASSERT_FALSE(mDevice->isEnabled());
2837
2838 // Prepare displays.
2839 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002840 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002841 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002842 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002843 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002844 ASSERT_TRUE(mDevice->isEnabled());
2845
2846 // Device should be disabled after set disable.
2847 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002848 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002849 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002850 ASSERT_FALSE(mDevice->isEnabled());
2851
2852 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002853 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002854 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002855 ASSERT_FALSE(mDevice->isEnabled());
2856}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857
Christine Franks1ba71cc2021-04-07 14:37:42 -07002858TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2859 // Device should be enabled by default.
2860 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002861 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2862 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002863 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002864 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2865 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002866 ASSERT_TRUE(mDevice->isEnabled());
2867
2868 // Device should be disabled because it is associated with a specific display, but the
2869 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002870 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002871 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002872 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002873 ASSERT_FALSE(mDevice->isEnabled());
2874
2875 // Device should be enabled when a display is found.
2876 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002877 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002878 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002879 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002880 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002881 ASSERT_TRUE(mDevice->isEnabled());
2882
2883 // Device should be disabled after set disable.
2884 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002885 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002886 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002887 ASSERT_FALSE(mDevice->isEnabled());
2888
2889 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002890 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002891 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002892 ASSERT_FALSE(mDevice->isEnabled());
2893}
2894
Christine Franks2a2293c2022-01-18 11:51:16 -08002895TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2896 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002897 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2898 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002899 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002900 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2901 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002902
Christine Franks2a2293c2022-01-18 11:51:16 -08002903 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2904 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002905 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002906 NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002907 const auto initialGeneration = mDevice->getGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002908 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002909 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002910 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002911 ASSERT_GT(mDevice->getGeneration(), initialGeneration);
2912 ASSERT_EQ(mDevice->getDeviceInfo().getAssociatedDisplayId(), SECONDARY_DISPLAY_ID);
Christine Franks2a2293c2022-01-18 11:51:16 -08002913}
2914
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002915/**
2916 * This test reproduces a crash caused by a dangling reference that remains after device is added
2917 * and removed. The reference is accessed in InputDevice::dump(..);
2918 */
2919TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2920 constexpr int32_t TEST_EVENTHUB_ID = 10;
2921 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2922
Harry Cutts33476232023-01-30 19:57:29 +00002923 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002924 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2925 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002926 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2927 std::string dumpStr, eventHubDevStr;
2928 device.dump(dumpStr, eventHubDevStr);
2929}
2930
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002931TEST_F(InputDeviceTest, GetBluetoothAddress) {
2932 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2933 ASSERT_TRUE(address);
2934 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2935}
2936
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002937TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2938 mFakePolicy->clearViewports();
2939 FakeInputMapper& mapper =
2940 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2941 AINPUT_SOURCE_KEYBOARD);
2942 std::list<NotifyArgs> unused =
2943 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2944 /*changes=*/{});
2945
2946 mapper.assertConfigureWasCalled();
2947 mapper.assertResetWasNotCalled();
2948
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002949 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002950 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002951 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002952 .type = EV_SYN,
2953 .code = SYN_REPORT,
2954 .value = 0};
2955
2956 // Events are processed normally.
2957 unused = mDevice->process(&event, /*count=*/1);
2958 mapper.assertProcessWasCalled();
2959
2960 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002961 event.type = EV_SYN;
2962 event.code = SYN_DROPPED;
2963 event.value = 0;
2964 unused = mDevice->process(&event, /*count=*/1);
2965 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002966
2967 // All events until the next SYN_REPORT should be dropped.
2968 event.type = EV_KEY;
2969 event.code = KEY_A;
2970 event.value = 1;
2971 unused = mDevice->process(&event, /*count=*/1);
2972 mapper.assertProcessWasNotCalled();
2973
2974 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00002975 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002976 event.type = EV_SYN;
2977 event.code = SYN_REPORT;
2978 event.value = 0;
2979 unused = mDevice->process(&event, /*count=*/1);
2980 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00002981 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002982
2983 // The mapper receives events normally now.
2984 event.type = EV_KEY;
2985 event.code = KEY_B;
2986 event.value = 1;
2987 unused = mDevice->process(&event, /*count=*/1);
2988 mapper.assertProcessWasCalled();
2989}
2990
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991// --- SwitchInputMapperTest ---
2992
2993class SwitchInputMapperTest : public InputMapperTest {
2994protected:
2995};
2996
2997TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002998 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002999
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003000 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001}
3002
3003TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003004 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003006 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003007 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003008
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003009 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003010 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011}
3012
3013TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003014 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003015 std::list<NotifyArgs> out;
3016 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3017 ASSERT_TRUE(out.empty());
3018 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3019 ASSERT_TRUE(out.empty());
3020 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3021 ASSERT_TRUE(out.empty());
3022 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003023
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003024 ASSERT_EQ(1u, out.size());
3025 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003027 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3028 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003029 args.switchMask);
3030 ASSERT_EQ(uint32_t(0), args.policyFlags);
3031}
3032
Chris Ye87143712020-11-10 05:05:58 +00003033// --- VibratorInputMapperTest ---
3034class VibratorInputMapperTest : public InputMapperTest {
3035protected:
3036 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3037};
3038
3039TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003040 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003041
3042 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3043}
3044
3045TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003046 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003047
3048 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3049}
3050
3051TEST_F(VibratorInputMapperTest, Vibrate) {
3052 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003053 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003054 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003055
3056 VibrationElement pattern(2);
3057 VibrationSequence sequence(2);
3058 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003059 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3060 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003061 sequence.addElement(pattern);
3062 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003063 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3064 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003065 sequence.addElement(pattern);
3066
3067 std::vector<int64_t> timings = {0, 1};
3068 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3069
3070 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003071 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003072 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003073 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003074 // Verify vibrator state listener was notified.
3075 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003076 ASSERT_EQ(1u, out.size());
3077 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3078 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3079 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003080 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003081 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003082 ASSERT_FALSE(mapper.isVibrating());
3083 // Verify vibrator state listener was notified.
3084 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003085 ASSERT_EQ(1u, out.size());
3086 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3087 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3088 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003089}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003090
Chris Yef59a2f42020-10-16 12:55:26 -07003091// --- SensorInputMapperTest ---
3092
3093class SensorInputMapperTest : public InputMapperTest {
3094protected:
3095 static const int32_t ACCEL_RAW_MIN;
3096 static const int32_t ACCEL_RAW_MAX;
3097 static const int32_t ACCEL_RAW_FUZZ;
3098 static const int32_t ACCEL_RAW_FLAT;
3099 static const int32_t ACCEL_RAW_RESOLUTION;
3100
3101 static const int32_t GYRO_RAW_MIN;
3102 static const int32_t GYRO_RAW_MAX;
3103 static const int32_t GYRO_RAW_FUZZ;
3104 static const int32_t GYRO_RAW_FLAT;
3105 static const int32_t GYRO_RAW_RESOLUTION;
3106
3107 static const float GRAVITY_MS2_UNIT;
3108 static const float DEGREE_RADIAN_UNIT;
3109
3110 void prepareAccelAxes();
3111 void prepareGyroAxes();
3112 void setAccelProperties();
3113 void setGyroProperties();
3114 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3115};
3116
3117const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3118const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3119const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3120const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3121const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3122
3123const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3124const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3125const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3126const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3127const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3128
3129const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3130const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3131
3132void SensorInputMapperTest::prepareAccelAxes() {
3133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3134 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3136 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3138 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3139}
3140
3141void SensorInputMapperTest::prepareGyroAxes() {
3142 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3143 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3144 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3145 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3146 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3147 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3148}
3149
3150void SensorInputMapperTest::setAccelProperties() {
3151 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3152 /* sensorDataIndex */ 0);
3153 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3154 /* sensorDataIndex */ 1);
3155 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3156 /* sensorDataIndex */ 2);
3157 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3158 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3159 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3160 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3161 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3162}
3163
3164void SensorInputMapperTest::setGyroProperties() {
3165 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3166 /* sensorDataIndex */ 0);
3167 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3168 /* sensorDataIndex */ 1);
3169 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3170 /* sensorDataIndex */ 2);
3171 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3172 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3173 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3174 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3175 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3176}
3177
3178TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003179 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003180
3181 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3182}
3183
3184TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3185 setAccelProperties();
3186 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003187 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003188
3189 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3190 std::chrono::microseconds(10000),
3191 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003192 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003198
3199 NotifySensorArgs args;
3200 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3201 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3202 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3203
3204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3205 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3206 ASSERT_EQ(args.deviceId, DEVICE_ID);
3207 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3208 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3209 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3210 ASSERT_EQ(args.values, values);
3211 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3212}
3213
3214TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3215 setGyroProperties();
3216 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003217 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003218
3219 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3220 std::chrono::microseconds(10000),
3221 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003222 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003228
3229 NotifySensorArgs args;
3230 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3231 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3232 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3233
3234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3235 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3236 ASSERT_EQ(args.deviceId, DEVICE_ID);
3237 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3238 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3239 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3240 ASSERT_EQ(args.values, values);
3241 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3242}
3243
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244// --- KeyboardInputMapperTest ---
3245
3246class KeyboardInputMapperTest : public InputMapperTest {
3247protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003248 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003249 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003250 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003251
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003253 int32_t originalKeyCode, int32_t rotatedKeyCode,
3254 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255};
3256
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003257/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3258 * orientation.
3259 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003260void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003261 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3262 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003263}
3264
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003266 int32_t originalScanCode, int32_t originalKeyCode,
3267 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003268 NotifyKeyArgs args;
3269
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3272 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3273 ASSERT_EQ(originalScanCode, args.scanCode);
3274 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003275 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003277 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3280 ASSERT_EQ(originalScanCode, args.scanCode);
3281 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003282 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283}
3284
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003286 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003287 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003290 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003291}
3292
3293TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3294 const int32_t USAGE_A = 0x070004;
3295 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003296 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3297 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003298 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3299 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3300 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003302 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003303 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003304 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003305 // Initial metastate is AMETA_NONE.
3306 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003307
3308 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003310 NotifyKeyArgs args;
3311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3312 ASSERT_EQ(DEVICE_ID, args.deviceId);
3313 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3314 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3315 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3316 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3317 ASSERT_EQ(KEY_HOME, args.scanCode);
3318 ASSERT_EQ(AMETA_NONE, args.metaState);
3319 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3320 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3321 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3322
3323 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003324 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3326 ASSERT_EQ(DEVICE_ID, args.deviceId);
3327 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3328 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3329 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3330 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3331 ASSERT_EQ(KEY_HOME, args.scanCode);
3332 ASSERT_EQ(AMETA_NONE, args.metaState);
3333 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3334 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3335 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3336
3337 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3341 ASSERT_EQ(DEVICE_ID, args.deviceId);
3342 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3343 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3344 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3345 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3346 ASSERT_EQ(0, args.scanCode);
3347 ASSERT_EQ(AMETA_NONE, args.metaState);
3348 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3349 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3350 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3351
3352 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003353 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3354 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3356 ASSERT_EQ(DEVICE_ID, args.deviceId);
3357 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3358 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3359 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3360 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3361 ASSERT_EQ(0, args.scanCode);
3362 ASSERT_EQ(AMETA_NONE, args.metaState);
3363 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3364 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3365 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3366
3367 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3371 ASSERT_EQ(DEVICE_ID, args.deviceId);
3372 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3373 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3374 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3375 ASSERT_EQ(0, args.keyCode);
3376 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3377 ASSERT_EQ(AMETA_NONE, args.metaState);
3378 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3379 ASSERT_EQ(0U, args.policyFlags);
3380 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3381
3382 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3384 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3386 ASSERT_EQ(DEVICE_ID, args.deviceId);
3387 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3388 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3389 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3390 ASSERT_EQ(0, args.keyCode);
3391 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3392 ASSERT_EQ(AMETA_NONE, args.metaState);
3393 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3394 ASSERT_EQ(0U, args.policyFlags);
3395 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3396}
3397
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003398TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3399 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3400 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3401 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3402
3403 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003404 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003405 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3406
3407 // Key down by scan code.
3408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3409 NotifyKeyArgs args;
3410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3411 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3412
3413 // Key up by scan code.
3414 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3416 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3417}
3418
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003419/**
3420 * Ensure that the readTime is set to the time when the EV_KEY is received.
3421 */
3422TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3423 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3424
3425 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003426 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003427 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3428 NotifyKeyArgs args;
3429
3430 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003431 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3433 ASSERT_EQ(12, args.readTime);
3434
3435 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003436 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3438 ASSERT_EQ(15, args.readTime);
3439}
3440
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003442 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3443 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003444 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3445 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3446 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003448 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003449 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003450 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451
Arthur Hung95f68612022-04-07 14:08:22 +08003452 // Initial metastate is AMETA_NONE.
3453 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454
3455 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457 NotifyKeyArgs args;
3458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3459 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003460 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003461 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462
3463 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003464 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3466 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003467 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468
3469 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003470 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3472 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003473 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474
3475 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003476 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3478 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003479 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003480 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481}
3482
3483TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003484 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3485 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3486 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3487 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003489 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003490 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003491 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003492
Michael Wrighta9cf4192022-12-01 23:46:39 +00003493 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003494 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3495 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3496 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3497 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3498 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3499 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3500 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3501 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3502}
3503
3504TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003505 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3506 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3507 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3508 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
Michael Wrightd02c5b62014-02-10 15:10:22 -08003510 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003511 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003512 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003513 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003514
Michael Wrighta9cf4192022-12-01 23:46:39 +00003515 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003516 ASSERT_NO_FATAL_FAILURE(
3517 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3518 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3519 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3520 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3521 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3522 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3523 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003525 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003526 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003527 ASSERT_NO_FATAL_FAILURE(
3528 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3529 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3530 AKEYCODE_DPAD_UP, DISPLAY_ID));
3531 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3532 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3533 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3534 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003536 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003537 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003538 ASSERT_NO_FATAL_FAILURE(
3539 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3540 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3541 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3542 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3543 AKEYCODE_DPAD_UP, DISPLAY_ID));
3544 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3545 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003547 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003548 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003549 ASSERT_NO_FATAL_FAILURE(
3550 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3551 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3552 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3553 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3554 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3555 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3556 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557
3558 // Special case: if orientation changes while key is down, we still emit the same keycode
3559 // in the key up as we did in the key down.
3560 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003561 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003562 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3565 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3566 ASSERT_EQ(KEY_UP, args.scanCode);
3567 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3568
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003569 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003570 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3573 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3574 ASSERT_EQ(KEY_UP, args.scanCode);
3575 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3576}
3577
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003578TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3579 // If the keyboard is not orientation aware,
3580 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003581 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003582
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003583 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003584 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003585 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003586 NotifyKeyArgs args;
3587
3588 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3593 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3594
Michael Wrighta9cf4192022-12-01 23:46:39 +00003595 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3600 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3601}
3602
3603TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3604 // If the keyboard is orientation aware,
3605 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003606 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003607
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003608 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003609 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003610 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003611 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003612 NotifyKeyArgs args;
3613
3614 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3615 // ^--- already checked by the previous test
3616
Michael Wrighta9cf4192022-12-01 23:46:39 +00003617 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003618 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003619 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3623 ASSERT_EQ(DISPLAY_ID, args.displayId);
3624
3625 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003626 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003627 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003628 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003629 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3633 ASSERT_EQ(newDisplayId, args.displayId);
3634}
3635
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003637 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003638 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003639 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003641 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003642 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003644 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003645 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003646}
3647
Philip Junker4af3b3d2021-12-14 10:36:55 +01003648TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3649 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003650 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003651 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3652
3653 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3654 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3655 << "If a mapping is available, the result is equal to the mapping";
3656
3657 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3658 << "If no mapping is available, the result is the key location";
3659}
3660
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003662 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003663 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003664 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003666 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003667 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003669 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003670 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671}
3672
3673TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003674 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003675 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003676 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003677
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003678 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003679
Michael Wrightd02c5b62014-02-10 15:10:22 -08003680 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003681 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682 ASSERT_TRUE(flags[0]);
3683 ASSERT_FALSE(flags[1]);
3684}
3685
3686TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003687 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3688 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3689 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3691 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3692 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003694 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003695 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003697 // Initial metastate is AMETA_NONE.
3698 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699
3700 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003701 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3702 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3703 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704
3705 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003708 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3709 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3710 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003711 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003712
3713 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003716 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3717 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3718 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003719 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003720
3721 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003724 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3725 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3726 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728
3729 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003732 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3733 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3734 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003735 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003736
3737 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003740 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3741 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3742 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003743 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
3745 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3749 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3750 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003751 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752}
3753
Chris Yea52ade12020-08-27 16:49:20 -07003754TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3755 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3756 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3757 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3758 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3759
3760 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003761 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003762 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3763
Chris Yea52ade12020-08-27 16:49:20 -07003764 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003765 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003766 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3767 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3768 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3769 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3770
3771 NotifyKeyArgs args;
3772 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003773 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3775 ASSERT_EQ(AMETA_NONE, args.metaState);
3776 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3777 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3778 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3779
3780 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003781 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3783 ASSERT_EQ(AMETA_NONE, args.metaState);
3784 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3785 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3786 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3787}
3788
Arthur Hung2c9a3342019-07-23 14:18:59 +08003789TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3790 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003791 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3792 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3793 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3794 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003795
3796 // keyboard 2.
3797 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003798 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003799 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003800 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003801 std::shared_ptr<InputDevice> device2 =
3802 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003803 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003805 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3806 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3807 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3808 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003809
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003810 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003811 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003812 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003813
Arpit Singh67ca6842023-04-26 14:43:16 +00003814 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003815 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003816 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3817 mFakePolicy
3818 ->getReaderConfiguration(),
3819 AINPUT_SOURCE_KEYBOARD,
3820 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003821 std::list<NotifyArgs> unused =
3822 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003823 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003824 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003825
3826 // Prepared displays and associated info.
3827 constexpr uint8_t hdmi1 = 0;
3828 constexpr uint8_t hdmi2 = 1;
3829 const std::string SECONDARY_UNIQUE_ID = "local:1";
3830
3831 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3832 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3833
3834 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003835 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003836 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003837 ASSERT_FALSE(device2->isEnabled());
3838
3839 // Prepare second display.
3840 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003841 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003842 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003843 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003844 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003845 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003846 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003847 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003848
3849 // Device should be enabled after the associated display is found.
3850 ASSERT_TRUE(mDevice->isEnabled());
3851 ASSERT_TRUE(device2->isEnabled());
3852
3853 // Test pad key events
3854 ASSERT_NO_FATAL_FAILURE(
3855 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3856 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3857 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3858 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3859 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3860 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3861 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3862
3863 ASSERT_NO_FATAL_FAILURE(
3864 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3865 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3866 AKEYCODE_DPAD_RIGHT, newDisplayId));
3867 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3868 AKEYCODE_DPAD_DOWN, newDisplayId));
3869 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3870 AKEYCODE_DPAD_LEFT, newDisplayId));
3871}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003872
arthurhungc903df12020-08-11 15:08:42 +08003873TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3874 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3875 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3876 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3877 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3878 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3879 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3880
3881 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003882 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003883 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003884 // Initial metastate is AMETA_NONE.
3885 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003886
3887 // Initialization should have turned all of the lights off.
3888 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3889 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3890 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3891
3892 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003895 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3896 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3897
3898 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003901 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3902 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3903
3904 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003907 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3908 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3909
3910 mFakeEventHub->removeDevice(EVENTHUB_ID);
3911 mReader->loopOnce();
3912
3913 // keyboard 2 should default toggle keys.
3914 const std::string USB2 = "USB2";
3915 const std::string DEVICE_NAME2 = "KEYBOARD2";
3916 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3917 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3918 std::shared_ptr<InputDevice> device2 =
3919 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003920 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003921 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3922 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3923 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3924 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3925 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3926 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3927
Arpit Singh67ca6842023-04-26 14:43:16 +00003928 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003929 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003930 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3931 mFakePolicy
3932 ->getReaderConfiguration(),
3933 AINPUT_SOURCE_KEYBOARD,
3934 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003935 std::list<NotifyArgs> unused =
3936 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003937 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003938 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003939
3940 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3941 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3942 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003943 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3944 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003945}
3946
Arthur Hungcb40a002021-08-03 14:31:01 +00003947TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3948 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3949 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3950 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3951
3952 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003953 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003954 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3955 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003956 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003957 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003958 // Initial metastate is AMETA_NONE.
3959 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003960
3961 mReader->toggleCapsLockState(DEVICE_ID);
3962 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3963}
3964
Arthur Hungfb3cc112022-04-13 07:39:50 +00003965TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3966 // keyboard 1.
3967 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3968 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3969 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3970 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3971 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3972 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3973
3974 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003975 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003976 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3977
3978 // keyboard 2.
3979 const std::string USB2 = "USB2";
3980 const std::string DEVICE_NAME2 = "KEYBOARD2";
3981 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3982 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3983 std::shared_ptr<InputDevice> device2 =
3984 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3985 ftl::Flags<InputDeviceClass>(0));
3986 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3987 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3988 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3989 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3990 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3991 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3992
Arpit Singh67ca6842023-04-26 14:43:16 +00003993 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003994 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003995 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3996 mFakePolicy
3997 ->getReaderConfiguration(),
3998 AINPUT_SOURCE_KEYBOARD,
3999 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004000 std::list<NotifyArgs> unused =
4001 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004002 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004003 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004004
Arthur Hung95f68612022-04-07 14:08:22 +08004005 // Initial metastate is AMETA_NONE.
4006 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4007 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4008
4009 // Toggle num lock on and off.
4010 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4011 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004012 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4013 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4014 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4015
4016 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4017 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4018 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4019 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4020 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4021
4022 // Toggle caps lock on and off.
4023 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4024 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4025 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4026 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4027 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4028
4029 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4030 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4031 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4032 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4033 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4034
4035 // Toggle scroll lock on and off.
4036 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4037 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4038 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4039 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4040 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4041
4042 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4043 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4044 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4045 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4046 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4047}
4048
Arthur Hung2141d542022-08-23 07:45:21 +00004049TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4050 const int32_t USAGE_A = 0x070004;
4051 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4052 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4053
4054 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004055 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004056 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4057 // Key down by scan code.
4058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4059 NotifyKeyArgs args;
4060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4061 ASSERT_EQ(DEVICE_ID, args.deviceId);
4062 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4063 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4064 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4065 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4066 ASSERT_EQ(KEY_HOME, args.scanCode);
4067 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4068
4069 // Disable device, it should synthesize cancellation events for down events.
4070 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004071 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004072
4073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4074 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4075 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4076 ASSERT_EQ(KEY_HOME, args.scanCode);
4077 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4078}
4079
Zixuan Qufecb6062022-11-12 04:44:31 +00004080TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004081 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4082 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004083 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004084 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4085 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004086
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004087 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004088 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4089
4090 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004091 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004092
4093 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4094 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4095 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4096 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4097 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004098 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4099
4100 // Call change layout association with the same values: Generation shouldn't change
4101 generation = mReader->getContext()->getGeneration();
4102 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4103 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4104 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4105 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004106}
4107
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004108TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4109 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4110 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4111
4112 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004113 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004114 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4115 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004116 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004117
4118 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4119 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4120}
4121
Justin Chung71ddb432023-03-27 04:29:07 +00004122TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4124 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004125 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004126 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4127 NotifyKeyArgs args;
4128
4129 // Key down
4130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4132 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4133}
4134
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004135// --- KeyboardInputMapperTest_ExternalDevice ---
4136
4137class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4138protected:
Chris Yea52ade12020-08-27 16:49:20 -07004139 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004140};
4141
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004142TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004143 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4144 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004145
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004146 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4147 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4148 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4149 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004151 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004152 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004153 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004154
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004156 NotifyKeyArgs args;
4157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4158 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4159
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004160 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4162 ASSERT_EQ(uint32_t(0), args.policyFlags);
4163
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004166 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004167
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004168 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4170 ASSERT_EQ(uint32_t(0), args.policyFlags);
4171
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4174 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4175
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004176 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4178 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4179}
4180
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004181TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_NoneAlphabeticKeyboard) {
4182 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4183 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4184
4185 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4186 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4187 POLICY_FLAG_WAKE);
4188
4189 KeyboardInputMapper& mapper =
4190 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4191 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4192
4193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4194 NotifyKeyArgs args;
4195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4196 ASSERT_EQ(uint32_t(0), args.policyFlags);
4197
4198 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4200 ASSERT_EQ(uint32_t(0), args.policyFlags);
4201
4202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4204 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4205
4206 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4208 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4209}
4210
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004211TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004212 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004213
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004214 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4215 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4216 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004217
Powei Fengd041c5d2019-05-03 17:11:33 -07004218 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004219 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004220 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004222
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004224 NotifyKeyArgs args;
4225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4226 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4227
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004228 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4230 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4231
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4234 ASSERT_EQ(uint32_t(0), args.policyFlags);
4235
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004236 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4238 ASSERT_EQ(uint32_t(0), args.policyFlags);
4239
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4242 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4243
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004244 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4246 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4247}
4248
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249// --- TouchInputMapperTest ---
4250
4251class TouchInputMapperTest : public InputMapperTest {
4252protected:
4253 static const int32_t RAW_X_MIN;
4254 static const int32_t RAW_X_MAX;
4255 static const int32_t RAW_Y_MIN;
4256 static const int32_t RAW_Y_MAX;
4257 static const int32_t RAW_TOUCH_MIN;
4258 static const int32_t RAW_TOUCH_MAX;
4259 static const int32_t RAW_TOOL_MIN;
4260 static const int32_t RAW_TOOL_MAX;
4261 static const int32_t RAW_PRESSURE_MIN;
4262 static const int32_t RAW_PRESSURE_MAX;
4263 static const int32_t RAW_ORIENTATION_MIN;
4264 static const int32_t RAW_ORIENTATION_MAX;
4265 static const int32_t RAW_DISTANCE_MIN;
4266 static const int32_t RAW_DISTANCE_MAX;
4267 static const int32_t RAW_TILT_MIN;
4268 static const int32_t RAW_TILT_MAX;
4269 static const int32_t RAW_ID_MIN;
4270 static const int32_t RAW_ID_MAX;
4271 static const int32_t RAW_SLOT_MIN;
4272 static const int32_t RAW_SLOT_MAX;
4273 static const float X_PRECISION;
4274 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004275 static const float X_PRECISION_VIRTUAL;
4276 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004277
4278 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004279 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280
4281 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4282
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004283 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004284 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004285
Michael Wrightd02c5b62014-02-10 15:10:22 -08004286 enum Axes {
4287 POSITION = 1 << 0,
4288 TOUCH = 1 << 1,
4289 TOOL = 1 << 2,
4290 PRESSURE = 1 << 3,
4291 ORIENTATION = 1 << 4,
4292 MINOR = 1 << 5,
4293 ID = 1 << 6,
4294 DISTANCE = 1 << 7,
4295 TILT = 1 << 8,
4296 SLOT = 1 << 9,
4297 TOOL_TYPE = 1 << 10,
4298 };
4299
Michael Wrighta9cf4192022-12-01 23:46:39 +00004300 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004301 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004302 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004303 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004304 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 int32_t toRawX(float displayX);
4306 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004307 int32_t toRotatedRawX(float displayX);
4308 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004309 float toCookedX(float rawX, float rawY);
4310 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004311 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004312 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004314 float toDisplayY(int32_t rawY, int32_t displayHeight);
4315
Michael Wrightd02c5b62014-02-10 15:10:22 -08004316};
4317
4318const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4319const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4320const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4321const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4322const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4323const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4324const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4325const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004326const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4327const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4329const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4330const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4331const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4332const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4333const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4334const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4335const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4336const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4337const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4338const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4339const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004340const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4341 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4342const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4343 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004344const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4345 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004346
4347const float TouchInputMapperTest::GEOMETRIC_SCALE =
4348 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4349 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4350
4351const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4352 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4353 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4354};
4355
Michael Wrighta9cf4192022-12-01 23:46:39 +00004356void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004357 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4358 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004359}
4360
4361void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4362 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004363 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364}
4365
Michael Wrighta9cf4192022-12-01 23:46:39 +00004366void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004367 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4368 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4369 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004370}
4371
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004373 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4374 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4375 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4376 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377}
4378
Jason Gerecke489fda82012-09-07 17:19:40 -07004379void TouchInputMapperTest::prepareLocationCalibration() {
4380 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4381}
4382
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383int32_t TouchInputMapperTest::toRawX(float displayX) {
4384 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4385}
4386
4387int32_t TouchInputMapperTest::toRawY(float displayY) {
4388 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4389}
4390
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004391int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4392 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4393}
4394
4395int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4396 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4397}
4398
Jason Gerecke489fda82012-09-07 17:19:40 -07004399float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4400 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4401 return rawX;
4402}
4403
4404float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4405 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4406 return rawY;
4407}
4408
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004410 return toDisplayX(rawX, DISPLAY_WIDTH);
4411}
4412
4413float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4414 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415}
4416
4417float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004418 return toDisplayY(rawY, DISPLAY_HEIGHT);
4419}
4420
4421float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4422 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423}
4424
4425
4426// --- SingleTouchInputMapperTest ---
4427
4428class SingleTouchInputMapperTest : public TouchInputMapperTest {
4429protected:
4430 void prepareButtons();
4431 void prepareAxes(int axes);
4432
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004433 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4434 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4435 void processUp(SingleTouchInputMapper& mappery);
4436 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4437 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4438 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4439 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4440 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4441 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442};
4443
4444void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004445 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446}
4447
4448void SingleTouchInputMapperTest::prepareAxes(int axes) {
4449 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004450 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4451 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004452 }
4453 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004454 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4455 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004456 }
4457 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004458 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4459 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004460 }
4461 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004462 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4463 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464 }
4465 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004466 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4467 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468 }
4469}
4470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004471void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475}
4476
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004477void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480}
4481
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004482void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004483 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484}
4485
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004486void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488}
4489
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004490void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4491 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004492 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493}
4494
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004495void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497}
4498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004499void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4500 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503}
4504
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004505void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4506 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004507 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508}
4509
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004510void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512}
4513
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515 prepareButtons();
4516 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004517 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518
Josep del Río2d8c79a2023-01-23 19:33:50 +00004519 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520}
4521
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 prepareButtons();
4524 prepareAxes(POSITION);
4525 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004526 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004528 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004529}
4530
4531TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004533 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 prepareButtons();
4535 prepareAxes(POSITION);
4536 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004537 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538
4539 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004540 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541
4542 // Virtual key is down.
4543 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4544 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4545 processDown(mapper, x, y);
4546 processSync(mapper);
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004549 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550
4551 // Virtual key is up.
4552 processUp(mapper);
4553 processSync(mapper);
4554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4555
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004556 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004557}
4558
4559TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004561 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562 prepareButtons();
4563 prepareAxes(POSITION);
4564 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004565 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004566
4567 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004568 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569
4570 // Virtual key is down.
4571 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4572 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4573 processDown(mapper, x, y);
4574 processSync(mapper);
4575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004577 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578
4579 // Virtual key is up.
4580 processUp(mapper);
4581 processSync(mapper);
4582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4583
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004584 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585}
4586
4587TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004589 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590 prepareButtons();
4591 prepareAxes(POSITION);
4592 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004593 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004596 ASSERT_TRUE(
4597 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 ASSERT_TRUE(flags[0]);
4599 ASSERT_FALSE(flags[1]);
4600}
4601
4602TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004604 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605 prepareButtons();
4606 prepareAxes(POSITION);
4607 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004608 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004609
arthurhungdcef2dc2020-08-11 14:47:50 +08004610 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611
4612 NotifyKeyArgs args;
4613
4614 // Press virtual key.
4615 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4616 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4617 processDown(mapper, x, y);
4618 processSync(mapper);
4619
4620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4621 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4622 ASSERT_EQ(DEVICE_ID, args.deviceId);
4623 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4624 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4625 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4626 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4627 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4628 ASSERT_EQ(KEY_HOME, args.scanCode);
4629 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4630 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4631
4632 // Release virtual key.
4633 processUp(mapper);
4634 processSync(mapper);
4635
4636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4637 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4638 ASSERT_EQ(DEVICE_ID, args.deviceId);
4639 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4640 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4641 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4642 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4643 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4644 ASSERT_EQ(KEY_HOME, args.scanCode);
4645 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4646 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4647
4648 // Should not have sent any motions.
4649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4650}
4651
4652TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004654 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655 prepareButtons();
4656 prepareAxes(POSITION);
4657 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004658 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004659
arthurhungdcef2dc2020-08-11 14:47:50 +08004660 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661
4662 NotifyKeyArgs keyArgs;
4663
4664 // Press virtual key.
4665 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4666 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4667 processDown(mapper, x, y);
4668 processSync(mapper);
4669
4670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4671 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4672 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4673 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4674 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4675 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4676 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4677 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4678 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4679 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4680 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4681
4682 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4683 // into the display area.
4684 y -= 100;
4685 processMove(mapper, x, y);
4686 processSync(mapper);
4687
4688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4689 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4690 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4691 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4692 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4693 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4694 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4695 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4696 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4697 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4698 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4699 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4700
4701 NotifyMotionArgs motionArgs;
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4703 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4704 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4705 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4706 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4708 ASSERT_EQ(0, motionArgs.flags);
4709 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4710 ASSERT_EQ(0, motionArgs.buttonState);
4711 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004712 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004713 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004714 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4716 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4717 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4718 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4719 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4720
4721 // Keep moving out of bounds. Should generate a pointer move.
4722 y -= 50;
4723 processMove(mapper, x, y);
4724 processSync(mapper);
4725
4726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4727 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4728 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4729 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4730 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4732 ASSERT_EQ(0, motionArgs.flags);
4733 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4734 ASSERT_EQ(0, motionArgs.buttonState);
4735 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004736 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004737 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004738 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4740 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4741 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4742 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4744
4745 // Release out of bounds. Should generate a pointer up.
4746 processUp(mapper);
4747 processSync(mapper);
4748
4749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4750 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4751 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4752 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4753 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4754 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4755 ASSERT_EQ(0, motionArgs.flags);
4756 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4757 ASSERT_EQ(0, motionArgs.buttonState);
4758 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004759 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004760 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004761 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4763 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4764 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4765 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4766 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4767
4768 // Should not have sent any more keys or motions.
4769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4771}
4772
4773TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004775 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776 prepareButtons();
4777 prepareAxes(POSITION);
4778 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004779 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780
arthurhungdcef2dc2020-08-11 14:47:50 +08004781 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004782
4783 NotifyMotionArgs motionArgs;
4784
4785 // Initially go down out of bounds.
4786 int32_t x = -10;
4787 int32_t y = -10;
4788 processDown(mapper, x, y);
4789 processSync(mapper);
4790
4791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4792
4793 // Move into the display area. Should generate a pointer down.
4794 x = 50;
4795 y = 75;
4796 processMove(mapper, x, y);
4797 processSync(mapper);
4798
4799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4800 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4801 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4802 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4803 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4805 ASSERT_EQ(0, motionArgs.flags);
4806 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4807 ASSERT_EQ(0, motionArgs.buttonState);
4808 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004809 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004810 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004811 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4813 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4814 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4815 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4817
4818 // Release. Should generate a pointer up.
4819 processUp(mapper);
4820 processSync(mapper);
4821
4822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4823 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4824 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4825 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4826 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4827 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4828 ASSERT_EQ(0, motionArgs.flags);
4829 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4830 ASSERT_EQ(0, motionArgs.buttonState);
4831 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004832 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004834 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4836 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4837 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4838 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4839 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4840
4841 // Should not have sent any more keys or motions.
4842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4844}
4845
Santos Cordonfa5cf462017-04-05 10:37:00 -07004846TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004847 addConfigurationProperty("touch.deviceType", "touchScreen");
4848 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4849
Michael Wrighta9cf4192022-12-01 23:46:39 +00004850 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004851 prepareButtons();
4852 prepareAxes(POSITION);
4853 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004854 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004855
arthurhungdcef2dc2020-08-11 14:47:50 +08004856 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004857
4858 NotifyMotionArgs motionArgs;
4859
4860 // Down.
4861 int32_t x = 100;
4862 int32_t y = 125;
4863 processDown(mapper, x, y);
4864 processSync(mapper);
4865
4866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4867 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4868 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4869 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4870 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4871 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4872 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4873 ASSERT_EQ(0, motionArgs.flags);
4874 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4875 ASSERT_EQ(0, motionArgs.buttonState);
4876 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004877 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004878 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004879 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4881 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4882 1, 0, 0, 0, 0, 0, 0, 0));
4883 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4884 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4885 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4886
4887 // Move.
4888 x += 50;
4889 y += 75;
4890 processMove(mapper, x, y);
4891 processSync(mapper);
4892
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4894 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4895 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4896 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4897 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4898 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4900 ASSERT_EQ(0, motionArgs.flags);
4901 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4902 ASSERT_EQ(0, motionArgs.buttonState);
4903 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004904 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004905 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004906 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4908 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4909 1, 0, 0, 0, 0, 0, 0, 0));
4910 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4911 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4913
4914 // Up.
4915 processUp(mapper);
4916 processSync(mapper);
4917
4918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4919 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4920 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4921 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4922 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4923 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4924 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4925 ASSERT_EQ(0, motionArgs.flags);
4926 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4927 ASSERT_EQ(0, motionArgs.buttonState);
4928 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004929 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004930 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004931 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4933 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4934 1, 0, 0, 0, 0, 0, 0, 0));
4935 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4936 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4937 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4938
4939 // Should not have sent any more keys or motions.
4940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4942}
4943
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004946 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947 prepareButtons();
4948 prepareAxes(POSITION);
4949 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004950 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004951
arthurhungdcef2dc2020-08-11 14:47:50 +08004952 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004953
4954 NotifyMotionArgs motionArgs;
4955
4956 // Down.
4957 int32_t x = 100;
4958 int32_t y = 125;
4959 processDown(mapper, x, y);
4960 processSync(mapper);
4961
4962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4963 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4964 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4965 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4966 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4967 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4968 ASSERT_EQ(0, motionArgs.flags);
4969 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4970 ASSERT_EQ(0, motionArgs.buttonState);
4971 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004972 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004974 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4976 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4977 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4978 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4979 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4980
4981 // Move.
4982 x += 50;
4983 y += 75;
4984 processMove(mapper, x, y);
4985 processSync(mapper);
4986
4987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4988 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4989 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4990 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4991 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4993 ASSERT_EQ(0, motionArgs.flags);
4994 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4995 ASSERT_EQ(0, motionArgs.buttonState);
4996 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004997 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004998 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004999 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5001 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5002 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5003 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5004 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5005
5006 // Up.
5007 processUp(mapper);
5008 processSync(mapper);
5009
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5011 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5012 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5013 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5014 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5015 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5016 ASSERT_EQ(0, motionArgs.flags);
5017 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5018 ASSERT_EQ(0, motionArgs.buttonState);
5019 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005020 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005022 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5024 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5025 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5026 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5027 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5028
5029 // Should not have sent any more keys or motions.
5030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5032}
5033
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005034TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 addConfigurationProperty("touch.deviceType", "touchScreen");
5036 prepareButtons();
5037 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005038 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5039 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005040 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005041
5042 NotifyMotionArgs args;
5043
5044 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005045 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046 processDown(mapper, toRawX(50), toRawY(75));
5047 processSync(mapper);
5048
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5050 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5051 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5052
5053 processUp(mapper);
5054 processSync(mapper);
5055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5056}
5057
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005058TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059 addConfigurationProperty("touch.deviceType", "touchScreen");
5060 prepareButtons();
5061 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005062 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5063 // orientation-aware are affected by display rotation.
5064 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005065 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066
5067 NotifyMotionArgs args;
5068
5069 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005070 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005071 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005072 processDown(mapper, toRawX(50), toRawY(75));
5073 processSync(mapper);
5074
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5076 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5077 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5078
5079 processUp(mapper);
5080 processSync(mapper);
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5082
5083 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005084 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005085 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005086 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005087 processSync(mapper);
5088
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5090 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5091 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5092
5093 processUp(mapper);
5094 processSync(mapper);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5096
5097 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005098 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005099 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005100 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5101 processSync(mapper);
5102
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5104 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5105 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5106
5107 processUp(mapper);
5108 processSync(mapper);
5109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5110
5111 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005112 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005113 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005114 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 processSync(mapper);
5116
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5118 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5119 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5120
5121 processUp(mapper);
5122 processSync(mapper);
5123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5124}
5125
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005126TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5127 addConfigurationProperty("touch.deviceType", "touchScreen");
5128 prepareButtons();
5129 prepareAxes(POSITION);
5130 addConfigurationProperty("touch.orientationAware", "1");
5131 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5132 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005133 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005134 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005135 NotifyMotionArgs args;
5136
5137 // Orientation 0.
5138 processDown(mapper, toRawX(50), toRawY(75));
5139 processSync(mapper);
5140
5141 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5142 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5143 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5144
5145 processUp(mapper);
5146 processSync(mapper);
5147 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5148}
5149
5150TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5151 addConfigurationProperty("touch.deviceType", "touchScreen");
5152 prepareButtons();
5153 prepareAxes(POSITION);
5154 addConfigurationProperty("touch.orientationAware", "1");
5155 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5156 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005157 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005158 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005159 NotifyMotionArgs args;
5160
5161 // Orientation 90.
5162 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5163 processSync(mapper);
5164
5165 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5166 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5167 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5168
5169 processUp(mapper);
5170 processSync(mapper);
5171 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5172}
5173
5174TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5175 addConfigurationProperty("touch.deviceType", "touchScreen");
5176 prepareButtons();
5177 prepareAxes(POSITION);
5178 addConfigurationProperty("touch.orientationAware", "1");
5179 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5180 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005181 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005182 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005183 NotifyMotionArgs args;
5184
5185 // Orientation 180.
5186 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5187 processSync(mapper);
5188
5189 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5190 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5191 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5192
5193 processUp(mapper);
5194 processSync(mapper);
5195 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5196}
5197
5198TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5199 addConfigurationProperty("touch.deviceType", "touchScreen");
5200 prepareButtons();
5201 prepareAxes(POSITION);
5202 addConfigurationProperty("touch.orientationAware", "1");
5203 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5204 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005205 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005206 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005207 NotifyMotionArgs args;
5208
5209 // Orientation 270.
5210 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5211 processSync(mapper);
5212
5213 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5214 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5215 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5216
5217 processUp(mapper);
5218 processSync(mapper);
5219 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5220}
5221
5222TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5223 addConfigurationProperty("touch.deviceType", "touchScreen");
5224 prepareButtons();
5225 prepareAxes(POSITION);
5226 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5227 // orientation-aware are affected by display rotation.
5228 addConfigurationProperty("touch.orientationAware", "0");
5229 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005230 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005231
5232 NotifyMotionArgs args;
5233
5234 // Orientation 90, Rotation 0.
5235 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005236 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005237 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5238 processSync(mapper);
5239
5240 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5241 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5242 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5243
5244 processUp(mapper);
5245 processSync(mapper);
5246 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5247
5248 // Orientation 90, Rotation 90.
5249 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005250 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005251 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005252 processSync(mapper);
5253
5254 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5255 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5256 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5257
5258 processUp(mapper);
5259 processSync(mapper);
5260 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5261
5262 // Orientation 90, Rotation 180.
5263 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005264 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005265 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5266 processSync(mapper);
5267
5268 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5269 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5270 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5271
5272 processUp(mapper);
5273 processSync(mapper);
5274 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5275
5276 // Orientation 90, Rotation 270.
5277 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005278 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005279 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005280 processSync(mapper);
5281
5282 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5283 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5284 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5285
5286 processUp(mapper);
5287 processSync(mapper);
5288 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5289}
5290
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005291TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5292 addConfigurationProperty("touch.deviceType", "touchScreen");
5293 prepareButtons();
5294 prepareAxes(POSITION);
5295 addConfigurationProperty("touch.orientationAware", "1");
5296 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005297 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005298
5299 // Set a physical frame in the display viewport.
5300 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5301 viewport->physicalLeft = 20;
5302 viewport->physicalTop = 600;
5303 viewport->physicalRight = 30;
5304 viewport->physicalBottom = 610;
5305 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005306 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005307
5308 // Start the touch.
5309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5310 processSync(mapper);
5311
5312 // Expect all input starting outside the physical frame to be ignored.
5313 const std::array<Point, 6> outsidePoints = {
5314 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5315 for (const auto& p : outsidePoints) {
5316 processMove(mapper, toRawX(p.x), toRawY(p.y));
5317 processSync(mapper);
5318 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5319 }
5320
5321 // Move the touch into the physical frame.
5322 processMove(mapper, toRawX(25), toRawY(605));
5323 processSync(mapper);
5324 NotifyMotionArgs args;
5325 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5326 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5327 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5328 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5329
5330 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5331 for (const auto& p : outsidePoints) {
5332 processMove(mapper, toRawX(p.x), toRawY(p.y));
5333 processSync(mapper);
5334 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5335 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5336 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5337 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5338 }
5339
5340 processUp(mapper);
5341 processSync(mapper);
5342 EXPECT_NO_FATAL_FAILURE(
5343 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5344}
5345
Harry Cutts1db43992023-06-19 17:05:07 +00005346TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5347 std::shared_ptr<FakePointerController> fakePointerController =
5348 std::make_shared<FakePointerController>();
5349 mFakePolicy->setPointerController(fakePointerController);
5350
5351 addConfigurationProperty("touch.deviceType", "pointer");
5352 prepareAxes(POSITION);
5353 prepareDisplay(ui::ROTATION_0);
5354 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5355
5356 // Set a physical frame in the display viewport.
5357 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5358 viewport->physicalLeft = 20;
5359 viewport->physicalTop = 600;
5360 viewport->physicalRight = 30;
5361 viewport->physicalBottom = 610;
5362 mFakePolicy->updateViewport(*viewport);
5363 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5364
5365 // Start the touch.
5366 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5367 processSync(mapper);
5368
5369 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5370 // produced.
5371 const std::array<Point, 6> outsidePoints = {
5372 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5373 for (const auto& p : outsidePoints) {
5374 processMove(mapper, toRawX(p.x), toRawY(p.y));
5375 processSync(mapper);
5376 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5377 }
5378}
5379
Michael Wrightd02c5b62014-02-10 15:10:22 -08005380TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005382 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383 prepareButtons();
5384 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005385 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005386
5387 // These calculations are based on the input device calibration documentation.
5388 int32_t rawX = 100;
5389 int32_t rawY = 200;
5390 int32_t rawPressure = 10;
5391 int32_t rawToolMajor = 12;
5392 int32_t rawDistance = 2;
5393 int32_t rawTiltX = 30;
5394 int32_t rawTiltY = 110;
5395
5396 float x = toDisplayX(rawX);
5397 float y = toDisplayY(rawY);
5398 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5399 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5400 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5401 float distance = float(rawDistance);
5402
5403 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5404 float tiltScale = M_PI / 180;
5405 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5406 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5407 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5408 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5409
5410 processDown(mapper, rawX, rawY);
5411 processPressure(mapper, rawPressure);
5412 processToolMajor(mapper, rawToolMajor);
5413 processDistance(mapper, rawDistance);
5414 processTilt(mapper, rawTiltX, rawTiltY);
5415 processSync(mapper);
5416
5417 NotifyMotionArgs args;
5418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5420 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5421 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5422}
5423
Jason Gerecke489fda82012-09-07 17:19:40 -07005424TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005425 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005426 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005427 prepareLocationCalibration();
5428 prepareButtons();
5429 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005430 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005431
5432 int32_t rawX = 100;
5433 int32_t rawY = 200;
5434
5435 float x = toDisplayX(toCookedX(rawX, rawY));
5436 float y = toDisplayY(toCookedY(rawX, rawY));
5437
5438 processDown(mapper, rawX, rawY);
5439 processSync(mapper);
5440
5441 NotifyMotionArgs args;
5442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5444 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5445}
5446
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005449 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005450 prepareButtons();
5451 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005452 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005453
5454 NotifyMotionArgs motionArgs;
5455 NotifyKeyArgs keyArgs;
5456
5457 processDown(mapper, 100, 200);
5458 processSync(mapper);
5459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5460 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5461 ASSERT_EQ(0, motionArgs.buttonState);
5462
5463 // press BTN_LEFT, release BTN_LEFT
5464 processKey(mapper, BTN_LEFT, 1);
5465 processSync(mapper);
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5468 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5469
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5471 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5472 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5473
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474 processKey(mapper, BTN_LEFT, 0);
5475 processSync(mapper);
5476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005477 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005478 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005479
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005482 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005483
5484 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5485 processKey(mapper, BTN_RIGHT, 1);
5486 processKey(mapper, BTN_MIDDLE, 1);
5487 processSync(mapper);
5488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5490 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5491 motionArgs.buttonState);
5492
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5495 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5496
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5499 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5500 motionArgs.buttonState);
5501
Michael Wrightd02c5b62014-02-10 15:10:22 -08005502 processKey(mapper, BTN_RIGHT, 0);
5503 processSync(mapper);
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005507
5508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005510 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511
5512 processKey(mapper, BTN_MIDDLE, 0);
5513 processSync(mapper);
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005515 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005517
5518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005520 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005521
5522 // press BTN_BACK, release BTN_BACK
5523 processKey(mapper, BTN_BACK, 1);
5524 processSync(mapper);
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5526 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5527 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005528
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005531 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5534 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5535 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536
5537 processKey(mapper, BTN_BACK, 0);
5538 processSync(mapper);
5539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005540 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005541 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005542
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005545 ASSERT_EQ(0, motionArgs.buttonState);
5546
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5548 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5549 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5550
5551 // press BTN_SIDE, release BTN_SIDE
5552 processKey(mapper, BTN_SIDE, 1);
5553 processSync(mapper);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5555 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5556 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005557
Michael Wrightd02c5b62014-02-10 15:10:22 -08005558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005560 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, 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_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565
5566 processKey(mapper, BTN_SIDE, 0);
5567 processSync(mapper);
5568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005569 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005570 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005571
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005573 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005574 ASSERT_EQ(0, motionArgs.buttonState);
5575
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5577 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5578 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5579
5580 // press BTN_FORWARD, release BTN_FORWARD
5581 processKey(mapper, BTN_FORWARD, 1);
5582 processSync(mapper);
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5584 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5585 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005586
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005589 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5590
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5592 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5593 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594
5595 processKey(mapper, BTN_FORWARD, 0);
5596 processSync(mapper);
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005598 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005600
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005603 ASSERT_EQ(0, motionArgs.buttonState);
5604
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5606 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5607 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5608
5609 // press BTN_EXTRA, release BTN_EXTRA
5610 processKey(mapper, BTN_EXTRA, 1);
5611 processSync(mapper);
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5613 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5614 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005615
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005618 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5619
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5621 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5622 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623
5624 processKey(mapper, BTN_EXTRA, 0);
5625 processSync(mapper);
5626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005628 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005629
5630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005632 ASSERT_EQ(0, motionArgs.buttonState);
5633
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5635 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5636 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5637
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5639
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640 // press BTN_STYLUS, release BTN_STYLUS
5641 processKey(mapper, BTN_STYLUS, 1);
5642 processSync(mapper);
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005645 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5646
5647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5648 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5649 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005650
5651 processKey(mapper, BTN_STYLUS, 0);
5652 processSync(mapper);
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005654 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005656
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005659 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660
5661 // press BTN_STYLUS2, release BTN_STYLUS2
5662 processKey(mapper, BTN_STYLUS2, 1);
5663 processSync(mapper);
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5665 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005666 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5667
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5669 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5670 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671
5672 processKey(mapper, BTN_STYLUS2, 0);
5673 processSync(mapper);
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005675 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005677
5678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005680 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681
5682 // release touch
5683 processUp(mapper);
5684 processSync(mapper);
5685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5686 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5687 ASSERT_EQ(0, motionArgs.buttonState);
5688}
5689
5690TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005691 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005692 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 prepareButtons();
5694 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005695 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696
5697 NotifyMotionArgs motionArgs;
5698
5699 // default tool type is finger
5700 processDown(mapper, 100, 200);
5701 processSync(mapper);
5702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5703 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005704 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705
5706 // eraser
5707 processKey(mapper, BTN_TOOL_RUBBER, 1);
5708 processSync(mapper);
5709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5710 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005711 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005712
5713 // stylus
5714 processKey(mapper, BTN_TOOL_RUBBER, 0);
5715 processKey(mapper, BTN_TOOL_PEN, 1);
5716 processSync(mapper);
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005719 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005720
5721 // brush
5722 processKey(mapper, BTN_TOOL_PEN, 0);
5723 processKey(mapper, BTN_TOOL_BRUSH, 1);
5724 processSync(mapper);
5725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005727 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728
5729 // pencil
5730 processKey(mapper, BTN_TOOL_BRUSH, 0);
5731 processKey(mapper, BTN_TOOL_PENCIL, 1);
5732 processSync(mapper);
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005735 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005736
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005737 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005738 processKey(mapper, BTN_TOOL_PENCIL, 0);
5739 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5740 processSync(mapper);
5741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005743 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005744
5745 // mouse
5746 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5747 processKey(mapper, BTN_TOOL_MOUSE, 1);
5748 processSync(mapper);
5749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005751 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005752
5753 // lens
5754 processKey(mapper, BTN_TOOL_MOUSE, 0);
5755 processKey(mapper, BTN_TOOL_LENS, 1);
5756 processSync(mapper);
5757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5758 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005759 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760
5761 // double-tap
5762 processKey(mapper, BTN_TOOL_LENS, 0);
5763 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5764 processSync(mapper);
5765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005767 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768
5769 // triple-tap
5770 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5771 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5772 processSync(mapper);
5773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5774 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005775 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005776
5777 // quad-tap
5778 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5779 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5780 processSync(mapper);
5781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005783 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005784
5785 // finger
5786 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5787 processKey(mapper, BTN_TOOL_FINGER, 1);
5788 processSync(mapper);
5789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005791 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792
5793 // stylus trumps finger
5794 processKey(mapper, BTN_TOOL_PEN, 1);
5795 processSync(mapper);
5796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005798 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799
5800 // eraser trumps stylus
5801 processKey(mapper, BTN_TOOL_RUBBER, 1);
5802 processSync(mapper);
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005805 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005806
5807 // mouse trumps eraser
5808 processKey(mapper, BTN_TOOL_MOUSE, 1);
5809 processSync(mapper);
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005812 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813
5814 // back to default tool type
5815 processKey(mapper, BTN_TOOL_MOUSE, 0);
5816 processKey(mapper, BTN_TOOL_RUBBER, 0);
5817 processKey(mapper, BTN_TOOL_PEN, 0);
5818 processKey(mapper, BTN_TOOL_FINGER, 0);
5819 processSync(mapper);
5820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005822 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005823}
5824
5825TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005827 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828 prepareButtons();
5829 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005830 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005831 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832
5833 NotifyMotionArgs motionArgs;
5834
5835 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5836 processKey(mapper, BTN_TOOL_FINGER, 1);
5837 processMove(mapper, 100, 200);
5838 processSync(mapper);
5839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5840 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5842 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5843
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5845 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5846 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5847 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5848
5849 // move a little
5850 processMove(mapper, 150, 250);
5851 processSync(mapper);
5852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5853 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5855 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5856
5857 // down when BTN_TOUCH is pressed, pressure defaults to 1
5858 processKey(mapper, BTN_TOUCH, 1);
5859 processSync(mapper);
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5861 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5863 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5864
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5868 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5869
5870 // up when BTN_TOUCH is released, hover restored
5871 processKey(mapper, BTN_TOUCH, 0);
5872 processSync(mapper);
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5874 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5876 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5877
5878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5879 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5881 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5882
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5884 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5886 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5887
5888 // exit hover when pointer goes away
5889 processKey(mapper, BTN_TOOL_FINGER, 0);
5890 processSync(mapper);
5891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5892 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5894 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5895}
5896
5897TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005899 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900 prepareButtons();
5901 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005902 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005903
5904 NotifyMotionArgs motionArgs;
5905
5906 // initially hovering because pressure is 0
5907 processDown(mapper, 100, 200);
5908 processPressure(mapper, 0);
5909 processSync(mapper);
5910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5911 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5912 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5913 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5914
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5916 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5918 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5919
5920 // move a little
5921 processMove(mapper, 150, 250);
5922 processSync(mapper);
5923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5924 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5926 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5927
5928 // down when pressure is non-zero
5929 processPressure(mapper, RAW_PRESSURE_MAX);
5930 processSync(mapper);
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5932 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5933 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5934 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5935
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5937 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5939 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5940
5941 // up when pressure becomes 0, hover restored
5942 processPressure(mapper, 0);
5943 processSync(mapper);
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5945 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5947 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5948
5949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5950 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5952 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5957 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5958
5959 // exit hover when pointer goes away
5960 processUp(mapper);
5961 processSync(mapper);
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5963 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5965 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5966}
5967
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005968TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
5969 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005970 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005971 prepareButtons();
5972 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005973 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005974
5975 // Touch down.
5976 processDown(mapper, 100, 200);
5977 processPressure(mapper, 1);
5978 processSync(mapper);
5979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5980 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
5981
5982 // Reset the mapper. This should cancel the ongoing gesture.
5983 resetMapper(mapper, ARBITRARY_TIME);
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5985 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
5986
5987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5988}
5989
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005990TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
5991 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005992 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005993 prepareButtons();
5994 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005995 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005996
5997 // Set the initial state for the touch pointer.
5998 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
5999 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6000 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6001 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6002
6003 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006004 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6005 // does not generate any events.
6006 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006007
6008 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6009 // the recreated touch state to generate a down event.
6010 processSync(mapper);
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6012 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6013
6014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6015}
6016
lilinnan687e58f2022-07-19 16:00:50 +08006017TEST_F(SingleTouchInputMapperTest,
6018 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6019 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006020 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006021 prepareButtons();
6022 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006023 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006024 NotifyMotionArgs motionArgs;
6025
6026 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006027 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006028 processSync(mapper);
6029
6030 // We should receive a down event
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6033
6034 // Change display id
6035 clearViewports();
6036 prepareSecondaryDisplay(ViewportType::INTERNAL);
6037
6038 // We should receive a cancel event
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6040 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6041 // Then receive reset called
6042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6043}
6044
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006045TEST_F(SingleTouchInputMapperTest,
6046 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6047 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006048 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006049 prepareButtons();
6050 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006051 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6053 NotifyMotionArgs motionArgs;
6054
6055 // Start a new gesture.
6056 processDown(mapper, 100, 200);
6057 processSync(mapper);
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6059 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6060
6061 // Make the viewport inactive. This will put the device in disabled mode.
6062 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6063 viewport->isActive = false;
6064 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006065 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006066
6067 // We should receive a cancel event for the ongoing gesture.
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6070 // Then we should be notified that the device was reset.
6071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6072
6073 // No events are generated while the viewport is inactive.
6074 processMove(mapper, 101, 201);
6075 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006076 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006077 processSync(mapper);
6078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6079
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006080 // Start a new gesture while the viewport is still inactive.
6081 processDown(mapper, 300, 400);
6082 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6083 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6084 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6085 processSync(mapper);
6086
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006087 // Make the viewport active again. The device should resume processing events.
6088 viewport->isActive = true;
6089 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006090 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006091
6092 // The device is reset because it changes back to direct mode, without generating any events.
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6095
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006096 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006097 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6099 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006100
6101 // No more events.
6102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6104}
6105
Prabir Pradhan211ba622022-10-31 21:09:21 +00006106TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6107 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006108 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006109 prepareButtons();
6110 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006111 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6113
6114 // Press a stylus button.
6115 processKey(mapper, BTN_STYLUS, 1);
6116 processSync(mapper);
6117
6118 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6119 processDown(mapper, 100, 200);
6120 processSync(mapper);
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6122 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6123 WithCoords(toDisplayX(100), toDisplayY(200)),
6124 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6127 WithCoords(toDisplayX(100), toDisplayY(200)),
6128 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6129
6130 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6131 // the button has not actually been released, since there will be no pointers through which the
6132 // button state can be reported. The event is generated at the location of the pointer before
6133 // it went up.
6134 processUp(mapper);
6135 processSync(mapper);
6136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6137 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6138 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6140 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6141 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6142}
6143
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006144TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6145 addConfigurationProperty("touch.deviceType", "touchScreen");
6146 prepareDisplay(ui::ROTATION_0);
6147 prepareButtons();
6148 prepareAxes(POSITION);
6149
6150 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6151
Arpit Singha8c236b2023-04-25 13:56:05 +00006152 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6154
6155 // Press a stylus button.
6156 processKey(mapper, BTN_STYLUS, 1);
6157 processSync(mapper);
6158
6159 // Start a touch gesture and ensure that the stylus button is not reported.
6160 processDown(mapper, 100, 200);
6161 processSync(mapper);
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6163 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6164
6165 // Release and press the stylus button again.
6166 processKey(mapper, BTN_STYLUS, 0);
6167 processSync(mapper);
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6169 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6170 processKey(mapper, BTN_STYLUS, 1);
6171 processSync(mapper);
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6173 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6174
6175 // Release the touch gesture.
6176 processUp(mapper);
6177 processSync(mapper);
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6179 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6180
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6182}
6183
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006184TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6185 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6186 prepareDisplay(ui::ROTATION_0);
6187 prepareButtons();
6188 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006189 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6191
6192 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6193}
6194
Seunghwan Choi356026c2023-02-01 14:37:25 +09006195TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6196 std::shared_ptr<FakePointerController> fakePointerController =
6197 std::make_shared<FakePointerController>();
6198 addConfigurationProperty("touch.deviceType", "touchScreen");
6199 prepareDisplay(ui::ROTATION_0);
6200 prepareButtons();
6201 prepareAxes(POSITION);
6202 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6203 mFakePolicy->setPointerController(fakePointerController);
6204 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006205 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006206
6207 processKey(mapper, BTN_TOOL_PEN, 1);
6208 processMove(mapper, 100, 200);
6209 processSync(mapper);
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6211 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006212 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006213 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6214 ASSERT_TRUE(fakePointerController->isPointerShown());
6215 ASSERT_NO_FATAL_FAILURE(
6216 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6217}
6218
6219TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6220 std::shared_ptr<FakePointerController> fakePointerController =
6221 std::make_shared<FakePointerController>();
6222 addConfigurationProperty("touch.deviceType", "touchScreen");
6223 prepareDisplay(ui::ROTATION_0);
6224 prepareButtons();
6225 prepareAxes(POSITION);
6226 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6227 mFakePolicy->setPointerController(fakePointerController);
6228 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006229 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006230
6231 processKey(mapper, BTN_TOOL_PEN, 1);
6232 processMove(mapper, 100, 200);
6233 processSync(mapper);
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6235 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006236 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006237 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6238 ASSERT_FALSE(fakePointerController->isPointerShown());
6239}
6240
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006241TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6242 // Initialize the device without setting device source to touch navigation.
6243 addConfigurationProperty("touch.deviceType", "touchScreen");
6244 prepareDisplay(ui::ROTATION_0);
6245 prepareButtons();
6246 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006247 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006248
6249 // Ensure that the device is created as a touchscreen, not touch navigation.
6250 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6251
6252 // Add device type association after the device was created.
6253 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6254
6255 // Send update to the mapper.
6256 std::list<NotifyArgs> unused2 =
6257 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006258 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006259
6260 // Check whether device type update was successful.
6261 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6262}
6263
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006264TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6265 // Initialize the device without setting device source to touch navigation.
6266 addConfigurationProperty("touch.deviceType", "touchScreen");
6267 prepareDisplay(ui::ROTATION_0);
6268 prepareButtons();
6269 prepareAxes(POSITION);
6270 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6271
6272 // Set a physical frame in the display viewport.
6273 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6274 viewport->physicalLeft = 0;
6275 viewport->physicalTop = 0;
6276 viewport->physicalRight = DISPLAY_WIDTH / 2;
6277 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6278 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006279 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006280
Arpit Singha8c236b2023-04-25 13:56:05 +00006281 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006282
6283 // Hovering inside the physical frame produces events.
6284 processKey(mapper, BTN_TOOL_PEN, 1);
6285 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6288 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6290 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6291
6292 // Leaving the physical frame ends the hovering gesture.
6293 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6296 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6297
6298 // Moving outside the physical frame does not produce events.
6299 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6300 processSync(mapper);
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6302
6303 // Re-entering the physical frame produces events.
6304 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
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
Prabir Pradhan5632d622021-09-06 07:57:20 -07006312// --- TouchDisplayProjectionTest ---
6313
6314class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6315public:
6316 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6317 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6318 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006319 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6320 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6321 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006322 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006323 auto rotatedWidth = naturalDisplayWidth;
6324 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006325 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006326 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006327 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006328 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006329 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006330 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006331 inverseRotationFlags = ui::Transform::ROT_180;
6332 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006333 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006334 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006335 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006336 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006337 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006338 inverseRotationFlags = ui::Transform::ROT_0;
6339 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006340 }
6341
Prabir Pradhana9df3162022-12-05 23:57:27 +00006342 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006343 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6344
6345 std::optional<DisplayViewport> internalViewport =
6346 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6347 DisplayViewport& v = *internalViewport;
6348 v.displayId = DISPLAY_ID;
6349 v.orientation = orientation;
6350
6351 v.logicalLeft = 0;
6352 v.logicalTop = 0;
6353 v.logicalRight = 100;
6354 v.logicalBottom = 100;
6355
6356 v.physicalLeft = rotatedPhysicalDisplay.left;
6357 v.physicalTop = rotatedPhysicalDisplay.top;
6358 v.physicalRight = rotatedPhysicalDisplay.right;
6359 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6360
Prabir Pradhana9df3162022-12-05 23:57:27 +00006361 v.deviceWidth = rotatedWidth;
6362 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006363
6364 v.isActive = true;
6365 v.uniqueId = UNIQUE_ID;
6366 v.type = ViewportType::INTERNAL;
6367 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006368 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006369 }
6370
6371 void assertReceivedMove(const Point& point) {
6372 NotifyMotionArgs motionArgs;
6373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006375 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006376 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6377 1, 0, 0, 0, 0, 0, 0, 0));
6378 }
6379};
6380
6381TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6382 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006383 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006384
6385 prepareButtons();
6386 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006387 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006388
6389 NotifyMotionArgs motionArgs;
6390
6391 // Configure the DisplayViewport such that the logical display maps to a subsection of
6392 // the display panel called the physical display. Here, the physical display is bounded by the
6393 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6394 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6395 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6396 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6397
Michael Wrighta9cf4192022-12-01 23:46:39 +00006398 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006399 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6400
6401 // Touches outside the physical display should be ignored, and should not generate any
6402 // events. Ensure touches at the following points that lie outside of the physical display
6403 // area do not generate any events.
6404 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6405 processDown(mapper, toRawX(point.x), toRawY(point.y));
6406 processSync(mapper);
6407 processUp(mapper);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6410 << "Unexpected event generated for touch outside physical display at point: "
6411 << point.x << ", " << point.y;
6412 }
6413 }
6414}
6415
6416TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6417 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006418 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006419
6420 prepareButtons();
6421 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006422 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006423
6424 NotifyMotionArgs motionArgs;
6425
6426 // Configure the DisplayViewport such that the logical display maps to a subsection of
6427 // the display panel called the physical display. Here, the physical display is bounded by the
6428 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6429 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6430
Michael Wrighta9cf4192022-12-01 23:46:39 +00006431 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006432 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6433
6434 // Touches that start outside the physical display should be ignored until it enters the
6435 // physical display bounds, at which point it should generate a down event. Start a touch at
6436 // the point (5, 100), which is outside the physical display bounds.
6437 static const Point kOutsidePoint{5, 100};
6438 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6439 processSync(mapper);
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6441
6442 // Move the touch into the physical display area. This should generate a pointer down.
6443 processMove(mapper, toRawX(11), toRawY(21));
6444 processSync(mapper);
6445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6446 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006447 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006448 ASSERT_NO_FATAL_FAILURE(
6449 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6450
6451 // Move the touch inside the physical display area. This should generate a pointer move.
6452 processMove(mapper, toRawX(69), toRawY(159));
6453 processSync(mapper);
6454 assertReceivedMove({69, 159});
6455
6456 // Move outside the physical display area. Since the pointer is already down, this should
6457 // now continue generating events.
6458 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6459 processSync(mapper);
6460 assertReceivedMove(kOutsidePoint);
6461
6462 // Release. This should generate a pointer up.
6463 processUp(mapper);
6464 processSync(mapper);
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6466 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6467 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6468 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6469
6470 // Ensure no more events were generated.
6471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6473 }
6474}
6475
Prabir Pradhana9df3162022-12-05 23:57:27 +00006476// --- TouchscreenPrecisionTests ---
6477
6478// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6479// in various orientations and with different display rotations. We configure the touchscreen to
6480// have a higher resolution than that of the display by an integer scale factor in each axis so that
6481// we can enforce that coordinates match precisely as expected.
6482class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6483 public ::testing::WithParamInterface<ui::Rotation> {
6484public:
6485 void SetUp() override {
6486 SingleTouchInputMapperTest::SetUp();
6487
6488 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6489 // four times the resolution of the display in the Y axis.
6490 prepareButtons();
6491 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006492 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6493 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006494 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006495 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6496 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006497 }
6498
6499 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6500 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6501 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6502 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6503
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006504 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6505 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6506
6507 static const int32_t PRECISION_RAW_X_FLAT = 16;
6508 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6509
6510 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6511 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6512
Prabir Pradhana9df3162022-12-05 23:57:27 +00006513 static const std::array<Point, 4> kRawCorners;
6514};
6515
6516const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6517 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6518 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6519 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6520 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6521}};
6522
6523// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6524// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6525// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6526TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6527 enum class Orientation {
6528 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6529 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6530 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6531 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6532 ftl_last = ORIENTATION_270,
6533 };
6534 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6535 Orientation::ORIENTATION_270;
6536 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6537 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6538 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6539 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6540 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6541 };
6542
6543 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6544
6545 // Configure the touchscreen as being installed in the one of the four different orientations
6546 // relative to the display.
6547 addConfigurationProperty("touch.deviceType", "touchScreen");
6548 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6549 prepareDisplay(ui::ROTATION_0);
6550
Arpit Singha8c236b2023-04-25 13:56:05 +00006551 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006552
6553 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6554 // orientations of either 90 or 270) this means the display's natural resolution will be
6555 // flipped.
6556 const bool displayRotated =
6557 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6558 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6559 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6560 const Rect physicalFrame{0, 0, width, height};
6561 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6562
6563 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6564 const float expectedPrecisionX = displayRotated ? 4 : 2;
6565 const float expectedPrecisionY = displayRotated ? 2 : 4;
6566
6567 // Test all four corners.
6568 for (int i = 0; i < 4; i++) {
6569 const auto& raw = kRawCorners[i];
6570 processDown(mapper, raw.x, raw.y);
6571 processSync(mapper);
6572 const auto& expected = expectedPoints[i];
6573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6574 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6575 WithCoords(expected.x, expected.y),
6576 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6577 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6578 << "with touchscreen orientation "
6579 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6580 << expected.x << ", " << expected.y << ").";
6581 processUp(mapper);
6582 processSync(mapper);
6583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6584 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6585 WithCoords(expected.x, expected.y))));
6586 }
6587}
6588
Prabir Pradhan82687402022-12-06 01:32:53 +00006589TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6590 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6591 kMappedCorners = {
6592 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6593 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6594 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6595 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6596 };
6597
6598 const ui::Rotation displayRotation = GetParam();
6599
6600 addConfigurationProperty("touch.deviceType", "touchScreen");
6601 prepareDisplay(displayRotation);
6602
Arpit Singha8c236b2023-04-25 13:56:05 +00006603 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006604
6605 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6606
6607 // Test all four corners.
6608 for (int i = 0; i < 4; i++) {
6609 const auto& expected = expectedPoints[i];
6610 const auto& raw = kRawCorners[i];
6611 processDown(mapper, raw.x, raw.y);
6612 processSync(mapper);
6613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6614 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6615 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6616 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6617 << "with display rotation " << ui::toCString(displayRotation)
6618 << ", expected point (" << expected.x << ", " << expected.y << ").";
6619 processUp(mapper);
6620 processSync(mapper);
6621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6622 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6623 WithCoords(expected.x, expected.y))));
6624 }
6625}
6626
Prabir Pradhan3e798762022-12-02 21:02:11 +00006627TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6628 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6629 kMappedCorners = {
6630 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6631 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6632 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6633 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6634 };
6635
6636 const ui::Rotation displayRotation = GetParam();
6637
6638 addConfigurationProperty("touch.deviceType", "touchScreen");
6639 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6640
Arpit Singha8c236b2023-04-25 13:56:05 +00006641 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006642
6643 // Ori 270, so width and height swapped
6644 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6645 prepareDisplay(displayRotation);
6646 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6647
6648 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6649
6650 // Test all four corners.
6651 for (int i = 0; i < 4; i++) {
6652 const auto& expected = expectedPoints[i];
6653 const auto& raw = kRawCorners[i];
6654 processDown(mapper, raw.x, raw.y);
6655 processSync(mapper);
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6657 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6658 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6659 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6660 << "with display rotation " << ui::toCString(displayRotation)
6661 << ", expected point (" << expected.x << ", " << expected.y << ").";
6662 processUp(mapper);
6663 processSync(mapper);
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6665 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6666 WithCoords(expected.x, expected.y))));
6667 }
6668}
6669
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006670TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6671 const ui::Rotation displayRotation = GetParam();
6672
6673 addConfigurationProperty("touch.deviceType", "touchScreen");
6674 prepareDisplay(displayRotation);
6675
6676 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006677 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006678
6679 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6680 // MotionRanges use display pixels as their units
6681 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6682 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6683
6684 // The MotionRanges should be oriented in the rotated display's coordinate space
6685 const bool displayRotated =
6686 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6687
6688 constexpr float MAX_X = 479.5;
6689 constexpr float MAX_Y = 799.75;
6690 EXPECT_EQ(xRange->min, 0.f);
6691 EXPECT_EQ(yRange->min, 0.f);
6692 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6693 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6694
6695 EXPECT_EQ(xRange->flat, 8.f);
6696 EXPECT_EQ(yRange->flat, 8.f);
6697
6698 EXPECT_EQ(xRange->fuzz, 2.f);
6699 EXPECT_EQ(yRange->fuzz, 2.f);
6700
6701 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6702 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6703}
6704
Prabir Pradhana9df3162022-12-05 23:57:27 +00006705// Run the precision tests for all rotations.
6706INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6707 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6708 ui::ROTATION_270),
6709 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6710 return ftl::enum_string(testParamInfo.param);
6711 });
6712
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006713// --- ExternalStylusFusionTest ---
6714
6715class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6716public:
6717 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6718 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006719 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006720 prepareButtons();
6721 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006722 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006723
6724 mStylusState.when = ARBITRARY_TIME;
6725 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006726 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006727 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006728 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006729 processExternalStylusState(mapper);
6730 return mapper;
6731 }
6732
6733 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6734 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6735 for (const NotifyArgs& args : generatedArgs) {
6736 mFakeListener->notify(args);
6737 }
6738 // Loop the reader to flush the input listener queue.
6739 mReader->loopOnce();
6740 return generatedArgs;
6741 }
6742
6743protected:
6744 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006745
6746 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6747 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006748 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006749
6750 // The first pointer is withheld.
6751 processDown(mapper, 100, 200);
6752 processSync(mapper);
6753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6754 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6755 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6756
6757 // The external stylus reports pressure. The withheld finger pointer is released as a
6758 // stylus.
6759 mStylusState.pressure = 1.f;
6760 processExternalStylusState(mapper);
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6762 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6763 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6764
6765 // Subsequent pointer events are not withheld.
6766 processMove(mapper, 101, 201);
6767 processSync(mapper);
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6769 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6770
6771 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6773 }
6774
6775 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6776 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6777
6778 // Releasing the touch pointer ends the gesture.
6779 processUp(mapper);
6780 processSync(mapper);
6781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006782 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006783 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006784
6785 mStylusState.pressure = 0.f;
6786 processExternalStylusState(mapper);
6787 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6789 }
6790
6791 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006792 // When stylus fusion is not successful, events should be reported with the original source.
6793 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006794 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006795 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006796
6797 // The first pointer is withheld when an external stylus is connected,
6798 // and a timeout is requested.
6799 processDown(mapper, 100, 200);
6800 processSync(mapper);
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6802 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6803 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6804
6805 // If the timeout expires early, it is requested again.
6806 handleTimeout(mapper, ARBITRARY_TIME + 1);
6807 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6808 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6809
6810 // When the timeout expires, the withheld touch is released as a finger pointer.
6811 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6813 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6814
6815 // Subsequent pointer events are not withheld.
6816 processMove(mapper, 101, 201);
6817 processSync(mapper);
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6819 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6820 processUp(mapper);
6821 processSync(mapper);
6822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6823 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6824
6825 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6827 }
6828
6829private:
6830 InputDeviceInfo mExternalStylusDeviceInfo{};
6831};
6832
6833TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6834 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006835 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006836}
6837
6838TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6839 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6840 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6841}
6842
6843TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6844 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6845 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6846}
6847
6848// Test a successful stylus fusion gesture where the pressure is reported by the external
6849// before the touch is reported by the touchscreen.
6850TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6851 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006852 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006853
6854 // The external stylus reports pressure first. It is ignored for now.
6855 mStylusState.pressure = 1.f;
6856 processExternalStylusState(mapper);
6857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6858 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6859
6860 // When the touch goes down afterwards, it is reported as a stylus pointer.
6861 processDown(mapper, 100, 200);
6862 processSync(mapper);
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6864 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6865 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6866
6867 processMove(mapper, 101, 201);
6868 processSync(mapper);
6869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6870 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6871 processUp(mapper);
6872 processSync(mapper);
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6874 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6875
6876 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6878}
6879
6880TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6881 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6882
6883 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6884 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6885
6886 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6887 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6888 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6889 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6890}
6891
6892TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6893 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006894 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006895
6896 mStylusState.pressure = 0.8f;
6897 processExternalStylusState(mapper);
6898 processDown(mapper, 100, 200);
6899 processSync(mapper);
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6901 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6902 WithPressure(0.8f))));
6903 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6904
6905 // The external stylus reports a pressure change. We wait for some time for a touch event.
6906 mStylusState.pressure = 0.6f;
6907 processExternalStylusState(mapper);
6908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6909 ASSERT_NO_FATAL_FAILURE(
6910 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6911
6912 // If a touch is reported within the timeout, it reports the updated pressure.
6913 processMove(mapper, 101, 201);
6914 processSync(mapper);
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6916 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6917 WithPressure(0.6f))));
6918 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6919
6920 // There is another pressure change.
6921 mStylusState.pressure = 0.5f;
6922 processExternalStylusState(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6924 ASSERT_NO_FATAL_FAILURE(
6925 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6926
6927 // If a touch is not reported within the timeout, a move event is generated to report
6928 // the new pressure.
6929 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6931 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6932 WithPressure(0.5f))));
6933
6934 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6935 // repeated indefinitely.
6936 mStylusState.pressure = 0.0f;
6937 processExternalStylusState(mapper);
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6939 ASSERT_NO_FATAL_FAILURE(
6940 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6941 processMove(mapper, 102, 202);
6942 processSync(mapper);
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6944 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6945 WithPressure(0.5f))));
6946 processMove(mapper, 103, 203);
6947 processSync(mapper);
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6949 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6950 WithPressure(0.5f))));
6951
6952 processUp(mapper);
6953 processSync(mapper);
6954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006955 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006956 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006957
6958 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6960}
6961
6962TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6963 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006964 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006965
6966 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006967 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006968 processExternalStylusState(mapper);
6969 processDown(mapper, 100, 200);
6970 processSync(mapper);
6971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6972 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006973 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006974 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6975
6976 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006977 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006978 processExternalStylusState(mapper);
6979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6980 ASSERT_NO_FATAL_FAILURE(
6981 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6982
6983 // If a touch is reported within the timeout, it reports the updated pressure.
6984 processMove(mapper, 101, 201);
6985 processSync(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6987 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006988 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006989 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6990
6991 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006992 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006993 processExternalStylusState(mapper);
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6995 ASSERT_NO_FATAL_FAILURE(
6996 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6997
6998 // If a touch is not reported within the timeout, a move event is generated to report
6999 // the new tool type.
7000 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7002 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007003 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007004
7005 processUp(mapper);
7006 processSync(mapper);
7007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7008 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007009 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007010
7011 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7013}
7014
7015TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7016 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007017 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007018
7019 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7020
7021 // The external stylus reports a button change. We wait for some time for a touch event.
7022 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7023 processExternalStylusState(mapper);
7024 ASSERT_NO_FATAL_FAILURE(
7025 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7026
7027 // If a touch is reported within the timeout, it reports the updated button state.
7028 processMove(mapper, 101, 201);
7029 processSync(mapper);
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7031 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7032 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7034 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7035 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7036 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7037
7038 // The button is now released.
7039 mStylusState.buttons = 0;
7040 processExternalStylusState(mapper);
7041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7042 ASSERT_NO_FATAL_FAILURE(
7043 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7044
7045 // If a touch is not reported within the timeout, a move event is generated to report
7046 // the new button state.
7047 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7049 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7050 WithButtonState(0))));
7051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007052 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7053 WithButtonState(0))));
7054
7055 processUp(mapper);
7056 processSync(mapper);
7057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007058 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7059
7060 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7062}
7063
Michael Wrightd02c5b62014-02-10 15:10:22 -08007064// --- MultiTouchInputMapperTest ---
7065
7066class MultiTouchInputMapperTest : public TouchInputMapperTest {
7067protected:
7068 void prepareAxes(int axes);
7069
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007070 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7071 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7072 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7073 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7074 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7075 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7076 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7077 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7078 void processId(MultiTouchInputMapper& mapper, int32_t id);
7079 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7080 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7081 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007082 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007083 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007084 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7085 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007086};
7087
7088void MultiTouchInputMapperTest::prepareAxes(int axes) {
7089 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007090 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007092 }
7093 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7095 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7098 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 }
7100 }
7101 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007102 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7103 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007104 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007105 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007106 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007107 }
7108 }
7109 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007110 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7111 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112 }
7113 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007114 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7115 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116 }
7117 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007118 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7119 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120 }
7121 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007122 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7123 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124 }
7125 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007126 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7127 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007128 }
7129 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 }
7132}
7133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007134void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7135 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7137 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138}
7139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007140void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7141 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143}
7144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007145void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7146 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007148}
7149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007150void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152}
7153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007154void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156}
7157
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007158void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7159 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161}
7162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007163void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007165}
7166
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007167void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007169}
7170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007171void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173}
7174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007175void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177}
7178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007179void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181}
7182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007183void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7184 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186}
7187
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007188void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7189 int32_t value) {
7190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7192}
7193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007194void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196}
7197
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007198void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7199 nsecs_t readTime) {
7200 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007201}
7202
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007205 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206 prepareAxes(POSITION);
7207 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007208 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007209
arthurhungdcef2dc2020-08-11 14:47:50 +08007210 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211
7212 NotifyMotionArgs motionArgs;
7213
7214 // Two fingers down at once.
7215 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7216 processPosition(mapper, x1, y1);
7217 processMTSync(mapper);
7218 processPosition(mapper, x2, y2);
7219 processMTSync(mapper);
7220 processSync(mapper);
7221
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7224 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7226 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7228 ASSERT_EQ(0, motionArgs.flags);
7229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7230 ASSERT_EQ(0, motionArgs.buttonState);
7231 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007232 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007234 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7236 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7237 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7238 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
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);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007246 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247 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(2), 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_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007255 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007256 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7257 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7259 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7260 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7261 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7263
7264 // Move.
7265 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7266 processPosition(mapper, x1, y1);
7267 processMTSync(mapper);
7268 processPosition(mapper, x2, y2);
7269 processMTSync(mapper);
7270 processSync(mapper);
7271
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7278 ASSERT_EQ(0, motionArgs.flags);
7279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7280 ASSERT_EQ(0, motionArgs.buttonState);
7281 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007282 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007284 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007285 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007286 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7288 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7290 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7291 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7292 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7294
7295 // First finger up.
7296 x2 += 15; y2 -= 20;
7297 processPosition(mapper, x2, y2);
7298 processMTSync(mapper);
7299 processSync(mapper);
7300
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7302 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7303 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7304 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7305 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007306 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007307 ASSERT_EQ(0, motionArgs.flags);
7308 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7309 ASSERT_EQ(0, motionArgs.buttonState);
7310 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007311 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007312 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007313 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007314 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007315 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7317 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7318 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7319 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7320 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7321 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7322 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7323
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7326 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7327 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7328 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7329 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7330 ASSERT_EQ(0, motionArgs.flags);
7331 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7332 ASSERT_EQ(0, motionArgs.buttonState);
7333 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007334 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007335 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007336 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
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 // Move.
7344 x2 += 20; y2 -= 25;
7345 processPosition(mapper, x2, y2);
7346 processMTSync(mapper);
7347 processSync(mapper);
7348
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7355 ASSERT_EQ(0, motionArgs.flags);
7356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7357 ASSERT_EQ(0, motionArgs.buttonState);
7358 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007359 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007361 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7363 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7367
7368 // New finger down.
7369 int32_t x3 = 700, y3 = 300;
7370 processPosition(mapper, x2, y2);
7371 processMTSync(mapper);
7372 processPosition(mapper, x3, y3);
7373 processMTSync(mapper);
7374 processSync(mapper);
7375
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7378 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7379 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7380 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007381 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007382 ASSERT_EQ(0, motionArgs.flags);
7383 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7384 ASSERT_EQ(0, motionArgs.buttonState);
7385 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007386 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007387 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007388 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007389 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007390 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7392 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7394 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7395 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7396 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7397 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7398
7399 // Second finger up.
7400 x3 += 30; y3 -= 20;
7401 processPosition(mapper, x3, y3);
7402 processMTSync(mapper);
7403 processSync(mapper);
7404
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7407 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7408 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7409 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007410 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007411 ASSERT_EQ(0, motionArgs.flags);
7412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7413 ASSERT_EQ(0, motionArgs.buttonState);
7414 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007415 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007417 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007418 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007419 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7421 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7423 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7424 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7425 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7426 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7427
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7430 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7431 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7432 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7433 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7434 ASSERT_EQ(0, motionArgs.flags);
7435 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7436 ASSERT_EQ(0, motionArgs.buttonState);
7437 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007438 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007440 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7442 toDisplayX(x3), toDisplayY(y3), 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 // Last finger up.
7448 processMTSync(mapper);
7449 processSync(mapper);
7450
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7453 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7454 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7455 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7456 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7457 ASSERT_EQ(0, motionArgs.flags);
7458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7459 ASSERT_EQ(0, motionArgs.buttonState);
7460 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007461 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007463 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7465 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7466 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7467 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7468 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7469
7470 // Should not have sent any more keys or motions.
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7473}
7474
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007475TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7476 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007477 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007478
7479 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7480 /*fuzz*/ 0, /*resolution*/ 10);
7481 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7482 /*fuzz*/ 0, /*resolution*/ 11);
7483 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7484 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7485 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7486 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7487 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7488 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7489 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7490 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7491
Arpit Singha8c236b2023-04-25 13:56:05 +00007492 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007493
7494 // X and Y axes
7495 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7496 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7497 // Touch major and minor
7498 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7499 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7500 // Tool major and minor
7501 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7502 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7503}
7504
7505TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7506 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007507 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007508
7509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7510 /*fuzz*/ 0, /*resolution*/ 10);
7511 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7512 /*fuzz*/ 0, /*resolution*/ 11);
7513
7514 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7515
Arpit Singha8c236b2023-04-25 13:56:05 +00007516 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007517
7518 // Touch major and minor
7519 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7520 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7521 // Tool major and minor
7522 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7523 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7524}
7525
Michael Wrightd02c5b62014-02-10 15:10:22 -08007526TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007527 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007528 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007529 prepareAxes(POSITION | ID);
7530 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007531 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007532
arthurhungdcef2dc2020-08-11 14:47:50 +08007533 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007534
7535 NotifyMotionArgs motionArgs;
7536
7537 // Two fingers down at once.
7538 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7539 processPosition(mapper, x1, y1);
7540 processId(mapper, 1);
7541 processMTSync(mapper);
7542 processPosition(mapper, x2, y2);
7543 processId(mapper, 2);
7544 processMTSync(mapper);
7545 processSync(mapper);
7546
7547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7548 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007549 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007550 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007551 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7553 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7554
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007556 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007557 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007558 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007559 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007560 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007561 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7563 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7565 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7566
7567 // Move.
7568 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7569 processPosition(mapper, x1, y1);
7570 processId(mapper, 1);
7571 processMTSync(mapper);
7572 processPosition(mapper, x2, y2);
7573 processId(mapper, 2);
7574 processMTSync(mapper);
7575 processSync(mapper);
7576
7577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007579 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007580 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007581 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007582 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007583 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7585 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7587 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7588
7589 // First finger up.
7590 x2 += 15; y2 -= 20;
7591 processPosition(mapper, x2, y2);
7592 processId(mapper, 2);
7593 processMTSync(mapper);
7594 processSync(mapper);
7595
7596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007597 ASSERT_EQ(ACTION_POINTER_0_UP, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007610 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007612 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7614 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7615
7616 // Move.
7617 x2 += 20; y2 -= 25;
7618 processPosition(mapper, x2, y2);
7619 processId(mapper, 2);
7620 processMTSync(mapper);
7621 processSync(mapper);
7622
7623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7624 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007625 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007627 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7629 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7630
7631 // New finger down.
7632 int32_t x3 = 700, y3 = 300;
7633 processPosition(mapper, x2, y2);
7634 processId(mapper, 2);
7635 processMTSync(mapper);
7636 processPosition(mapper, x3, y3);
7637 processId(mapper, 3);
7638 processMTSync(mapper);
7639 processSync(mapper);
7640
7641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007642 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007643 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007644 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007645 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007647 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7649 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7651 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7652
7653 // Second finger up.
7654 x3 += 30; y3 -= 20;
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_1_UP, 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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007674 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007676 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7678 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7679
7680 // Last finger up.
7681 processMTSync(mapper);
7682 processSync(mapper);
7683
7684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7685 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007686 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007687 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007688 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7690 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7691
7692 // Should not have sent any more keys or motions.
7693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7695}
7696
7697TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007698 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007699 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007700 prepareAxes(POSITION | ID | SLOT);
7701 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007702 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007703
arthurhungdcef2dc2020-08-11 14:47:50 +08007704 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007705
7706 NotifyMotionArgs motionArgs;
7707
7708 // Two fingers down at once.
7709 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7710 processPosition(mapper, x1, y1);
7711 processId(mapper, 1);
7712 processSlot(mapper, 1);
7713 processPosition(mapper, x2, y2);
7714 processId(mapper, 2);
7715 processSync(mapper);
7716
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7718 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007719 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007720 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007721 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7723 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7724
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007726 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007727 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007729 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007730 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007731 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7733 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7735 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7736
7737 // Move.
7738 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7739 processSlot(mapper, 0);
7740 processPosition(mapper, x1, y1);
7741 processSlot(mapper, 1);
7742 processPosition(mapper, x2, y2);
7743 processSync(mapper);
7744
7745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7746 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007747 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007748 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007749 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007750 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007751 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7753 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7754 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7755 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7756
7757 // First finger up.
7758 x2 += 15; y2 -= 20;
7759 processSlot(mapper, 0);
7760 processId(mapper, -1);
7761 processSlot(mapper, 1);
7762 processPosition(mapper, x2, y2);
7763 processSync(mapper);
7764
7765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007766 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007767 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007768 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007769 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007770 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007771 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7773 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7774 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7775 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7776
7777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7778 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007779 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007780 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007781 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007782 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7783 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7784
7785 // Move.
7786 x2 += 20; y2 -= 25;
7787 processPosition(mapper, x2, y2);
7788 processSync(mapper);
7789
7790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007792 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007793 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007794 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7796 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7797
7798 // New finger down.
7799 int32_t x3 = 700, y3 = 300;
7800 processPosition(mapper, x2, y2);
7801 processSlot(mapper, 0);
7802 processId(mapper, 3);
7803 processPosition(mapper, x3, y3);
7804 processSync(mapper);
7805
7806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007807 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007808 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007809 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007810 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007811 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007812 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7814 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7816 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7817
7818 // Second finger up.
7819 x3 += 30; y3 -= 20;
7820 processSlot(mapper, 1);
7821 processId(mapper, -1);
7822 processSlot(mapper, 0);
7823 processPosition(mapper, x3, y3);
7824 processSync(mapper);
7825
7826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007827 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007828 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007829 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007830 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007831 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007832 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7834 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7836 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7837
7838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007840 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007841 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007842 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7844 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7845
7846 // Last finger up.
7847 processId(mapper, -1);
7848 processSync(mapper);
7849
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7851 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007852 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007854 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7856 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7857
7858 // Should not have sent any more keys or motions.
7859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7861}
7862
7863TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007865 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007866 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007867 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007868
7869 // These calculations are based on the input device calibration documentation.
7870 int32_t rawX = 100;
7871 int32_t rawY = 200;
7872 int32_t rawTouchMajor = 7;
7873 int32_t rawTouchMinor = 6;
7874 int32_t rawToolMajor = 9;
7875 int32_t rawToolMinor = 8;
7876 int32_t rawPressure = 11;
7877 int32_t rawDistance = 0;
7878 int32_t rawOrientation = 3;
7879 int32_t id = 5;
7880
7881 float x = toDisplayX(rawX);
7882 float y = toDisplayY(rawY);
7883 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7884 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7885 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7886 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7887 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7888 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7889 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7890 float distance = float(rawDistance);
7891
7892 processPosition(mapper, rawX, rawY);
7893 processTouchMajor(mapper, rawTouchMajor);
7894 processTouchMinor(mapper, rawTouchMinor);
7895 processToolMajor(mapper, rawToolMajor);
7896 processToolMinor(mapper, rawToolMinor);
7897 processPressure(mapper, rawPressure);
7898 processOrientation(mapper, rawOrientation);
7899 processDistance(mapper, rawDistance);
7900 processId(mapper, id);
7901 processMTSync(mapper);
7902 processSync(mapper);
7903
7904 NotifyMotionArgs args;
7905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7906 ASSERT_EQ(0, args.pointerProperties[0].id);
7907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7908 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7909 orientation, distance));
7910}
7911
7912TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007914 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7916 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007917 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007918
7919 // These calculations are based on the input device calibration documentation.
7920 int32_t rawX = 100;
7921 int32_t rawY = 200;
7922 int32_t rawTouchMajor = 140;
7923 int32_t rawTouchMinor = 120;
7924 int32_t rawToolMajor = 180;
7925 int32_t rawToolMinor = 160;
7926
7927 float x = toDisplayX(rawX);
7928 float y = toDisplayY(rawY);
7929 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7930 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7931 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7932 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7933 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7934
7935 processPosition(mapper, rawX, rawY);
7936 processTouchMajor(mapper, rawTouchMajor);
7937 processTouchMinor(mapper, rawTouchMinor);
7938 processToolMajor(mapper, rawToolMajor);
7939 processToolMinor(mapper, rawToolMinor);
7940 processMTSync(mapper);
7941 processSync(mapper);
7942
7943 NotifyMotionArgs args;
7944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7946 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7947}
7948
7949TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007950 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007951 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952 prepareAxes(POSITION | TOUCH | TOOL);
7953 addConfigurationProperty("touch.size.calibration", "diameter");
7954 addConfigurationProperty("touch.size.scale", "10");
7955 addConfigurationProperty("touch.size.bias", "160");
7956 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007957 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958
7959 // These calculations are based on the input device calibration documentation.
7960 // Note: We only provide a single common touch/tool value because the device is assumed
7961 // not to emit separate values for each pointer (isSummed = 1).
7962 int32_t rawX = 100;
7963 int32_t rawY = 200;
7964 int32_t rawX2 = 150;
7965 int32_t rawY2 = 250;
7966 int32_t rawTouchMajor = 5;
7967 int32_t rawToolMajor = 8;
7968
7969 float x = toDisplayX(rawX);
7970 float y = toDisplayY(rawY);
7971 float x2 = toDisplayX(rawX2);
7972 float y2 = toDisplayY(rawY2);
7973 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7974 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7975 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7976
7977 processPosition(mapper, rawX, rawY);
7978 processTouchMajor(mapper, rawTouchMajor);
7979 processToolMajor(mapper, rawToolMajor);
7980 processMTSync(mapper);
7981 processPosition(mapper, rawX2, rawY2);
7982 processTouchMajor(mapper, rawTouchMajor);
7983 processToolMajor(mapper, rawToolMajor);
7984 processMTSync(mapper);
7985 processSync(mapper);
7986
7987 NotifyMotionArgs args;
7988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7989 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7990
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007992 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007993 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7995 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7997 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7998}
7999
8000TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008002 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008003 prepareAxes(POSITION | TOUCH | TOOL);
8004 addConfigurationProperty("touch.size.calibration", "area");
8005 addConfigurationProperty("touch.size.scale", "43");
8006 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008007 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008
8009 // These calculations are based on the input device calibration documentation.
8010 int32_t rawX = 100;
8011 int32_t rawY = 200;
8012 int32_t rawTouchMajor = 5;
8013 int32_t rawToolMajor = 8;
8014
8015 float x = toDisplayX(rawX);
8016 float y = toDisplayY(rawY);
8017 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8018 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8019 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8020
8021 processPosition(mapper, rawX, rawY);
8022 processTouchMajor(mapper, rawTouchMajor);
8023 processToolMajor(mapper, rawToolMajor);
8024 processMTSync(mapper);
8025 processSync(mapper);
8026
8027 NotifyMotionArgs args;
8028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8030 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8031}
8032
8033TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008035 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 prepareAxes(POSITION | PRESSURE);
8037 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8038 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008039 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008040
Michael Wrightaa449c92017-12-13 21:21:43 +00008041 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008042 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008043 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8044 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8045 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8046
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047 // These calculations are based on the input device calibration documentation.
8048 int32_t rawX = 100;
8049 int32_t rawY = 200;
8050 int32_t rawPressure = 60;
8051
8052 float x = toDisplayX(rawX);
8053 float y = toDisplayY(rawY);
8054 float pressure = float(rawPressure) * 0.01f;
8055
8056 processPosition(mapper, rawX, rawY);
8057 processPressure(mapper, rawPressure);
8058 processMTSync(mapper);
8059 processSync(mapper);
8060
8061 NotifyMotionArgs args;
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8064 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8065}
8066
8067TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008068 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008069 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008071 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072
8073 NotifyMotionArgs motionArgs;
8074 NotifyKeyArgs keyArgs;
8075
8076 processId(mapper, 1);
8077 processPosition(mapper, 100, 200);
8078 processSync(mapper);
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8081 ASSERT_EQ(0, motionArgs.buttonState);
8082
8083 // press BTN_LEFT, release BTN_LEFT
8084 processKey(mapper, BTN_LEFT, 1);
8085 processSync(mapper);
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8087 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8088 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8089
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8091 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8092 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8093
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 processKey(mapper, BTN_LEFT, 0);
8095 processSync(mapper);
8096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008098 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008099
8100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008102 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008103
8104 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8105 processKey(mapper, BTN_RIGHT, 1);
8106 processKey(mapper, BTN_MIDDLE, 1);
8107 processSync(mapper);
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8110 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8111 motionArgs.buttonState);
8112
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8114 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8115 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8116
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8118 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8119 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8120 motionArgs.buttonState);
8121
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 processKey(mapper, BTN_RIGHT, 0);
8123 processSync(mapper);
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008127
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008130 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008131
8132 processKey(mapper, BTN_MIDDLE, 0);
8133 processSync(mapper);
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008135 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137
8138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008140 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141
8142 // press BTN_BACK, release BTN_BACK
8143 processKey(mapper, BTN_BACK, 1);
8144 processSync(mapper);
8145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8146 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8147 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008148
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008151 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8152
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8155 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156
8157 processKey(mapper, BTN_BACK, 0);
8158 processSync(mapper);
8159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008165 ASSERT_EQ(0, motionArgs.buttonState);
8166
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8168 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8169 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8170
8171 // press BTN_SIDE, release BTN_SIDE
8172 processKey(mapper, BTN_SIDE, 1);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8175 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8176 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008177
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008180 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8181
8182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8184 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185
8186 processKey(mapper, BTN_SIDE, 0);
8187 processSync(mapper);
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008194 ASSERT_EQ(0, motionArgs.buttonState);
8195
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8197 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8198 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8199
8200 // press BTN_FORWARD, release BTN_FORWARD
8201 processKey(mapper, BTN_FORWARD, 1);
8202 processSync(mapper);
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8204 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8205 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008206
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8210
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214
8215 processKey(mapper, BTN_FORWARD, 0);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223 ASSERT_EQ(0, motionArgs.buttonState);
8224
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8226 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8227 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8228
8229 // press BTN_EXTRA, release BTN_EXTRA
8230 processKey(mapper, BTN_EXTRA, 1);
8231 processSync(mapper);
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8233 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8234 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008235
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008238 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8239
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8242 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243
8244 processKey(mapper, BTN_EXTRA, 0);
8245 processSync(mapper);
8246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008248 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008249
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008252 ASSERT_EQ(0, motionArgs.buttonState);
8253
Michael Wrightd02c5b62014-02-10 15:10:22 -08008254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8255 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8256 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8257
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8259
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260 // press BTN_STYLUS, release BTN_STYLUS
8261 processKey(mapper, BTN_STYLUS, 1);
8262 processSync(mapper);
8263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008265 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8266
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8269 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008270
8271 processKey(mapper, BTN_STYLUS, 0);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008274 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008275 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008276
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008278 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008279 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280
8281 // press BTN_STYLUS2, release BTN_STYLUS2
8282 processKey(mapper, BTN_STYLUS2, 1);
8283 processSync(mapper);
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008286 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8287
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8290 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008291
8292 processKey(mapper, BTN_STYLUS2, 0);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008295 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008296 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008297
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008300 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008301
8302 // release touch
8303 processId(mapper, -1);
8304 processSync(mapper);
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8306 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8307 ASSERT_EQ(0, motionArgs.buttonState);
8308}
8309
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008310TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8311 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008312 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008313 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008314 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008315
8316 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8317 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8318
8319 // Touch down.
8320 processId(mapper, 1);
8321 processPosition(mapper, 100, 200);
8322 processSync(mapper);
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8324 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8325
8326 // Press and release button mapped to the primary stylus button.
8327 processKey(mapper, BTN_A, 1);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8330 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8331 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8333 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8334 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8335
8336 processKey(mapper, BTN_A, 0);
8337 processSync(mapper);
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8339 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8341 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8342
8343 // Press and release the HID usage mapped to the secondary stylus button.
8344 processHidUsage(mapper, 0xabcd, 1);
8345 processSync(mapper);
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8347 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8348 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8350 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8351 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8352
8353 processHidUsage(mapper, 0xabcd, 0);
8354 processSync(mapper);
8355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8356 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8358 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8359
8360 // Release touch.
8361 processId(mapper, -1);
8362 processSync(mapper);
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8364 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8365}
8366
Michael Wrightd02c5b62014-02-10 15:10:22 -08008367TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008368 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008369 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008370 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008371 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372
8373 NotifyMotionArgs motionArgs;
8374
8375 // default tool type is finger
8376 processId(mapper, 1);
8377 processPosition(mapper, 100, 200);
8378 processSync(mapper);
8379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8380 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008381 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008382
8383 // eraser
8384 processKey(mapper, BTN_TOOL_RUBBER, 1);
8385 processSync(mapper);
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008388 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389
8390 // stylus
8391 processKey(mapper, BTN_TOOL_RUBBER, 0);
8392 processKey(mapper, BTN_TOOL_PEN, 1);
8393 processSync(mapper);
8394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008396 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397
8398 // brush
8399 processKey(mapper, BTN_TOOL_PEN, 0);
8400 processKey(mapper, BTN_TOOL_BRUSH, 1);
8401 processSync(mapper);
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008404 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008405
8406 // pencil
8407 processKey(mapper, BTN_TOOL_BRUSH, 0);
8408 processKey(mapper, BTN_TOOL_PENCIL, 1);
8409 processSync(mapper);
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008412 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008414 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008415 processKey(mapper, BTN_TOOL_PENCIL, 0);
8416 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8417 processSync(mapper);
8418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008420 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008421
8422 // mouse
8423 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8424 processKey(mapper, BTN_TOOL_MOUSE, 1);
8425 processSync(mapper);
8426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008428 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429
8430 // lens
8431 processKey(mapper, BTN_TOOL_MOUSE, 0);
8432 processKey(mapper, BTN_TOOL_LENS, 1);
8433 processSync(mapper);
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008436 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008437
8438 // double-tap
8439 processKey(mapper, BTN_TOOL_LENS, 0);
8440 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8441 processSync(mapper);
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008444 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445
8446 // triple-tap
8447 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8448 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8449 processSync(mapper);
8450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008452 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008453
8454 // quad-tap
8455 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8456 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8457 processSync(mapper);
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008460 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008461
8462 // finger
8463 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8464 processKey(mapper, BTN_TOOL_FINGER, 1);
8465 processSync(mapper);
8466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008468 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008469
8470 // stylus trumps finger
8471 processKey(mapper, BTN_TOOL_PEN, 1);
8472 processSync(mapper);
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008475 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476
8477 // eraser trumps stylus
8478 processKey(mapper, BTN_TOOL_RUBBER, 1);
8479 processSync(mapper);
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008482 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483
8484 // mouse trumps eraser
8485 processKey(mapper, BTN_TOOL_MOUSE, 1);
8486 processSync(mapper);
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008489 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490
8491 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8492 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8493 processSync(mapper);
8494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8495 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008496 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008497
8498 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8499 processToolType(mapper, MT_TOOL_PEN);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008503 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504
8505 // back to default tool type
8506 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8507 processKey(mapper, BTN_TOOL_MOUSE, 0);
8508 processKey(mapper, BTN_TOOL_RUBBER, 0);
8509 processKey(mapper, BTN_TOOL_PEN, 0);
8510 processKey(mapper, BTN_TOOL_FINGER, 0);
8511 processSync(mapper);
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008514 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008515}
8516
8517TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008518 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008519 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008520 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008521 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008522 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008523
8524 NotifyMotionArgs motionArgs;
8525
8526 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8527 processId(mapper, 1);
8528 processPosition(mapper, 100, 200);
8529 processSync(mapper);
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8532 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8533 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8534
8535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8536 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8538 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8539
8540 // move a little
8541 processPosition(mapper, 150, 250);
8542 processSync(mapper);
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8544 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8545 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8546 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8547
8548 // down when BTN_TOUCH is pressed, pressure defaults to 1
8549 processKey(mapper, BTN_TOUCH, 1);
8550 processSync(mapper);
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8554 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8555
8556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8557 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8558 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8559 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8560
8561 // up when BTN_TOUCH is released, hover restored
8562 processKey(mapper, BTN_TOUCH, 0);
8563 processSync(mapper);
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8565 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8567 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8568
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8570 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8572 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8573
8574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8575 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8577 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8578
8579 // exit hover when pointer goes away
8580 processId(mapper, -1);
8581 processSync(mapper);
8582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8583 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8585 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8586}
8587
8588TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008589 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008590 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008591 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008592 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008593
8594 NotifyMotionArgs motionArgs;
8595
8596 // initially hovering because pressure is 0
8597 processId(mapper, 1);
8598 processPosition(mapper, 100, 200);
8599 processPressure(mapper, 0);
8600 processSync(mapper);
8601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8602 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8604 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8605
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8607 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8609 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8610
8611 // move a little
8612 processPosition(mapper, 150, 250);
8613 processSync(mapper);
8614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8615 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8617 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8618
8619 // down when pressure becomes non-zero
8620 processPressure(mapper, RAW_PRESSURE_MAX);
8621 processSync(mapper);
8622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8623 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8625 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8626
8627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8628 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8630 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8631
8632 // up when pressure becomes 0, hover restored
8633 processPressure(mapper, 0);
8634 processSync(mapper);
8635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8636 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8638 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8639
8640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8641 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8642 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8643 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8644
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8648 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8649
8650 // exit hover when pointer goes away
8651 processId(mapper, -1);
8652 processSync(mapper);
8653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8654 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8656 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8657}
8658
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008659/**
8660 * Set the input device port <--> display port associations, and check that the
8661 * events are routed to the display that matches the display port.
8662 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8663 */
8664TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008665 const std::string usb2 = "USB2";
8666 const uint8_t hdmi1 = 0;
8667 const uint8_t hdmi2 = 1;
8668 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008669 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008670
8671 addConfigurationProperty("touch.deviceType", "touchScreen");
8672 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008673 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008674
8675 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8676 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8677
8678 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8679 // for this input device is specified, and the matching viewport is not present,
8680 // the input device should be disabled (at the mapper level).
8681
8682 // Add viewport for display 2 on hdmi2
8683 prepareSecondaryDisplay(type, hdmi2);
8684 // Send a touch event
8685 processPosition(mapper, 100, 100);
8686 processSync(mapper);
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8688
8689 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008690 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008691 // Send a touch event again
8692 processPosition(mapper, 100, 100);
8693 processSync(mapper);
8694
8695 NotifyMotionArgs args;
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8697 ASSERT_EQ(DISPLAY_ID, args.displayId);
8698}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008699
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008700TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8701 addConfigurationProperty("touch.deviceType", "touchScreen");
8702 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008703 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008704
8705 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8706
Michael Wrighta9cf4192022-12-01 23:46:39 +00008707 prepareDisplay(ui::ROTATION_0);
8708 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008709
8710 // Send a touch event
8711 processPosition(mapper, 100, 100);
8712 processSync(mapper);
8713
8714 NotifyMotionArgs args;
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8716 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8717}
8718
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008719TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008720 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008721 std::shared_ptr<FakePointerController> fakePointerController =
8722 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008723 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008724 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008725 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008726
Garfield Tan888a6a42020-01-09 11:39:16 -08008727 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008728 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008729
Michael Wrighta9cf4192022-12-01 23:46:39 +00008730 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008731 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008732 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008733
Josep del Río2d8c79a2023-01-23 19:33:50 +00008734 // Check source is mouse that would obtain the PointerController.
8735 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008736
8737 NotifyMotionArgs motionArgs;
8738 processPosition(mapper, 100, 100);
8739 processSync(mapper);
8740
8741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8742 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8743 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8744}
8745
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008746/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008747 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8748 */
8749TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8750 addConfigurationProperty("touch.deviceType", "touchScreen");
8751 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008752 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008753
Michael Wrighta9cf4192022-12-01 23:46:39 +00008754 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008755 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8756 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8757 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8758 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008759
8760 NotifyMotionArgs args;
8761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8762 ASSERT_EQ(26, args.readTime);
8763
Harry Cutts33476232023-01-30 19:57:29 +00008764 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8765 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8766 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008767
8768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8769 ASSERT_EQ(33, args.readTime);
8770}
8771
8772/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008773 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8774 * events should not be delivered to the listener.
8775 */
8776TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8777 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008778 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008779 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008780 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008781 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008782 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008783 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008784
8785 NotifyMotionArgs motionArgs;
8786 processPosition(mapper, 100, 100);
8787 processSync(mapper);
8788
8789 mFakeListener->assertNotifyMotionWasNotCalled();
8790}
8791
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008792/**
8793 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8794 * the touch mapper can process the events and the events can be delivered to the listener.
8795 */
8796TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8797 addConfigurationProperty("touch.deviceType", "touchScreen");
8798 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008799 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008800 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008801 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008802 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008803 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008804
8805 NotifyMotionArgs motionArgs;
8806 processPosition(mapper, 100, 100);
8807 processSync(mapper);
8808
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8810 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8811}
8812
Josh Thielene986aed2023-06-01 14:17:30 +00008813/**
8814 * When the viewport is deactivated (isActive transitions from true to false),
8815 * and touch.enableForInactiveViewport is false, touches prior to the transition
8816 * should be cancelled.
8817 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008818TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8819 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008820 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008821 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008822 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008823 std::optional<DisplayViewport> optionalDisplayViewport =
8824 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8825 ASSERT_TRUE(optionalDisplayViewport.has_value());
8826 DisplayViewport displayViewport = *optionalDisplayViewport;
8827
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008828 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008829 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008830 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008831
8832 // Finger down
8833 int32_t x = 100, y = 100;
8834 processPosition(mapper, x, y);
8835 processSync(mapper);
8836
8837 NotifyMotionArgs motionArgs;
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8839 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8840
8841 // Deactivate display viewport
8842 displayViewport.isActive = false;
8843 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008844 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008845
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008846 // The ongoing touch should be canceled immediately
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8848 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8849
8850 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008851 x += 10, y += 10;
8852 processPosition(mapper, x, y);
8853 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008855
8856 // Reactivate display viewport
8857 displayViewport.isActive = true;
8858 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008859 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008860
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008861 // Finger move again starts new gesture
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->assertNotifyMotionWasCalled(&motionArgs));
8866 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008867}
8868
Josh Thielene986aed2023-06-01 14:17:30 +00008869/**
8870 * When the viewport is deactivated (isActive transitions from true to false),
8871 * and touch.enableForInactiveViewport is true, touches prior to the transition
8872 * should not be cancelled.
8873 */
8874TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8875 addConfigurationProperty("touch.deviceType", "touchScreen");
8876 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8877 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8878 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8879 std::optional<DisplayViewport> optionalDisplayViewport =
8880 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8881 ASSERT_TRUE(optionalDisplayViewport.has_value());
8882 DisplayViewport displayViewport = *optionalDisplayViewport;
8883
8884 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8885 prepareAxes(POSITION);
8886 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8887
8888 // Finger down
8889 int32_t x = 100, y = 100;
8890 processPosition(mapper, x, y);
8891 processSync(mapper);
8892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8893 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8894
8895 // Deactivate display viewport
8896 displayViewport.isActive = false;
8897 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8898 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8899
8900 // The ongoing touch should not be canceled
8901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8902
8903 // Finger move is not ignored
8904 x += 10, y += 10;
8905 processPosition(mapper, x, y);
8906 processSync(mapper);
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8908 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8909
8910 // Reactivate display viewport
8911 displayViewport.isActive = true;
8912 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8913 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8914
8915 // Finger move continues and does not start new gesture
8916 x += 10, y += 10;
8917 processPosition(mapper, x, y);
8918 processSync(mapper);
8919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8920 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8921}
8922
Arthur Hung7c645402019-01-25 17:45:42 +08008923TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8924 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008925 prepareAxes(POSITION | ID | SLOT);
8926 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008927 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008928
8929 // Create the second touch screen device, and enable multi fingers.
8930 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008931 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008932 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008933 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008934 std::shared_ptr<InputDevice> device2 =
8935 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008936 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008937
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008938 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008939 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008940 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008941 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008942 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008943 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008944 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008945 /*flat=*/0, /*fuzz=*/0);
8946 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008947 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8948 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008949
8950 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00008951 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
8952 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
8953 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008954 std::list<NotifyArgs> unused =
8955 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008956 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008957 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008958
8959 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008960 std::shared_ptr<FakePointerController> fakePointerController =
8961 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008962 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008963
8964 // Setup policy for associated displays and show touches.
8965 const uint8_t hdmi1 = 0;
8966 const uint8_t hdmi2 = 1;
8967 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8968 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8969 mFakePolicy->setShowTouches(true);
8970
8971 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008972 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008973 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008974
8975 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008976 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008977 InputReaderConfiguration::Change::DISPLAY_INFO |
8978 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008979
8980 // Two fingers down at default display.
8981 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8982 processPosition(mapper, x1, y1);
8983 processId(mapper, 1);
8984 processSlot(mapper, 1);
8985 processPosition(mapper, x2, y2);
8986 processId(mapper, 2);
8987 processSync(mapper);
8988
8989 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8990 fakePointerController->getSpots().find(DISPLAY_ID);
8991 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8992 ASSERT_EQ(size_t(2), iter->second.size());
8993
8994 // Two fingers down at second display.
8995 processPosition(mapper2, x1, y1);
8996 processId(mapper2, 1);
8997 processSlot(mapper2, 1);
8998 processPosition(mapper2, x2, y2);
8999 processId(mapper2, 2);
9000 processSync(mapper2);
9001
9002 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9003 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9004 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009005
9006 // Disable the show touches configuration and ensure the spots are cleared.
9007 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009008 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009009 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009010
9011 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009012}
9013
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009014TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009015 prepareAxes(POSITION);
9016 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009017 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009018 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009019
9020 NotifyMotionArgs motionArgs;
9021 // Unrotated video frame
9022 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9023 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009024 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009025 processPosition(mapper, 100, 200);
9026 processSync(mapper);
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(frames, motionArgs.videoFrames);
9029
9030 // Subsequent touch events should not have any videoframes
9031 // This is implemented separately in FakeEventHub,
9032 // but that should match the behaviour of TouchVideoDevice.
9033 processPosition(mapper, 200, 200);
9034 processSync(mapper);
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9036 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9037}
9038
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009039TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009040 prepareAxes(POSITION);
9041 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009042 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009043 // Unrotated video frame
9044 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9045 NotifyMotionArgs motionArgs;
9046
9047 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009048 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009049 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9050 clearViewports();
9051 prepareDisplay(orientation);
9052 std::vector<TouchVideoFrame> frames{frame};
9053 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9054 processPosition(mapper, 100, 200);
9055 processSync(mapper);
9056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9057 ASSERT_EQ(frames, motionArgs.videoFrames);
9058 }
9059}
9060
9061TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9062 prepareAxes(POSITION);
9063 addConfigurationProperty("touch.deviceType", "touchScreen");
9064 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9065 // orientation-aware are affected by display rotation.
9066 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009067 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009068 // Unrotated video frame
9069 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9070 NotifyMotionArgs motionArgs;
9071
9072 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009073 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009074 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9075 clearViewports();
9076 prepareDisplay(orientation);
9077 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009078 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009079 processPosition(mapper, 100, 200);
9080 processSync(mapper);
9081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009082 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9083 // compared to the display. This is so that when the window transform (which contains the
9084 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9085 // window's coordinate space.
9086 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009087 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009088
9089 // Release finger.
9090 processSync(mapper);
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009092 }
9093}
9094
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009095TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009096 prepareAxes(POSITION);
9097 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009098 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009099 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9100 // so mix these.
9101 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9102 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9103 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9104 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9105 NotifyMotionArgs motionArgs;
9106
Michael Wrighta9cf4192022-12-01 23:46:39 +00009107 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009108 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009109 processPosition(mapper, 100, 200);
9110 processSync(mapper);
9111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009112 ASSERT_EQ(frames, motionArgs.videoFrames);
9113}
9114
9115TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9116 prepareAxes(POSITION);
9117 addConfigurationProperty("touch.deviceType", "touchScreen");
9118 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9119 // orientation-aware are affected by display rotation.
9120 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009121 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009122 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9123 // so mix these.
9124 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9125 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9126 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9127 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9128 NotifyMotionArgs motionArgs;
9129
Michael Wrighta9cf4192022-12-01 23:46:39 +00009130 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009131 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9132 processPosition(mapper, 100, 200);
9133 processSync(mapper);
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9135 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9136 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9137 // compared to the display. This is so that when the window transform (which contains the
9138 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9139 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009140 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009141 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009142 ASSERT_EQ(frames, motionArgs.videoFrames);
9143}
9144
Arthur Hung9da14732019-09-02 16:16:58 +08009145/**
9146 * If we had defined port associations, but the viewport is not ready, the touch device would be
9147 * expected to be disabled, and it should be enabled after the viewport has found.
9148 */
9149TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009150 constexpr uint8_t hdmi2 = 1;
9151 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009152 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009153
9154 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9155
9156 addConfigurationProperty("touch.deviceType", "touchScreen");
9157 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009158 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009159
9160 ASSERT_EQ(mDevice->isEnabled(), false);
9161
9162 // Add display on hdmi2, the device should be enabled and can receive touch event.
9163 prepareSecondaryDisplay(type, hdmi2);
9164 ASSERT_EQ(mDevice->isEnabled(), true);
9165
9166 // Send a touch event.
9167 processPosition(mapper, 100, 100);
9168 processSync(mapper);
9169
9170 NotifyMotionArgs args;
9171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9172 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9173}
9174
Arthur Hung421eb1c2020-01-16 00:09:42 +08009175TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009176 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009177 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009178 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009179 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009180
9181 NotifyMotionArgs motionArgs;
9182
9183 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9184 // finger down
9185 processId(mapper, 1);
9186 processPosition(mapper, x1, y1);
9187 processSync(mapper);
9188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009190 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009191
9192 // finger move
9193 processId(mapper, 1);
9194 processPosition(mapper, x2, y2);
9195 processSync(mapper);
9196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009198 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009199
9200 // finger up.
9201 processId(mapper, -1);
9202 processSync(mapper);
9203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9204 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009205 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009206
9207 // new finger down
9208 processId(mapper, 1);
9209 processPosition(mapper, x3, y3);
9210 processSync(mapper);
9211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9212 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009213 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009214}
9215
9216/**
arthurhungcc7f9802020-04-30 17:55:40 +08009217 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9218 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009219 */
arthurhungcc7f9802020-04-30 17:55:40 +08009220TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009221 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009222 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009223 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009224 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009225
9226 NotifyMotionArgs motionArgs;
9227
9228 // default tool type is finger
9229 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009230 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009231 processPosition(mapper, x1, y1);
9232 processSync(mapper);
9233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9234 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009235 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009236
9237 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9238 processToolType(mapper, MT_TOOL_PALM);
9239 processSync(mapper);
9240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9241 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9242
9243 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009244 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009245 processPosition(mapper, x2, y2);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9248
9249 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009250 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009251 processSync(mapper);
9252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9253
9254 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009255 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009256 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009257 processPosition(mapper, x3, y3);
9258 processSync(mapper);
9259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9260 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009261 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009262}
9263
arthurhungbf89a482020-04-17 17:37:55 +08009264/**
arthurhungcc7f9802020-04-30 17:55:40 +08009265 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9266 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009267 */
arthurhungcc7f9802020-04-30 17:55:40 +08009268TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009269 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009270 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009271 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009272 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009273
9274 NotifyMotionArgs motionArgs;
9275
9276 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009277 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9278 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009279 processPosition(mapper, x1, y1);
9280 processSync(mapper);
9281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9282 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009283 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009284
9285 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009286 processSlot(mapper, SECOND_SLOT);
9287 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009288 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009289 processSync(mapper);
9290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009291 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009292 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009293
9294 // If the tool type of the first finger changes to MT_TOOL_PALM,
9295 // we expect to receive ACTION_POINTER_UP with cancel flag.
9296 processSlot(mapper, FIRST_SLOT);
9297 processId(mapper, FIRST_TRACKING_ID);
9298 processToolType(mapper, MT_TOOL_PALM);
9299 processSync(mapper);
9300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009301 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009302 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9303
9304 // The following MOVE events of second finger should be processed.
9305 processSlot(mapper, SECOND_SLOT);
9306 processId(mapper, SECOND_TRACKING_ID);
9307 processPosition(mapper, x2 + 1, y2 + 1);
9308 processSync(mapper);
9309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009311 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009312
9313 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9314 // it. Second finger receive move.
9315 processSlot(mapper, FIRST_SLOT);
9316 processId(mapper, INVALID_TRACKING_ID);
9317 processSync(mapper);
9318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009320 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009321
9322 // Second finger keeps moving.
9323 processSlot(mapper, SECOND_SLOT);
9324 processId(mapper, SECOND_TRACKING_ID);
9325 processPosition(mapper, x2 + 2, y2 + 2);
9326 processSync(mapper);
9327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009329 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009330
9331 // Second finger up.
9332 processId(mapper, INVALID_TRACKING_ID);
9333 processSync(mapper);
9334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9335 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9336 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9337}
9338
9339/**
9340 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9341 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9342 */
9343TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9344 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009345 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009346 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009347 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009348
9349 NotifyMotionArgs motionArgs;
9350
9351 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9352 // First finger down.
9353 processId(mapper, FIRST_TRACKING_ID);
9354 processPosition(mapper, x1, y1);
9355 processSync(mapper);
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009358 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009359
9360 // Second finger down.
9361 processSlot(mapper, SECOND_SLOT);
9362 processId(mapper, SECOND_TRACKING_ID);
9363 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009364 processSync(mapper);
9365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009366 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009367 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009368
arthurhungcc7f9802020-04-30 17:55:40 +08009369 // If the tool type of the first finger changes to MT_TOOL_PALM,
9370 // we expect to receive ACTION_POINTER_UP with cancel flag.
9371 processSlot(mapper, FIRST_SLOT);
9372 processId(mapper, FIRST_TRACKING_ID);
9373 processToolType(mapper, MT_TOOL_PALM);
9374 processSync(mapper);
9375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009376 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009377 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9378
9379 // Second finger keeps moving.
9380 processSlot(mapper, SECOND_SLOT);
9381 processId(mapper, SECOND_TRACKING_ID);
9382 processPosition(mapper, x2 + 1, y2 + 1);
9383 processSync(mapper);
9384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9385 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9386
9387 // second finger becomes palm, receive cancel due to only 1 finger is active.
9388 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009389 processToolType(mapper, MT_TOOL_PALM);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9392 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9393
arthurhungcc7f9802020-04-30 17:55:40 +08009394 // third finger down.
9395 processSlot(mapper, THIRD_SLOT);
9396 processId(mapper, THIRD_TRACKING_ID);
9397 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009398 processPosition(mapper, x3, y3);
9399 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009402 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009403 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009404
9405 // third finger move
9406 processId(mapper, THIRD_TRACKING_ID);
9407 processPosition(mapper, x3 + 1, y3 + 1);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9411
9412 // first finger up, third finger receive move.
9413 processSlot(mapper, FIRST_SLOT);
9414 processId(mapper, INVALID_TRACKING_ID);
9415 processSync(mapper);
9416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009418 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009419
9420 // second finger up, third finger receive move.
9421 processSlot(mapper, SECOND_SLOT);
9422 processId(mapper, INVALID_TRACKING_ID);
9423 processSync(mapper);
9424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009426 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009427
9428 // third finger up.
9429 processSlot(mapper, THIRD_SLOT);
9430 processId(mapper, INVALID_TRACKING_ID);
9431 processSync(mapper);
9432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9433 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9434 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9435}
9436
9437/**
9438 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9439 * and the active finger could still be allowed to receive the events
9440 */
9441TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9442 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009443 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009444 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009445 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009446
9447 NotifyMotionArgs motionArgs;
9448
9449 // default tool type is finger
9450 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9451 processId(mapper, FIRST_TRACKING_ID);
9452 processPosition(mapper, x1, y1);
9453 processSync(mapper);
9454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9455 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009456 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009457
9458 // Second finger down.
9459 processSlot(mapper, SECOND_SLOT);
9460 processId(mapper, SECOND_TRACKING_ID);
9461 processPosition(mapper, x2, y2);
9462 processSync(mapper);
9463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009464 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009465 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009466
9467 // If the tool type of the second finger changes to MT_TOOL_PALM,
9468 // we expect to receive ACTION_POINTER_UP with cancel flag.
9469 processId(mapper, SECOND_TRACKING_ID);
9470 processToolType(mapper, MT_TOOL_PALM);
9471 processSync(mapper);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009473 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009474 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9475
9476 // The following MOVE event should be processed.
9477 processSlot(mapper, FIRST_SLOT);
9478 processId(mapper, FIRST_TRACKING_ID);
9479 processPosition(mapper, x1 + 1, y1 + 1);
9480 processSync(mapper);
9481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009483 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009484
9485 // second finger up.
9486 processSlot(mapper, SECOND_SLOT);
9487 processId(mapper, INVALID_TRACKING_ID);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9491
9492 // first finger keep moving
9493 processSlot(mapper, FIRST_SLOT);
9494 processId(mapper, FIRST_TRACKING_ID);
9495 processPosition(mapper, x1 + 2, y1 + 2);
9496 processSync(mapper);
9497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9499
9500 // first finger up.
9501 processId(mapper, INVALID_TRACKING_ID);
9502 processSync(mapper);
9503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9504 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9505 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009506}
9507
Arthur Hung9ad18942021-06-19 02:04:46 +00009508/**
9509 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9510 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9511 * cause slot be valid again.
9512 */
9513TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9514 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009515 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009516 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009517 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009518
9519 NotifyMotionArgs motionArgs;
9520
9521 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9522 // First finger down.
9523 processId(mapper, FIRST_TRACKING_ID);
9524 processPosition(mapper, x1, y1);
9525 processPressure(mapper, RAW_PRESSURE_MAX);
9526 processSync(mapper);
9527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9528 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009529 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009530
9531 // First finger move.
9532 processId(mapper, FIRST_TRACKING_ID);
9533 processPosition(mapper, x1 + 1, y1 + 1);
9534 processPressure(mapper, RAW_PRESSURE_MAX);
9535 processSync(mapper);
9536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009538 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009539
9540 // Second finger down.
9541 processSlot(mapper, SECOND_SLOT);
9542 processId(mapper, SECOND_TRACKING_ID);
9543 processPosition(mapper, x2, y2);
9544 processPressure(mapper, RAW_PRESSURE_MAX);
9545 processSync(mapper);
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009547 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009548 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009549
9550 // second finger up with some unexpected data.
9551 processSlot(mapper, SECOND_SLOT);
9552 processId(mapper, INVALID_TRACKING_ID);
9553 processPosition(mapper, x2, y2);
9554 processSync(mapper);
9555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009556 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009557 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009558
9559 // first finger up with some unexpected data.
9560 processSlot(mapper, FIRST_SLOT);
9561 processId(mapper, INVALID_TRACKING_ID);
9562 processPosition(mapper, x2, y2);
9563 processPressure(mapper, RAW_PRESSURE_MAX);
9564 processSync(mapper);
9565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9566 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009567 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009568}
9569
Arpit Singh4b4a4572023-11-24 18:19:56 +00009570TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009571 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009572 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009573 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009574 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009575
9576 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009577 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009578 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009579 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009580 processPressure(mapper, RAW_PRESSURE_MAX);
9581 processSync(mapper);
9582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9583 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9584
9585 // Second finger down.
9586 processSlot(mapper, SECOND_SLOT);
9587 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009588 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009589 processPressure(mapper, RAW_PRESSURE_MAX);
9590 processSync(mapper);
9591 ASSERT_NO_FATAL_FAILURE(
9592 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9593
Arpit Singh4b4a4572023-11-24 18:19:56 +00009594 // Set MT Slot state to be repopulated for the required slots
9595 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9596 mtSlotValues[0] = FIRST_TRACKING_ID;
9597 mtSlotValues[1] = SECOND_TRACKING_ID;
9598 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9599
9600 mtSlotValues[0] = x1;
9601 mtSlotValues[1] = x2;
9602 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9603
9604 mtSlotValues[0] = y1;
9605 mtSlotValues[1] = y2;
9606 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9607
9608 mtSlotValues[0] = RAW_PRESSURE_MAX;
9609 mtSlotValues[1] = RAW_PRESSURE_MAX;
9610 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9611
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009612 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009613 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009614 resetMapper(mapper, ARBITRARY_TIME);
9615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9616 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009617
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009618 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9619 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009620 processPosition(mapper, 301, 302);
9621 processSync(mapper);
9622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9623 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9625 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009626
9627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9628}
9629
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009630TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009631 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009632 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009633 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009634 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009635
9636 // First finger touches down and releases.
9637 processId(mapper, FIRST_TRACKING_ID);
9638 processPosition(mapper, 100, 200);
9639 processPressure(mapper, RAW_PRESSURE_MAX);
9640 processSync(mapper);
9641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9642 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9643 processId(mapper, INVALID_TRACKING_ID);
9644 processSync(mapper);
9645 ASSERT_NO_FATAL_FAILURE(
9646 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9647
9648 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9649 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009650 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9652
9653 // Send an empty sync frame. Since there are no pointers, no events are generated.
9654 processSync(mapper);
9655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9656}
9657
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009658TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009659 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009660 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009661 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009662 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009664
9665 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9666 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9667 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9668 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9669 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9670
9671 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009672 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009673 processId(mapper, FIRST_TRACKING_ID);
9674 processToolType(mapper, MT_TOOL_PEN);
9675 processPosition(mapper, 100, 200);
9676 processPressure(mapper, RAW_PRESSURE_MAX);
9677 processSync(mapper);
9678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9679 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9680 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009681 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009682
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009683 // Now that we know the device supports styluses, ensure that the device is re-configured with
9684 // the stylus source.
9685 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9686 {
9687 const auto& devices = mReader->getInputDevices();
9688 auto deviceInfo =
9689 std::find_if(devices.begin(), devices.end(),
9690 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9691 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9692 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9693 }
9694
9695 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9697
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009698 processId(mapper, INVALID_TRACKING_ID);
9699 processSync(mapper);
9700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9701 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9702 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009703 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009704}
9705
Seunghwan Choi356026c2023-02-01 14:37:25 +09009706TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
9707 addConfigurationProperty("touch.deviceType", "touchScreen");
9708 prepareDisplay(ui::ROTATION_0);
9709 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9710 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9711 // indicate stylus presence dynamically.
9712 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9713 std::shared_ptr<FakePointerController> fakePointerController =
9714 std::make_shared<FakePointerController>();
9715 mFakePolicy->setPointerController(fakePointerController);
9716 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00009717 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09009718
9719 processId(mapper, FIRST_TRACKING_ID);
9720 processPressure(mapper, RAW_PRESSURE_MIN);
9721 processPosition(mapper, 100, 200);
9722 processToolType(mapper, MT_TOOL_PEN);
9723 processSync(mapper);
9724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9725 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009726 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09009727 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9728 ASSERT_TRUE(fakePointerController->isPointerShown());
9729 ASSERT_NO_FATAL_FAILURE(
9730 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
9731}
9732
9733TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
9734 addConfigurationProperty("touch.deviceType", "touchScreen");
9735 prepareDisplay(ui::ROTATION_0);
9736 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9737 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9738 // indicate stylus presence dynamically.
9739 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9740 std::shared_ptr<FakePointerController> fakePointerController =
9741 std::make_shared<FakePointerController>();
9742 mFakePolicy->setPointerController(fakePointerController);
9743 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00009744 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09009745
9746 processId(mapper, FIRST_TRACKING_ID);
9747 processPressure(mapper, RAW_PRESSURE_MIN);
9748 processPosition(mapper, 100, 200);
9749 processToolType(mapper, MT_TOOL_PEN);
9750 processSync(mapper);
9751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9752 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009753 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09009754 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9755 ASSERT_FALSE(fakePointerController->isPointerShown());
9756}
9757
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009758// --- MultiTouchInputMapperTest_ExternalDevice ---
9759
9760class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9761protected:
Chris Yea52ade12020-08-27 16:49:20 -07009762 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009763};
9764
9765/**
9766 * Expect fallback to internal viewport if device is external and external viewport is not present.
9767 */
9768TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9769 prepareAxes(POSITION);
9770 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009771 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009772 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009773
9774 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9775
9776 NotifyMotionArgs motionArgs;
9777
9778 // Expect the event to be sent to the internal viewport,
9779 // because an external viewport is not present.
9780 processPosition(mapper, 100, 100);
9781 processSync(mapper);
9782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9783 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9784
9785 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009786 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009787 processPosition(mapper, 100, 100);
9788 processSync(mapper);
9789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9790 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9791}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009792
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009793TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9794 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9795 std::shared_ptr<FakePointerController> fakePointerController =
9796 std::make_shared<FakePointerController>();
9797 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9798 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009799
9800 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009801 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009802 prepareAxes(POSITION | ID | SLOT);
9803 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9804 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9805 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009806 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +00009807 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009808
9809 // captured touchpad should be a touchpad source
9810 NotifyDeviceResetArgs resetArgs;
9811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9812 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9813
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009814 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009815
9816 const InputDeviceInfo::MotionRange* relRangeX =
9817 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9818 ASSERT_NE(relRangeX, nullptr);
9819 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9820 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9821 const InputDeviceInfo::MotionRange* relRangeY =
9822 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9823 ASSERT_NE(relRangeY, nullptr);
9824 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9825 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9826
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009827 // run captured pointer tests - note that this is unscaled, so input listener events should be
9828 // identical to what the hardware sends (accounting for any
9829 // calibration).
9830 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009831 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009832 processId(mapper, 1);
9833 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9834 processKey(mapper, BTN_TOUCH, 1);
9835 processSync(mapper);
9836
9837 // expect coord[0] to contain initial location of touch 0
9838 NotifyMotionArgs args;
9839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9840 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009841 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009842 ASSERT_EQ(0, args.pointerProperties[0].id);
9843 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9844 ASSERT_NO_FATAL_FAILURE(
9845 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9846
9847 // FINGER 1 DOWN
9848 processSlot(mapper, 1);
9849 processId(mapper, 2);
9850 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9851 processSync(mapper);
9852
9853 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009855 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009856 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009857 ASSERT_EQ(0, args.pointerProperties[0].id);
9858 ASSERT_EQ(1, args.pointerProperties[1].id);
9859 ASSERT_NO_FATAL_FAILURE(
9860 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9861 ASSERT_NO_FATAL_FAILURE(
9862 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9863
9864 // FINGER 1 MOVE
9865 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9866 processSync(mapper);
9867
9868 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9869 // from move
9870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9872 ASSERT_NO_FATAL_FAILURE(
9873 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9874 ASSERT_NO_FATAL_FAILURE(
9875 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9876
9877 // FINGER 0 MOVE
9878 processSlot(mapper, 0);
9879 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9880 processSync(mapper);
9881
9882 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9885 ASSERT_NO_FATAL_FAILURE(
9886 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9887 ASSERT_NO_FATAL_FAILURE(
9888 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9889
9890 // BUTTON DOWN
9891 processKey(mapper, BTN_LEFT, 1);
9892 processSync(mapper);
9893
9894 // touchinputmapper design sends a move before button press
9895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9896 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9898 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9899
9900 // BUTTON UP
9901 processKey(mapper, BTN_LEFT, 0);
9902 processSync(mapper);
9903
9904 // touchinputmapper design sends a move after button release
9905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9906 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9909
9910 // FINGER 0 UP
9911 processId(mapper, -1);
9912 processSync(mapper);
9913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9914 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9915
9916 // FINGER 1 MOVE
9917 processSlot(mapper, 1);
9918 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9919 processSync(mapper);
9920
9921 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009924 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009925 ASSERT_EQ(1, args.pointerProperties[0].id);
9926 ASSERT_NO_FATAL_FAILURE(
9927 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9928
9929 // FINGER 1 UP
9930 processId(mapper, -1);
9931 processKey(mapper, BTN_TOUCH, 0);
9932 processSync(mapper);
9933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9934 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9935
Josep del Río2d8c79a2023-01-23 19:33:50 +00009936 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009937 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009938 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +00009940 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009941}
9942
9943TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9944 std::shared_ptr<FakePointerController> fakePointerController =
9945 std::make_shared<FakePointerController>();
9946 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9947 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009948
9949 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009950 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009951 prepareAxes(POSITION | ID | SLOT);
9952 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9953 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009954 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +00009955 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009956 // run uncaptured pointer tests - pushes out generic events
9957 // FINGER 0 DOWN
9958 processId(mapper, 3);
9959 processPosition(mapper, 100, 100);
9960 processKey(mapper, BTN_TOUCH, 1);
9961 processSync(mapper);
9962
9963 // start at (100,100), cursor should be at (0,0) * scale
9964 NotifyMotionArgs args;
9965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9966 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9967 ASSERT_NO_FATAL_FAILURE(
9968 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9969
9970 // FINGER 0 MOVE
9971 processPosition(mapper, 200, 200);
9972 processSync(mapper);
9973
9974 // compute scaling to help with touch position checking
9975 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9976 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9977 float scale =
9978 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9979
9980 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9982 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9984 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009985
9986 // BUTTON DOWN
9987 processKey(mapper, BTN_LEFT, 1);
9988 processSync(mapper);
9989
9990 // touchinputmapper design sends a move before button press
9991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9992 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9995
9996 // BUTTON UP
9997 processKey(mapper, BTN_LEFT, 0);
9998 processSync(mapper);
9999
10000 // touchinputmapper design sends a move after button release
10001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10002 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10004 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010005}
10006
10007TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10008 std::shared_ptr<FakePointerController> fakePointerController =
10009 std::make_shared<FakePointerController>();
10010
Michael Wrighta9cf4192022-12-01 23:46:39 +000010011 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010012 prepareAxes(POSITION | ID | SLOT);
10013 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010014 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010015 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010016 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010017
Josep del Río2d8c79a2023-01-23 19:33:50 +000010018 // uncaptured touchpad should be a pointer device
10019 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010020
Josep del Río2d8c79a2023-01-23 19:33:50 +000010021 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010022 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010023 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010024 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10025}
10026
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010027// --- BluetoothMultiTouchInputMapperTest ---
10028
10029class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10030protected:
10031 void SetUp() override {
10032 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10033 }
10034};
10035
10036TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10037 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010038 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010039 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010040 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010041
10042 nsecs_t kernelEventTime = ARBITRARY_TIME;
10043 nsecs_t expectedEventTime = ARBITRARY_TIME;
10044 // Touch down.
10045 processId(mapper, FIRST_TRACKING_ID);
10046 processPosition(mapper, 100, 200);
10047 processPressure(mapper, RAW_PRESSURE_MAX);
10048 processSync(mapper, ARBITRARY_TIME);
10049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10050 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10051
10052 // Process several events that come in quick succession, according to their timestamps.
10053 for (int i = 0; i < 3; i++) {
10054 constexpr static nsecs_t delta = ms2ns(1);
10055 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10056 kernelEventTime += delta;
10057 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10058
10059 processPosition(mapper, 101 + i, 201 + i);
10060 processSync(mapper, kernelEventTime);
10061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10062 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10063 WithEventTime(expectedEventTime))));
10064 }
10065
10066 // Release the touch.
10067 processId(mapper, INVALID_TRACKING_ID);
10068 processPressure(mapper, RAW_PRESSURE_MIN);
10069 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10071 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10072 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10073}
10074
10075// --- MultiTouchPointerModeTest ---
10076
HQ Liue6983c72022-04-19 22:14:56 +000010077class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10078protected:
10079 float mPointerMovementScale;
10080 float mPointerXZoomScale;
10081 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10082 addConfigurationProperty("touch.deviceType", "pointer");
10083 std::shared_ptr<FakePointerController> fakePointerController =
10084 std::make_shared<FakePointerController>();
10085 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10086 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010087 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010088
10089 prepareAxes(POSITION);
10090 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10091 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10092 // needs to be disabled, and the pointer gesture needs to be enabled.
10093 mFakePolicy->setPointerCapture(false);
10094 mFakePolicy->setPointerGestureEnabled(true);
10095 mFakePolicy->setPointerController(fakePointerController);
10096
10097 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10098 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10099 mPointerMovementScale =
10100 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10101 mPointerXZoomScale =
10102 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10103 }
10104
10105 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10106 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10107 /*flat*/ 0,
10108 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10110 /*flat*/ 0,
10111 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10112 }
10113};
10114
10115/**
10116 * Two fingers down on a pointer mode touch pad. The width
10117 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10118 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10119 * be greater than the both value to be freeform gesture, so that after two
10120 * fingers start to move downwards, the gesture should be swipe.
10121 */
10122TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10123 // The min freeform gesture width is 25units/mm x 30mm = 750
10124 // which is greater than fraction of the diagnal length of the touchpad (349).
10125 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010126 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010127 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010128 NotifyMotionArgs motionArgs;
10129
10130 // Two fingers down at once.
10131 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10132 // Pointer's initial position is used the [0,0] coordinate.
10133 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10134
10135 processId(mapper, FIRST_TRACKING_ID);
10136 processPosition(mapper, x1, y1);
10137 processMTSync(mapper);
10138 processId(mapper, SECOND_TRACKING_ID);
10139 processPosition(mapper, x2, y2);
10140 processMTSync(mapper);
10141 processSync(mapper);
10142
10143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010144 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010145 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010146 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010147 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010148 ASSERT_NO_FATAL_FAILURE(
10149 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10150
10151 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10152 // that there should be 1 pointer.
10153 int32_t movingDistance = 200;
10154 y1 += movingDistance;
10155 y2 += movingDistance;
10156
10157 processId(mapper, FIRST_TRACKING_ID);
10158 processPosition(mapper, x1, y1);
10159 processMTSync(mapper);
10160 processId(mapper, SECOND_TRACKING_ID);
10161 processPosition(mapper, x2, y2);
10162 processMTSync(mapper);
10163 processSync(mapper);
10164
10165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010166 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010167 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010168 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010169 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010170 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10171 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10172 0, 0, 0, 0));
10173}
10174
10175/**
10176 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10177 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10178 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10179 * value to be freeform gesture, so that after two fingers start to move downwards,
10180 * the gesture should be swipe.
10181 */
10182TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10183 // The min freeform gesture width is 5units/mm x 30mm = 150
10184 // which is greater than fraction of the diagnal length of the touchpad (349).
10185 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010186 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010187 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010188 NotifyMotionArgs motionArgs;
10189
10190 // Two fingers down at once.
10191 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10192 // Pointer's initial position is used the [0,0] coordinate.
10193 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10194
10195 processId(mapper, FIRST_TRACKING_ID);
10196 processPosition(mapper, x1, y1);
10197 processMTSync(mapper);
10198 processId(mapper, SECOND_TRACKING_ID);
10199 processPosition(mapper, x2, y2);
10200 processMTSync(mapper);
10201 processSync(mapper);
10202
10203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010204 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010205 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010206 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010207 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010208 ASSERT_NO_FATAL_FAILURE(
10209 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10210
10211 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10212 // and there should be 1 pointer.
10213 int32_t movingDistance = 200;
10214 y1 += movingDistance;
10215 y2 += movingDistance;
10216
10217 processId(mapper, FIRST_TRACKING_ID);
10218 processPosition(mapper, x1, y1);
10219 processMTSync(mapper);
10220 processId(mapper, SECOND_TRACKING_ID);
10221 processPosition(mapper, x2, y2);
10222 processMTSync(mapper);
10223 processSync(mapper);
10224
10225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010226 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010228 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010229 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010230 // New coordinate is the scaled relative coordinate from the initial coordinate.
10231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10232 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10233 0, 0, 0, 0));
10234}
10235
10236/**
10237 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10238 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10239 * freeform gestures after two fingers start to move downwards.
10240 */
10241TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010242 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010243 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010244
10245 NotifyMotionArgs motionArgs;
10246
10247 // Two fingers down at once. Wider than the max swipe width.
10248 // The gesture is expected to be PRESS, then transformed to FREEFORM
10249 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10250
10251 processId(mapper, FIRST_TRACKING_ID);
10252 processPosition(mapper, x1, y1);
10253 processMTSync(mapper);
10254 processId(mapper, SECOND_TRACKING_ID);
10255 processPosition(mapper, x2, y2);
10256 processMTSync(mapper);
10257 processSync(mapper);
10258
10259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010260 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010261 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010262 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010263 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010264 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10265 ASSERT_NO_FATAL_FAILURE(
10266 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10267
10268 int32_t movingDistance = 200;
10269
10270 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10271 // then two down events for two pointers.
10272 y1 += movingDistance;
10273 y2 += movingDistance;
10274
10275 processId(mapper, FIRST_TRACKING_ID);
10276 processPosition(mapper, x1, y1);
10277 processMTSync(mapper);
10278 processId(mapper, SECOND_TRACKING_ID);
10279 processPosition(mapper, x2, y2);
10280 processMTSync(mapper);
10281 processSync(mapper);
10282
10283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10284 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010285 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010286 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010288 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010289 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010290 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010292 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010293 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010294 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010295 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010296 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010297 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010298 // Two pointers' scaled relative coordinates from their initial centroid.
10299 // Initial y coordinates are 0 as y1 and y2 have the same value.
10300 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10301 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10302 // When pointers move, the new coordinates equal to the initial coordinates plus
10303 // scaled moving distance.
10304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10305 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10306 0, 0, 0, 0));
10307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10308 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10309 0, 0, 0, 0));
10310
10311 // Move two fingers down again, expect one MOVE motion event.
10312 y1 += movingDistance;
10313 y2 += movingDistance;
10314
10315 processId(mapper, FIRST_TRACKING_ID);
10316 processPosition(mapper, x1, y1);
10317 processMTSync(mapper);
10318 processId(mapper, SECOND_TRACKING_ID);
10319 processPosition(mapper, x2, y2);
10320 processMTSync(mapper);
10321 processSync(mapper);
10322
10323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010324 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010326 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010327 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10329 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10330 0, 0, 0, 0, 0));
10331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10332 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10333 0, 0, 0, 0, 0));
10334}
10335
Harry Cutts39b7ca22022-10-05 15:55:48 +000010336TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010337 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010338 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010339 NotifyMotionArgs motionArgs;
10340
10341 // Place two fingers down.
10342 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10343
10344 processId(mapper, FIRST_TRACKING_ID);
10345 processPosition(mapper, x1, y1);
10346 processMTSync(mapper);
10347 processId(mapper, SECOND_TRACKING_ID);
10348 processPosition(mapper, x2, y2);
10349 processMTSync(mapper);
10350 processSync(mapper);
10351
10352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010353 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010354 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10355 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10356 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10357 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10358
10359 // Move the two fingers down and to the left.
10360 int32_t movingDistance = 200;
10361 x1 -= movingDistance;
10362 y1 += movingDistance;
10363 x2 -= movingDistance;
10364 y2 += movingDistance;
10365
10366 processId(mapper, FIRST_TRACKING_ID);
10367 processPosition(mapper, x1, y1);
10368 processMTSync(mapper);
10369 processId(mapper, SECOND_TRACKING_ID);
10370 processPosition(mapper, x2, y2);
10371 processMTSync(mapper);
10372 processSync(mapper);
10373
10374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010375 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10377 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10378 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10379 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10380}
10381
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010382TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010383 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010384 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010385 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10387
10388 // Start a stylus gesture.
10389 processKey(mapper, BTN_TOOL_PEN, 1);
10390 processId(mapper, FIRST_TRACKING_ID);
10391 processPosition(mapper, 100, 200);
10392 processSync(mapper);
10393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10394 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10395 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010396 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010397 // TODO(b/257078296): Pointer mode generates extra event.
10398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10399 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10400 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010401 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10403
10404 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10405 // gesture should be disabled.
10406 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10407 viewport->isActive = false;
10408 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010409 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10411 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10412 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010413 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010414 // TODO(b/257078296): Pointer mode generates extra event.
10415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10416 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10417 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010418 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10420}
10421
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010422// --- JoystickInputMapperTest ---
10423
10424class JoystickInputMapperTest : public InputMapperTest {
10425protected:
10426 static const int32_t RAW_X_MIN;
10427 static const int32_t RAW_X_MAX;
10428 static const int32_t RAW_Y_MIN;
10429 static const int32_t RAW_Y_MAX;
10430
10431 void SetUp() override {
10432 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10433 }
10434 void prepareAxes() {
10435 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10436 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10437 }
10438
10439 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10441 }
10442
10443 void processSync(JoystickInputMapper& mapper) {
10444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10445 }
10446
Michael Wrighta9cf4192022-12-01 23:46:39 +000010447 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010448 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10449 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10450 NO_PORT, ViewportType::VIRTUAL);
10451 }
10452};
10453
10454const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10455const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10456const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10457const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10458
10459TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10460 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010461 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010462
10463 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10464
Michael Wrighta9cf4192022-12-01 23:46:39 +000010465 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010466
10467 // Send an axis event
10468 processAxis(mapper, ABS_X, 100);
10469 processSync(mapper);
10470
10471 NotifyMotionArgs args;
10472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10473 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10474
10475 // Send another axis event
10476 processAxis(mapper, ABS_Y, 100);
10477 processSync(mapper);
10478
10479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10480 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10481}
10482
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010483// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010484
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010485class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010486protected:
10487 static const char* DEVICE_NAME;
10488 static const char* DEVICE_LOCATION;
10489 static const int32_t DEVICE_ID;
10490 static const int32_t DEVICE_GENERATION;
10491 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010492 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010493 static const int32_t EVENTHUB_ID;
10494
10495 std::shared_ptr<FakeEventHub> mFakeEventHub;
10496 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010497 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010498 std::unique_ptr<InstrumentedInputReader> mReader;
10499 std::shared_ptr<InputDevice> mDevice;
10500
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010501 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010502 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010503 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010504 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010505 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010506 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010507 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10508 }
10509
10510 void SetUp() override { SetUp(DEVICE_CLASSES); }
10511
10512 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010513 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010514 mFakePolicy.clear();
10515 }
10516
Chris Yee2b1e5c2021-03-10 22:45:12 -080010517 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10518 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010519 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010520 InputDeviceIdentifier identifier;
10521 identifier.name = name;
10522 identifier.location = location;
10523 std::shared_ptr<InputDevice> device =
10524 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10525 identifier);
10526 mReader->pushNextDevice(device);
10527 mFakeEventHub->addDevice(eventHubId, name, classes);
10528 mReader->loopOnce();
10529 return device;
10530 }
10531
10532 template <class T, typename... Args>
10533 T& addControllerAndConfigure(Args... args) {
10534 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10535
10536 return controller;
10537 }
10538};
10539
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010540const char* PeripheralControllerTest::DEVICE_NAME = "device";
10541const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10542const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10543const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10544const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010545const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10546 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010547const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010548
10549// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010550class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010551protected:
10552 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010553 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010554 }
10555};
10556
10557TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010558 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010559
Harry Cuttsa5b71292022-11-28 12:56:17 +000010560 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10561 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10562 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010563}
10564
10565TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010566 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010567
Harry Cuttsa5b71292022-11-28 12:56:17 +000010568 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10569 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10570 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010571}
10572
10573// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010574class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010575protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010576 void SetUp() override {
10577 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10578 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010579};
10580
Chris Ye85758332021-05-16 23:05:17 -070010581TEST_F(LightControllerTest, MonoLight) {
10582 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010583 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010584 .maxBrightness = 255,
10585 .flags = InputLightClass::BRIGHTNESS,
10586 .path = ""};
10587 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010588
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010589 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010590 InputDeviceInfo info;
10591 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010592 std::vector<InputDeviceLightInfo> lights = info.getLights();
10593 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010594 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10595 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10596
10597 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10598 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10599}
10600
10601TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10602 RawLightInfo infoMono = {.id = 1,
10603 .name = "mono_keyboard_backlight",
10604 .maxBrightness = 255,
10605 .flags = InputLightClass::BRIGHTNESS |
10606 InputLightClass::KEYBOARD_BACKLIGHT,
10607 .path = ""};
10608 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10609
10610 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10611 InputDeviceInfo info;
10612 controller.populateDeviceInfo(&info);
10613 std::vector<InputDeviceLightInfo> lights = info.getLights();
10614 ASSERT_EQ(1U, lights.size());
10615 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10616 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010617
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010618 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10619 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010620}
10621
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010622TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10623 RawLightInfo infoMono = {.id = 1,
10624 .name = "mono_light",
10625 .maxBrightness = 255,
10626 .flags = InputLightClass::BRIGHTNESS,
10627 .path = ""};
10628 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10629 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10630 "0,100,200");
10631
10632 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10633 std::list<NotifyArgs> unused =
10634 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10635 /*changes=*/{});
10636
10637 InputDeviceInfo info;
10638 controller.populateDeviceInfo(&info);
10639 std::vector<InputDeviceLightInfo> lights = info.getLights();
10640 ASSERT_EQ(1U, lights.size());
10641 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10642}
10643
10644TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10645 RawLightInfo infoMono = {.id = 1,
10646 .name = "mono_keyboard_backlight",
10647 .maxBrightness = 255,
10648 .flags = InputLightClass::BRIGHTNESS |
10649 InputLightClass::KEYBOARD_BACKLIGHT,
10650 .path = ""};
10651 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10652
10653 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10654 std::list<NotifyArgs> unused =
10655 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10656 /*changes=*/{});
10657
10658 InputDeviceInfo info;
10659 controller.populateDeviceInfo(&info);
10660 std::vector<InputDeviceLightInfo> lights = info.getLights();
10661 ASSERT_EQ(1U, lights.size());
10662 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10663}
10664
10665TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10666 RawLightInfo infoMono = {.id = 1,
10667 .name = "mono_keyboard_backlight",
10668 .maxBrightness = 255,
10669 .flags = InputLightClass::BRIGHTNESS |
10670 InputLightClass::KEYBOARD_BACKLIGHT,
10671 .path = ""};
10672 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10673 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10674 "0,100,200");
10675
10676 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10677 std::list<NotifyArgs> unused =
10678 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10679 /*changes=*/{});
10680
10681 InputDeviceInfo info;
10682 controller.populateDeviceInfo(&info);
10683 std::vector<InputDeviceLightInfo> lights = info.getLights();
10684 ASSERT_EQ(1U, lights.size());
10685 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10686 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10687 ASSERT_EQ(BrightnessLevel(0), *it);
10688 std::advance(it, 1);
10689 ASSERT_EQ(BrightnessLevel(100), *it);
10690 std::advance(it, 1);
10691 ASSERT_EQ(BrightnessLevel(200), *it);
10692}
10693
10694TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10695 RawLightInfo infoMono = {.id = 1,
10696 .name = "mono_keyboard_backlight",
10697 .maxBrightness = 255,
10698 .flags = InputLightClass::BRIGHTNESS |
10699 InputLightClass::KEYBOARD_BACKLIGHT,
10700 .path = ""};
10701 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10702 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10703 "0,100,200,300,400,500");
10704
10705 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10706 std::list<NotifyArgs> unused =
10707 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10708 /*changes=*/{});
10709
10710 InputDeviceInfo info;
10711 controller.populateDeviceInfo(&info);
10712 std::vector<InputDeviceLightInfo> lights = info.getLights();
10713 ASSERT_EQ(1U, lights.size());
10714 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10715}
10716
Chris Yee2b1e5c2021-03-10 22:45:12 -080010717TEST_F(LightControllerTest, RGBLight) {
10718 RawLightInfo infoRed = {.id = 1,
10719 .name = "red",
10720 .maxBrightness = 255,
10721 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10722 .path = ""};
10723 RawLightInfo infoGreen = {.id = 2,
10724 .name = "green",
10725 .maxBrightness = 255,
10726 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10727 .path = ""};
10728 RawLightInfo infoBlue = {.id = 3,
10729 .name = "blue",
10730 .maxBrightness = 255,
10731 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10732 .path = ""};
10733 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10734 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10735 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10736
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010737 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010738 InputDeviceInfo info;
10739 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010740 std::vector<InputDeviceLightInfo> lights = info.getLights();
10741 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010742 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10743 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10744 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10745
10746 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10747 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10748}
10749
10750TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10751 RawLightInfo infoRed = {.id = 1,
10752 .name = "red_keyboard_backlight",
10753 .maxBrightness = 255,
10754 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10755 InputLightClass::KEYBOARD_BACKLIGHT,
10756 .path = ""};
10757 RawLightInfo infoGreen = {.id = 2,
10758 .name = "green_keyboard_backlight",
10759 .maxBrightness = 255,
10760 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10761 InputLightClass::KEYBOARD_BACKLIGHT,
10762 .path = ""};
10763 RawLightInfo infoBlue = {.id = 3,
10764 .name = "blue_keyboard_backlight",
10765 .maxBrightness = 255,
10766 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10767 InputLightClass::KEYBOARD_BACKLIGHT,
10768 .path = ""};
10769 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10770 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10771 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10772
10773 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10774 InputDeviceInfo info;
10775 controller.populateDeviceInfo(&info);
10776 std::vector<InputDeviceLightInfo> lights = info.getLights();
10777 ASSERT_EQ(1U, lights.size());
10778 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10779 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10780 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10781
10782 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10783 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10784}
10785
10786TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10787 RawLightInfo infoRed = {.id = 1,
10788 .name = "red",
10789 .maxBrightness = 255,
10790 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10791 .path = ""};
10792 RawLightInfo infoGreen = {.id = 2,
10793 .name = "green",
10794 .maxBrightness = 255,
10795 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10796 .path = ""};
10797 RawLightInfo infoBlue = {.id = 3,
10798 .name = "blue",
10799 .maxBrightness = 255,
10800 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10801 .path = ""};
10802 RawLightInfo infoGlobal = {.id = 3,
10803 .name = "global_keyboard_backlight",
10804 .maxBrightness = 255,
10805 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10806 InputLightClass::KEYBOARD_BACKLIGHT,
10807 .path = ""};
10808 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10809 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10810 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10811 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10812
10813 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10814 InputDeviceInfo info;
10815 controller.populateDeviceInfo(&info);
10816 std::vector<InputDeviceLightInfo> lights = info.getLights();
10817 ASSERT_EQ(1U, lights.size());
10818 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10819 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10820 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010821
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010822 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10823 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010824}
10825
10826TEST_F(LightControllerTest, MultiColorRGBLight) {
10827 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010828 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010829 .maxBrightness = 255,
10830 .flags = InputLightClass::BRIGHTNESS |
10831 InputLightClass::MULTI_INTENSITY |
10832 InputLightClass::MULTI_INDEX,
10833 .path = ""};
10834
10835 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10836
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010837 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010838 InputDeviceInfo info;
10839 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010840 std::vector<InputDeviceLightInfo> lights = info.getLights();
10841 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010842 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10843 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10844 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10845
10846 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10847 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10848}
10849
10850TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10851 RawLightInfo infoColor = {.id = 1,
10852 .name = "multi_color_keyboard_backlight",
10853 .maxBrightness = 255,
10854 .flags = InputLightClass::BRIGHTNESS |
10855 InputLightClass::MULTI_INTENSITY |
10856 InputLightClass::MULTI_INDEX |
10857 InputLightClass::KEYBOARD_BACKLIGHT,
10858 .path = ""};
10859
10860 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10861
10862 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10863 InputDeviceInfo info;
10864 controller.populateDeviceInfo(&info);
10865 std::vector<InputDeviceLightInfo> lights = info.getLights();
10866 ASSERT_EQ(1U, lights.size());
10867 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10868 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10869 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010870
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010871 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10872 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010873}
10874
10875TEST_F(LightControllerTest, PlayerIdLight) {
10876 RawLightInfo info1 = {.id = 1,
10877 .name = "player1",
10878 .maxBrightness = 255,
10879 .flags = InputLightClass::BRIGHTNESS,
10880 .path = ""};
10881 RawLightInfo info2 = {.id = 2,
10882 .name = "player2",
10883 .maxBrightness = 255,
10884 .flags = InputLightClass::BRIGHTNESS,
10885 .path = ""};
10886 RawLightInfo info3 = {.id = 3,
10887 .name = "player3",
10888 .maxBrightness = 255,
10889 .flags = InputLightClass::BRIGHTNESS,
10890 .path = ""};
10891 RawLightInfo info4 = {.id = 4,
10892 .name = "player4",
10893 .maxBrightness = 255,
10894 .flags = InputLightClass::BRIGHTNESS,
10895 .path = ""};
10896 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10897 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10898 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10899 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10900
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010901 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010902 InputDeviceInfo info;
10903 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010904 std::vector<InputDeviceLightInfo> lights = info.getLights();
10905 ASSERT_EQ(1U, lights.size());
10906 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010907 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10908 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010909
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010910 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10911 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10912 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010913}
10914
Michael Wrightd02c5b62014-02-10 15:10:22 -080010915} // namespace android