blob: 07fa59ff320b6a32ebdcffbca25d1680d364ed93 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Prabir Pradhan018faea2024-05-08 21:52:54 +000030#include <NotifyArgsBuilders.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070031#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070032#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070033#include <SingleTouchInputMapper.h>
34#include <SwitchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000035#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070036#include <TestInputListener.h>
37#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080038#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000039#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070040#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090041#include <com_android_input_flags.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000042#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043#include <gtest/gtest.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000044#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070046#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000047#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000048#include "FakeInputReaderPolicy.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059using std::chrono_literals::operator""ms;
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -080060using std::chrono_literals::operator""s;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070061
Michael Wrightd02c5b62014-02-10 15:10:22 -080062// Arbitrary display properties.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070063static constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
Linnan Li13bf76a2024-05-05 19:18:02 +080065static constexpr ui::LogicalDisplayId SECONDARY_DISPLAY_ID =
66 ui::LogicalDisplayId{DISPLAY_ID.val() + 1};
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
Linnan Li13bf76a2024-05-05 19:18:02 +080069static constexpr ui::LogicalDisplayId VIRTUAL_DISPLAY_ID = ui::LogicalDisplayId{1};
arthurhungcc7f9802020-04-30 17:55:40 +080070static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080082static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
83static constexpr int32_t LIGHT_COLOR = 0x7F448866;
84static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080085
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080086static constexpr int32_t ACTION_POINTER_0_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_0_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000095static constexpr uint32_t STYLUS_FUSION_SOURCE =
96 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
97
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000098// Minimum timestamp separation between subsequent input events from a Bluetooth device.
99static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000100
Byoungho Jungda10dd32023-10-06 17:03:45 +0900101namespace input_flags = com::android::input::flags;
102
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103template<typename T>
104static inline T min(T a, T b) {
105 return a < b ? a : b;
106}
107
108static inline float avg(float x, float y) {
109 return (x + y) / 2;
110}
111
Chris Ye3fdbfef2021-01-06 18:45:18 -0800112// Mapping for light color name and the light color
113const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
114 {"green", LightColor::GREEN},
115 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Michael Wrighta9cf4192022-12-01 23:46:39 +0000117static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700118 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000119 case ui::ROTATION_90:
120 return ui::ROTATION_270;
121 case ui::ROTATION_270:
122 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700123 default:
124 return orientation;
125 }
126}
127
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800128static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
129 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000130 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
135}
136
137static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
138 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000139 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800140
141 const InputDeviceInfo::MotionRange* motionRange =
142 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
143 ASSERT_EQ(nullptr, motionRange);
144}
145
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700146[[maybe_unused]] static void dumpReader(InputReader& reader) {
147 std::string dump;
148 reader.dump(dump);
149 std::istringstream iss(dump);
150 for (std::string line; std::getline(iss, line);) {
151 ALOGE("%s", line.c_str());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -0800152 std::this_thread::sleep_for(1ms);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700153 }
154}
155
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156// --- FakeInputMapper ---
157
158class FakeInputMapper : public InputMapper {
159 uint32_t mSources;
160 int32_t mKeyboardType;
161 int32_t mMetaState;
162 KeyedVector<int32_t, int32_t> mKeyCodeStates;
163 KeyedVector<int32_t, int32_t> mScanCodeStates;
164 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100165 // fake mapping which would normally come from keyCharacterMap
166 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800167 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700168 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700170 std::mutex mLock;
171 std::condition_variable mStateChangedCondition;
172 bool mConfigureWasCalled GUARDED_BY(mLock);
173 bool mResetWasCalled GUARDED_BY(mLock);
174 bool mProcessWasCalled GUARDED_BY(mLock);
175 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176
Arthur Hungc23540e2018-11-29 20:42:11 +0800177 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000179 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
180 uint32_t sources)
181 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800182 mSources(sources),
183 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800185 mConfigureWasCalled(false),
186 mResetWasCalled(false),
187 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191 void setKeyboardType(int32_t keyboardType) {
192 mKeyboardType = keyboardType;
193 }
194
195 void setMetaState(int32_t metaState) {
196 mMetaState = metaState;
197 }
198
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700199 // Sets the return value for the `process` call.
200 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
201 mProcessResult.clear();
202 for (auto notifyArg : notifyArgs) {
203 mProcessResult.push_back(notifyArg);
204 }
205 }
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool configureCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mConfigureWasCalled;
213 });
214 if (!configureCalled) {
215 FAIL() << "Expected configure() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mConfigureWasCalled = false;
218 }
219
220 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool resetCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mResetWasCalled;
226 });
227 if (!resetCalled) {
228 FAIL() << "Expected reset() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 mResetWasCalled = false;
231 }
232
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000233 void assertResetWasNotCalled() {
234 std::scoped_lock lock(mLock);
235 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
236 }
237
Yi Kong9b14ac62018-07-17 13:48:38 -0700238 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700239 std::unique_lock<std::mutex> lock(mLock);
240 base::ScopedLockAssertion assumeLocked(mLock);
241 const bool processCalled =
242 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
243 return mProcessWasCalled;
244 });
245 if (!processCalled) {
246 FAIL() << "Expected process() to have been called.";
247 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 if (outLastEvent) {
249 *outLastEvent = mLastEvent;
250 }
251 mProcessWasCalled = false;
252 }
253
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000254 void assertProcessWasNotCalled() {
255 std::scoped_lock lock(mLock);
256 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
257 }
258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 void setKeyCodeState(int32_t keyCode, int32_t state) {
260 mKeyCodeStates.replaceValueFor(keyCode, state);
261 }
262
263 void setScanCodeState(int32_t scanCode, int32_t state) {
264 mScanCodeStates.replaceValueFor(scanCode, state);
265 }
266
267 void setSwitchState(int32_t switchCode, int32_t state) {
268 mSwitchStates.replaceValueFor(switchCode, state);
269 }
270
271 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800272 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 }
274
Philip Junker4af3b3d2021-12-14 10:36:55 +0100275 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
276 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
277 }
278
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100280 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281
Harry Cuttsd02ea102023-03-17 18:21:30 +0000282 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 InputMapper::populateDeviceInfo(deviceInfo);
284
285 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000286 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288 }
289
Arpit Singhed6c3de2023-04-05 19:24:37 +0000290 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000291 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800294
295 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800296 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000297 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000298 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800299 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700300
301 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700302 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 }
304
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700305 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700306 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700308 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700309 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 }
311
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700312 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700313 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 mLastEvent = *rawEvent;
315 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700316 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700317 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 }
319
Chris Yea52ade12020-08-27 16:49:20 -0700320 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
322 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
323 }
324
Philip Junker4af3b3d2021-12-14 10:36:55 +0100325 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
326 auto it = mKeyCodeMapping.find(locationKeyCode);
327 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
328 }
329
Chris Yea52ade12020-08-27 16:49:20 -0700330 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
332 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
333 }
334
Chris Yea52ade12020-08-27 16:49:20 -0700335 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 ssize_t index = mSwitchStates.indexOfKey(switchCode);
337 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
338 }
339
Chris Yea52ade12020-08-27 16:49:20 -0700340 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700341 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700342 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700343 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
345 if (keyCodes[i] == mSupportedKeyCodes[j]) {
346 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348 }
349 }
Chris Yea52ade12020-08-27 16:49:20 -0700350 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return result;
352 }
353
354 virtual int32_t getMetaState() {
355 return mMetaState;
356 }
357
358 virtual void fadePointer() {
359 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800360
Linnan Li13bf76a2024-05-05 19:18:02 +0800361 virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplay() {
Arthur Hungc23540e2018-11-29 20:42:11 +0800362 if (mViewport) {
363 return std::make_optional(mViewport->displayId);
364 }
365 return std::nullopt;
366 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367};
368
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700369// --- InputReaderPolicyTest ---
370class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700371protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700372 sp<FakeInputReaderPolicy> mFakePolicy;
373
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700374 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700375 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376};
377
378/**
379 * Check that empty set of viewports is an acceptable configuration.
380 * Also try to get internal viewport two different ways - by type and by uniqueId.
381 *
382 * There will be confusion if two viewports with empty uniqueId and identical type are present.
383 * Such configuration is not currently allowed.
384 */
385TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700386 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387
388 // We didn't add any viewports yet, so there shouldn't be any.
389 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100390 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700391 ASSERT_FALSE(internalViewport);
392
393 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000394 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000395 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700396
397 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700398 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700399 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100400 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700401
402 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100403 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700405 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700406
407 mFakePolicy->clearViewports();
408 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700409 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700410 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100411 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 ASSERT_FALSE(internalViewport);
413}
414
415TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
416 const std::string internalUniqueId = "local:0";
417 const std::string externalUniqueId = "local:1";
418 const std::string virtualUniqueId1 = "virtual:2";
419 const std::string virtualUniqueId2 = "virtual:3";
Linnan Li13bf76a2024-05-05 19:18:02 +0800420 constexpr ui::LogicalDisplayId virtualDisplayId1 = ui::LogicalDisplayId{2};
421 constexpr ui::LogicalDisplayId virtualDisplayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700422
423 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000424 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000425 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000426 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700427 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000428 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000429 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000430 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700431 // Add an virtual viewport
432 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000433 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000434 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 // Add another virtual viewport
436 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000437 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000438 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700439
440 // Check matching by type for internal
441 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100442 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_TRUE(internalViewport);
444 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
445
446 // Check matching by type for external
447 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100448 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700449 ASSERT_TRUE(externalViewport);
450 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
451
452 // Check matching by uniqueId for virtual viewport #1
453 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700454 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700455 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100456 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700457 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
458 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
459
460 // Check matching by uniqueId for virtual viewport #2
461 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700462 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700463 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100464 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
466 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
467}
468
469
470/**
471 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
472 * that lookup works by checking display id.
473 * Check that 2 viewports of each kind is possible, for all existing viewport types.
474 */
475TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
476 const std::string uniqueId1 = "uniqueId1";
477 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800478 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{2};
479 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700480
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100481 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
482 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700483 for (const ViewportType& type : types) {
484 mFakePolicy->clearViewports();
485 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000486 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000487 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700488 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000489 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000490 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700491
492 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493 std::optional<DisplayViewport> viewport1 =
494 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700495 ASSERT_TRUE(viewport1);
496 ASSERT_EQ(displayId1, viewport1->displayId);
497 ASSERT_EQ(type, viewport1->type);
498
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700499 std::optional<DisplayViewport> viewport2 =
500 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700501 ASSERT_TRUE(viewport2);
502 ASSERT_EQ(displayId2, viewport2->displayId);
503 ASSERT_EQ(type, viewport2->type);
504
505 // When there are multiple viewports of the same kind, and uniqueId is not specified
506 // in the call to getDisplayViewport, then that situation is not supported.
507 // The viewports can be stored in any order, so we cannot rely on the order, since that
508 // is just implementation detail.
509 // However, we can check that it still returns *a* viewport, we just cannot assert
510 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700511 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700512 ASSERT_TRUE(someViewport);
513 }
514}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700516/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000517 * When we have multiple internal displays make sure we always return the default display when
518 * querying by type.
519 */
520TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
521 const std::string uniqueId1 = "uniqueId1";
522 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800523 constexpr ui::LogicalDisplayId nonDefaultDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700524 ASSERT_NE(nonDefaultDisplayId, ui::LogicalDisplayId::DEFAULT)
525 << "Test display ID should not be ui::LogicalDisplayId::DEFAULT ";
Michael Wrightdde67b82020-10-27 16:09:22 +0000526
527 // Add the default display first and ensure it gets returned.
528 mFakePolicy->clearViewports();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700529 mFakePolicy->addDisplayViewport(ui::LogicalDisplayId::DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000530 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000531 ViewportType::INTERNAL);
532 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000533 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000534 ViewportType::INTERNAL);
535
536 std::optional<DisplayViewport> viewport =
537 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
538 ASSERT_TRUE(viewport);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700539 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000540 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
541
542 // Add the default display second to make sure order doesn't matter.
543 mFakePolicy->clearViewports();
544 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000545 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000546 ViewportType::INTERNAL);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700547 mFakePolicy->addDisplayViewport(ui::LogicalDisplayId::DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000548 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000549 ViewportType::INTERNAL);
550
551 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
552 ASSERT_TRUE(viewport);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700553 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000554 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
555}
556
557/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700558 * Check getDisplayViewportByPort
559 */
560TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100561 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700562 const std::string uniqueId1 = "uniqueId1";
563 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800564 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{1};
565 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{2};
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700566 const uint8_t hdmi1 = 0;
567 const uint8_t hdmi2 = 1;
568 const uint8_t hdmi3 = 2;
569
570 mFakePolicy->clearViewports();
571 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000572 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000573 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700574 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000575 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000576 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700577
578 // Check that correct display viewport was returned by comparing the display ports.
579 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
580 ASSERT_TRUE(hdmi1Viewport);
581 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
582 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
583
584 // Check that we can still get the same viewport using the uniqueId
585 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
586 ASSERT_TRUE(hdmi1Viewport);
587 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
588 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
589 ASSERT_EQ(type, hdmi1Viewport->type);
590
591 // Check that we cannot find a port with "HDMI2", because we never added one
592 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
593 ASSERT_FALSE(hdmi2Viewport);
594}
595
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596// --- InputReaderTest ---
597
598class InputReaderTest : public testing::Test {
599protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700600 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700602 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000603 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604
Chris Yea52ade12020-08-27 16:49:20 -0700605 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700606 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700607 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700608 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609
Prabir Pradhan28efc192019-11-05 01:10:04 +0000610 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700611 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 }
613
Chris Yea52ade12020-08-27 16:49:20 -0700614 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700615 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700619 void addDevice(int32_t eventHubId, const std::string& name,
620 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622
623 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800624 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 }
626 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000627 mReader->loopOnce();
628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700629 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700631 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 }
633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800634 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700635 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000636 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700637 }
638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800639 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700640 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000641 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700642 }
643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700645 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700646 ftl::Flags<InputDeviceClass> classes,
647 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800648 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000650 FakeInputMapper& mapper =
651 device->addMapper<FakeInputMapper>(eventHubId,
652 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800653 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800654 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 return mapper;
656 }
657};
658
Chris Ye98d3f532020-10-01 21:48:59 -0700659TEST_F(InputReaderTest, PolicyGetInputDevices) {
660 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700661 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700662 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663
664 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700665 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800667 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100668 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000671 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672}
673
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000674TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
675 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
676 mFakeEventHub->setSysfsRootPath(1, "xyz");
677
678 // Should also have received a notification describing the new input device.
679 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
680 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
681 ASSERT_EQ(0U, inputDevice.getLights().size());
682
683 RawLightInfo infoMonolight = {.id = 123,
684 .name = "mono_keyboard_backlight",
685 .maxBrightness = 255,
686 .flags = InputLightClass::BRIGHTNESS,
687 .path = ""};
688 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
689 mReader->sysfsNodeChanged("xyz");
690 mReader->loopOnce();
691
692 // Should also have received a notification describing the new recreated input device.
693 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
694 inputDevice = mFakePolicy->getInputDevices()[0];
695 ASSERT_EQ(1U, inputDevice.getLights().size());
696}
697
Chris Yee7310032020-09-22 15:36:28 -0700698TEST_F(InputReaderTest, GetMergedInputDevices) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 ASSERT_NO_FATAL_FAILURE(
713 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
714 ASSERT_NO_FATAL_FAILURE(
715 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
716
717 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000718 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700719}
720
Chris Yee14523a2020-12-19 13:46:00 -0800721TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
723 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
724 // Add two subdevices to device
725 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
726 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000727 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
728 AINPUT_SOURCE_KEYBOARD);
729 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
730 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800731
732 // Push same device instance for next device to be added, so they'll have same identifier.
733 mReader->pushNextDevice(device);
734 mReader->pushNextDevice(device);
735 // Sensor device is initially disabled
736 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
737 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
738 nullptr));
739 // Device is disabled because the only sub device is a sensor device and disabled initially.
740 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
741 ASSERT_FALSE(device->isEnabled());
742 ASSERT_NO_FATAL_FAILURE(
743 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
744 // The merged device is enabled if any sub device is enabled
745 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
746 ASSERT_TRUE(device->isEnabled());
747}
748
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700749TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800750 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700751 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800752 constexpr int32_t eventHubId = 1;
753 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700754 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000755 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
756 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800757 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800758 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700759
Yi Kong9b14ac62018-07-17 13:48:38 -0700760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700761
762 NotifyDeviceResetArgs resetArgs;
763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700764 ASSERT_EQ(deviceId, resetArgs.deviceId);
765
766 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800767 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000768 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700769
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700771 ASSERT_EQ(deviceId, resetArgs.deviceId);
772 ASSERT_EQ(device->isEnabled(), false);
773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800774 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000775 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700778 ASSERT_EQ(device->isEnabled(), false);
779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800780 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000781 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700783 ASSERT_EQ(deviceId, resetArgs.deviceId);
784 ASSERT_EQ(device->isEnabled(), true);
785}
786
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800788 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700789 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800790 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800791 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800792 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800793 AINPUT_SOURCE_KEYBOARD, nullptr);
794 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795
796 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
797 AINPUT_SOURCE_ANY, AKEYCODE_A))
798 << "Should return unknown when the device id is >= 0 but unknown.";
799
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 ASSERT_EQ(AKEY_STATE_UNKNOWN,
801 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
802 << "Should return unknown when the device id is valid but the sources are not "
803 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800805 ASSERT_EQ(AKEY_STATE_DOWN,
806 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
807 AKEYCODE_A))
808 << "Should return value provided by mapper when device id is valid and the device "
809 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
811 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
812 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
813 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
814
815 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
816 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
817 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
818}
819
Philip Junker4af3b3d2021-12-14 10:36:55 +0100820TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
821 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
822 constexpr int32_t eventHubId = 1;
823 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
824 InputDeviceClass::KEYBOARD,
825 AINPUT_SOURCE_KEYBOARD, nullptr);
826 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
827
828 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
829 << "Should return unknown when the device with the specified id is not found.";
830
831 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
832 << "Should return correct mapping when device id is valid and mapping exists.";
833
834 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
835 << "Should return the location key code when device id is valid and there's no "
836 "mapping.";
837}
838
839TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
840 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
841 constexpr int32_t eventHubId = 1;
842 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
843 InputDeviceClass::JOYSTICK,
844 AINPUT_SOURCE_GAMEPAD, nullptr);
845 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
846
847 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
848 << "Should return unknown when the device id is valid but there is no keyboard mapper";
849}
850
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800852 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700853 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800854 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800855 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800856 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800857 AINPUT_SOURCE_KEYBOARD, nullptr);
858 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859
860 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
861 AINPUT_SOURCE_ANY, KEY_A))
862 << "Should return unknown when the device id is >= 0 but unknown.";
863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 ASSERT_EQ(AKEY_STATE_UNKNOWN,
865 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
866 << "Should return unknown when the device id is valid but the sources are not "
867 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800869 ASSERT_EQ(AKEY_STATE_DOWN,
870 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
871 KEY_A))
872 << "Should return value provided by mapper when device id is valid and the device "
873 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874
875 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
876 AINPUT_SOURCE_TRACKBALL, KEY_A))
877 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
878
879 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
880 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
881 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
882}
883
884TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800885 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700886 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800888 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800890 AINPUT_SOURCE_KEYBOARD, nullptr);
891 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892
893 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
894 AINPUT_SOURCE_ANY, SW_LID))
895 << "Should return unknown when the device id is >= 0 but unknown.";
896
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 ASSERT_EQ(AKEY_STATE_UNKNOWN,
898 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
899 << "Should return unknown when the device id is valid but the sources are not "
900 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800902 ASSERT_EQ(AKEY_STATE_DOWN,
903 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
904 SW_LID))
905 << "Should return value provided by mapper when device id is valid and the device "
906 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907
908 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
909 AINPUT_SOURCE_TRACKBALL, SW_LID))
910 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
911
912 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
913 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
914 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
915}
916
917TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800918 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700919 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800921 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800923 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800925 mapper.addSupportedKeyCode(AKEYCODE_A);
926 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700928 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 uint8_t flags[4] = { 0, 0, 0, 1 };
930
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700931 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 << "Should return false when device id is >= 0 but unknown.";
933 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
934
935 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700936 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800937 << "Should return false when device id is valid but the sources are not supported by "
938 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
940
941 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700942 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 keyCodes, flags))
944 << "Should return value provided by mapper when device id is valid and the device "
945 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
947
948 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
950 << "Should return false when the device id is < 0 but the sources are not supported by "
951 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
953
954 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700955 ASSERT_TRUE(
956 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
957 << "Should return value provided by mapper when device id is < 0 and one of the "
958 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
960}
961
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000962TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800963 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700964 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
966 NotifyConfigurationChangedArgs args;
967
968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
969 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
970}
971
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000972TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800973 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700974 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000975 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000977 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800978 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800979 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800980 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000982 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000983 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
985
986 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800987 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000988 ASSERT_EQ(when, event.when);
989 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800990 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 ASSERT_EQ(EV_KEY, event.type);
992 ASSERT_EQ(KEY_A, event.code);
993 ASSERT_EQ(1, event.value);
994}
995
Garfield Tan1c7bc862020-01-28 13:24:04 -0800996TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800997 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700998 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800999 constexpr int32_t eventHubId = 1;
1000 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001001 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001002 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1003 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001004 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001005 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001006
1007 NotifyDeviceResetArgs resetArgs;
1008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001009 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001011 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001012 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001014 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001015 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001017 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001018 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001020 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001021 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001023 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001024 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001026 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001027 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001028}
1029
Garfield Tan1c7bc862020-01-28 13:24:04 -08001030TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1031 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001032 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001033 constexpr int32_t eventHubId = 1;
1034 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1035 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001036 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1037 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001038 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001039 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1040
1041 NotifyDeviceResetArgs resetArgs;
1042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1043 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1044}
1045
Arthur Hungc23540e2018-11-29 20:42:11 +08001046TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001047 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001048 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001049 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001050 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001051 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1052 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001053 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1054 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001055 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
1057 const uint8_t hdmi1 = 1;
1058
1059 // Associated touch screen with second display.
1060 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1061
1062 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001064 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001065 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001067 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001068 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001069 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001070 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001071
1072 // Add the device, and make sure all of the callbacks are triggered.
1073 // The device is added after the input port associations are processed since
1074 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001075 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001078 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001079
Arthur Hung2c9a3342019-07-23 14:18:59 +08001080 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001081 ASSERT_EQ(deviceId, device->getId());
1082 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1083 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001084
1085 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001086 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001087 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001088 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001089}
1090
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001091TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1092 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001093 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001094 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1095 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1096 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001097 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1098 AINPUT_SOURCE_KEYBOARD);
1099 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1100 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001101 mReader->pushNextDevice(device);
1102 mReader->pushNextDevice(device);
1103 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1105
1106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1107
1108 NotifyDeviceResetArgs resetArgs;
1109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1110 ASSERT_EQ(deviceId, resetArgs.deviceId);
1111 ASSERT_TRUE(device->isEnabled());
1112 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1113 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1114
1115 disableDevice(deviceId);
1116 mReader->loopOnce();
1117
1118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1119 ASSERT_EQ(deviceId, resetArgs.deviceId);
1120 ASSERT_FALSE(device->isEnabled());
1121 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1122 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1123
1124 enableDevice(deviceId);
1125 mReader->loopOnce();
1126
1127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1128 ASSERT_EQ(deviceId, resetArgs.deviceId);
1129 ASSERT_TRUE(device->isEnabled());
1130 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1131 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1132}
1133
1134TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1135 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001136 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001137 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1138 // Add two subdevices to device
1139 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1140 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001141 device->addMapper<FakeInputMapper>(eventHubIds[0],
1142 mFakePolicy->getReaderConfiguration(),
1143 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001144 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001145 device->addMapper<FakeInputMapper>(eventHubIds[1],
1146 mFakePolicy->getReaderConfiguration(),
1147 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001148 mReader->pushNextDevice(device);
1149 mReader->pushNextDevice(device);
1150 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1151 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1152
1153 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1154 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1155
1156 ASSERT_EQ(AKEY_STATE_DOWN,
1157 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1158 ASSERT_EQ(AKEY_STATE_DOWN,
1159 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1160 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1161 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1162}
1163
Prabir Pradhan7e186182020-11-10 13:56:45 -08001164TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1165 NotifyPointerCaptureChangedArgs args;
1166
Hiroki Sato25040232024-02-22 17:21:22 +09001167 auto request = mFakePolicy->setPointerCapture(/*window=*/sp<BBinder>::make());
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001168 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001169 mReader->loopOnce();
1170 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001171 ASSERT_TRUE(args.request.isEnable()) << "Pointer Capture should be enabled.";
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001172 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001173
Hiroki Sato25040232024-02-22 17:21:22 +09001174 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001175 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001176 mReader->loopOnce();
1177 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001178 ASSERT_FALSE(args.request.isEnable()) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001179
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001180 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001181 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001182 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001183 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001184 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001185}
1186
Prabir Pradhan018faea2024-05-08 21:52:54 +00001187TEST_F(InputReaderTest, GetLastUsedInputDeviceId) {
1188 constexpr int32_t FIRST_DEVICE_ID = END_RESERVED_ID + 1000;
1189 constexpr int32_t SECOND_DEVICE_ID = FIRST_DEVICE_ID + 1;
1190 FakeInputMapper& firstMapper =
1191 addDeviceWithFakeInputMapper(FIRST_DEVICE_ID, FIRST_DEVICE_ID, "first",
1192 InputDeviceClass::KEYBOARD, AINPUT_SOURCE_KEYBOARD,
1193 /*configuration=*/nullptr);
1194 FakeInputMapper& secondMapper =
1195 addDeviceWithFakeInputMapper(SECOND_DEVICE_ID, SECOND_DEVICE_ID, "second",
1196 InputDeviceClass::TOUCH_MT, AINPUT_SOURCE_STYLUS,
1197 /*configuration=*/nullptr);
1198
1199 ASSERT_EQ(ReservedInputDeviceId::INVALID_INPUT_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1200
1201 // Start a new key gesture from the first device
1202 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
1203 .deviceId(FIRST_DEVICE_ID)
1204 .build()});
1205 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1206 mReader->loopOnce();
1207 ASSERT_EQ(firstMapper.getDeviceId(), mReader->getLastUsedInputDeviceId());
1208
1209 // Start a new touch gesture from the second device
1210 secondMapper.setProcessResult(
1211 {MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
1212 .deviceId(SECOND_DEVICE_ID)
1213 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER))
1214 .build()});
1215 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1216 mReader->loopOnce();
1217 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1218
1219 // Releasing the key is not a new gesture, so it does not update the last used device
1220 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
1221 .deviceId(FIRST_DEVICE_ID)
1222 .build()});
1223 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1224 mReader->loopOnce();
1225 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1226
1227 // But pressing a new key does start a new gesture
1228 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
1229 .deviceId(FIRST_DEVICE_ID)
1230 .build()});
1231 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1232 mReader->loopOnce();
1233 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1234
1235 // Moving or ending a touch gesture does not update the last used device
1236 secondMapper.setProcessResult(
1237 {MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
1238 .deviceId(SECOND_DEVICE_ID)
1239 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1240 .build()});
1241 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1242 mReader->loopOnce();
1243 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1244 secondMapper.setProcessResult({MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
1245 .deviceId(SECOND_DEVICE_ID)
1246 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1247 .build()});
1248 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1249 mReader->loopOnce();
1250 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1251
1252 // Starting a new hover gesture updates the last used device
1253 secondMapper.setProcessResult(
1254 {MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
1255 .deviceId(SECOND_DEVICE_ID)
1256 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1257 .build()});
1258 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1259 mReader->loopOnce();
1260 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1261}
1262
Chris Ye87143712020-11-10 05:05:58 +00001263class FakeVibratorInputMapper : public FakeInputMapper {
1264public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001265 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1266 const InputReaderConfiguration& readerConfig, uint32_t sources)
1267 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001268
1269 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1270};
1271
1272TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1273 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001274 ftl::Flags<InputDeviceClass> deviceClass =
1275 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001276 constexpr int32_t eventHubId = 1;
1277 const char* DEVICE_LOCATION = "BLUETOOTH";
1278 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1279 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001280 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1281 mFakePolicy->getReaderConfiguration(),
1282 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001283 mReader->pushNextDevice(device);
1284
1285 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1286 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1287
1288 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1289 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1290}
1291
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001292// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001293
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001294class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001295public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001296 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001297
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001298 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001299
Andy Chenf9f1a022022-08-29 20:07:10 -04001300 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1301
Chris Yee2b1e5c2021-03-10 22:45:12 -08001302 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1303
1304 void dump(std::string& dump) override {}
1305
1306 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1307 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001308 }
1309
Chris Yee2b1e5c2021-03-10 22:45:12 -08001310 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1311 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001312 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313
1314 bool setLightColor(int32_t lightId, int32_t color) override {
1315 getDeviceContext().setLightBrightness(lightId, color >> 24);
1316 return true;
1317 }
1318
1319 std::optional<int32_t> getLightColor(int32_t lightId) override {
1320 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1321 if (!result.has_value()) {
1322 return std::nullopt;
1323 }
1324 return result.value() << 24;
1325 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001326
1327 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1328
1329 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1330
1331private:
1332 InputDeviceContext& mDeviceContext;
1333 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1334 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001335 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001336};
1337
Chris Yee2b1e5c2021-03-10 22:45:12 -08001338TEST_F(InputReaderTest, BatteryGetCapacity) {
1339 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001340 ftl::Flags<InputDeviceClass> deviceClass =
1341 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001342 constexpr int32_t eventHubId = 1;
1343 const char* DEVICE_LOCATION = "BLUETOOTH";
1344 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001345 FakePeripheralController& controller =
1346 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001347 mReader->pushNextDevice(device);
1348
1349 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1350
Harry Cuttsa5b71292022-11-28 12:56:17 +00001351 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1352 FakeEventHub::BATTERY_CAPACITY);
1353 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001354}
1355
1356TEST_F(InputReaderTest, BatteryGetStatus) {
1357 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001358 ftl::Flags<InputDeviceClass> deviceClass =
1359 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001360 constexpr int32_t eventHubId = 1;
1361 const char* DEVICE_LOCATION = "BLUETOOTH";
1362 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001363 FakePeripheralController& controller =
1364 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001365 mReader->pushNextDevice(device);
1366
1367 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1368
Harry Cuttsa5b71292022-11-28 12:56:17 +00001369 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1370 FakeEventHub::BATTERY_STATUS);
1371 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001372}
1373
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001374TEST_F(InputReaderTest, BatteryGetDevicePath) {
1375 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1376 ftl::Flags<InputDeviceClass> deviceClass =
1377 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1378 constexpr int32_t eventHubId = 1;
1379 const char* DEVICE_LOCATION = "BLUETOOTH";
1380 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1381 device->addController<FakePeripheralController>(eventHubId);
1382 mReader->pushNextDevice(device);
1383
1384 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1385
Harry Cuttsa5b71292022-11-28 12:56:17 +00001386 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001387}
1388
Chris Ye3fdbfef2021-01-06 18:45:18 -08001389TEST_F(InputReaderTest, LightGetColor) {
1390 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001391 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001392 constexpr int32_t eventHubId = 1;
1393 const char* DEVICE_LOCATION = "BLUETOOTH";
1394 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001395 FakePeripheralController& controller =
1396 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001397 mReader->pushNextDevice(device);
1398 RawLightInfo info = {.id = 1,
1399 .name = "Mono",
1400 .maxBrightness = 255,
1401 .flags = InputLightClass::BRIGHTNESS,
1402 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001403 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1404 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001405
1406 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001407
Harry Cutts33476232023-01-30 19:57:29 +00001408 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1409 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1410 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1411 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001412}
1413
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001414// --- InputReaderIntegrationTest ---
1415
1416// These tests create and interact with the InputReader only through its interface.
1417// The InputReader is started during SetUp(), which starts its processing in its own
1418// thread. The tests use linux uinput to emulate input devices.
1419// NOTE: Interacting with the physical device while these tests are running may cause
1420// the tests to fail.
1421class InputReaderIntegrationTest : public testing::Test {
1422protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001423 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001424 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001425 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001426
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001427 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1428 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1429
Chris Yea52ade12020-08-27 16:49:20 -07001430 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001431#if !defined(__ANDROID__)
1432 GTEST_SKIP();
1433#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001434 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001435
Arpit Singh440bf652023-08-09 09:23:43 +00001436 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001437 }
1438
Chris Yea52ade12020-08-27 16:49:20 -07001439 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001440#if !defined(__ANDROID__)
1441 return;
1442#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001443 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001444 mReader.reset();
1445 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001446 mFakePolicy.clear();
1447 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001448
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001449 std::optional<InputDeviceInfo> waitForDevice(const std::string& deviceName) {
1450 std::chrono::time_point start = std::chrono::steady_clock::now();
1451 while (true) {
1452 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1453 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1454 [&deviceName](const InputDeviceInfo& info) {
1455 return info.getIdentifier().name == deviceName;
1456 });
1457 if (it != inputDevices.end()) {
1458 return std::make_optional(*it);
1459 }
1460 std::this_thread::sleep_for(1ms);
1461 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
1462 if (elapsed > 5s) {
1463 return {};
1464 }
1465 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001466 }
Arpit Singh440bf652023-08-09 09:23:43 +00001467
1468 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001469 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1470 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001471
1472 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1473 *mTestListener);
1474 ASSERT_EQ(mReader->start(), OK);
1475
1476 // Since this test is run on a real device, all the input devices connected
1477 // to the test device will show up in mReader. We wait for those input devices to
1478 // show up before beginning the tests.
1479 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1480 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1482 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001483};
1484
1485TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1486 // An invalid input device that is only used for this test.
1487 class InvalidUinputDevice : public UinputDevice {
1488 public:
Harry Cutts33476232023-01-30 19:57:29 +00001489 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001490
1491 private:
1492 void configureDevice(int fd, uinput_user_dev* device) override {}
1493 };
1494
1495 const size_t numDevices = mFakePolicy->getInputDevices().size();
1496
1497 // UinputDevice does not set any event or key bits, so InputReader should not
1498 // consider it as a valid device.
1499 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1500 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1501 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1502 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1503
1504 invalidDevice.reset();
1505 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1507 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1508}
1509
1510TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1511 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1512
1513 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1514 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1515 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1516 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1517
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001518 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001519 ASSERT_TRUE(device.has_value());
1520 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1521 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1522 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001523
1524 keyboard.reset();
1525 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1527 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1528}
1529
1530TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1531 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1532 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1533
1534 NotifyConfigurationChangedArgs configChangedArgs;
1535 ASSERT_NO_FATAL_FAILURE(
1536 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001537 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001538 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1539
1540 NotifyKeyArgs keyArgs;
1541 keyboard->pressAndReleaseHomeKey();
1542 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1543 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001544 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001545 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001546 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001547 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001548 prevTimestamp = keyArgs.eventTime;
1549
1550 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1551 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001552 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001553 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001554 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001555}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001556
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001557TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1558 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1559 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1560
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001561 const auto device = waitForDevice(stylus->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001562 ASSERT_TRUE(device.has_value());
1563
Prabir Pradhana3621852022-10-14 18:57:23 +00001564 // An external stylus with buttons should also be recognized as a keyboard.
1565 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001566 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1567 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1568
1569 const auto DOWN =
1570 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1571 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1572
1573 stylus->pressAndReleaseKey(BTN_STYLUS);
1574 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1575 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1576 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1577 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1578
1579 stylus->pressAndReleaseKey(BTN_STYLUS2);
1580 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1581 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1582 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1583 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1584
1585 stylus->pressAndReleaseKey(BTN_STYLUS3);
1586 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1587 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1589 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1590}
1591
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001592TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1593 std::unique_ptr<UinputKeyboard> keyboard =
1594 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1595 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1596 KEY_R, KEY_T, KEY_Y,
1597 BTN_STYLUS, BTN_STYLUS2,
1598 BTN_STYLUS3});
1599 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1600
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001601 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001602 ASSERT_TRUE(device.has_value());
1603
1604 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1605 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1606 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1607 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1608}
1609
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001610TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1611 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1612 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1613 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1614 createUinputDevice<UinputKeyboardWithHidUsage>(
1615 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1616 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1617
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001618 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001619 ASSERT_TRUE(device.has_value());
1620
1621 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1622 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1623
1624 // If a device supports reporting HID usage codes, it shouldn't automatically support
1625 // stylus keys.
1626 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1627 uint8_t outFlags[] = {0};
1628 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1629 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1630}
1631
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001632/**
1633 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1634 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1635 * are passed to the listener.
1636 */
1637static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1638TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1639 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1640 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1641 NotifyKeyArgs keyArgs;
1642
1643 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1644 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1645 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1646 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1647
1648 controller->pressAndReleaseKey(BTN_GEAR_UP);
1649 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1650 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1651 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1652}
1653
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001654// --- TouchIntegrationTest ---
1655
Arpit Singh440bf652023-08-09 09:23:43 +00001656class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001657protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001658 const std::string UNIQUE_ID = "local:0";
1659
Chris Yea52ade12020-08-27 16:49:20 -07001660 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001661#if !defined(__ANDROID__)
1662 GTEST_SKIP();
1663#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001664 InputReaderIntegrationTest::SetUp();
1665 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001666 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1667 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001668
1669 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
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());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001673 ASSERT_TRUE(info);
1674 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001675 }
1676
Linnan Li13bf76a2024-05-05 19:18:02 +08001677 void setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001678 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001679 std::optional<uint8_t> physicalPort,
1680 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001681 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001682 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001683 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001684 }
1685
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001686 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1687 NotifyMotionArgs args;
1688 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1689 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001690 ASSERT_EQ(points.size(), args.getPointerCount());
1691 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001692 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1693 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1694 }
1695 }
1696
Arthur Hungaab25622020-01-16 11:22:11 +08001697 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001698 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001699};
1700
Arpit Singh440bf652023-08-09 09:23:43 +00001701enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1702
1703class TouchIntegrationTest : public BaseTouchIntegrationTest,
1704 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1705protected:
1706 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1707 const std::string INPUT_PORT = "uinput_touch/input0";
1708
1709 void SetUp() override {
1710#if !defined(__ANDROID__)
1711 GTEST_SKIP();
1712#endif
1713 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1714 BaseTouchIntegrationTest::SetUp();
1715 return;
1716 }
1717
1718 // setup policy with a input-port or UniqueId association to the display
1719 bool isInputPortAssociation =
1720 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1721
1722 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1723 if (isInputPortAssociation) {
1724 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1725 } else {
1726 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1727 }
Arpit Singh440bf652023-08-09 09:23:43 +00001728
1729 InputReaderIntegrationTest::setupInputReader();
1730
1731 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1732 INPUT_PORT);
1733 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1734
1735 // Add a display linked to a physical port or UniqueId.
1736 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1737 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1738 ViewportType::INTERNAL);
1739 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1740 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001741 const auto info = waitForDevice(mDevice->getName());
Arpit Singh440bf652023-08-09 09:23:43 +00001742 ASSERT_TRUE(info);
1743 mDeviceInfo = *info;
1744 }
1745};
1746
1747TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001748 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1749 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1750 // presses).
1751 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1752 mDeviceInfo.getSources());
1753}
1754
Arpit Singh440bf652023-08-09 09:23:43 +00001755TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001756 NotifyMotionArgs args;
1757 const Point centerPoint = mDevice->getCenterPoint();
1758
1759 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001760 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001761 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001762 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001763 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1765
1766 // ACTION_MOVE
1767 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001768 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1770 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1771
1772 // ACTION_UP
1773 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001774 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001775 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1776 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1777}
1778
Arpit Singh440bf652023-08-09 09:23:43 +00001779TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001780 NotifyMotionArgs args;
1781 const Point centerPoint = mDevice->getCenterPoint();
1782
1783 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001784 mDevice->sendSlot(FIRST_SLOT);
1785 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001786 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001787 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001788 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1789 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1790
1791 // ACTION_POINTER_DOWN (Second slot)
1792 const Point secondPoint = centerPoint + Point(100, 100);
1793 mDevice->sendSlot(SECOND_SLOT);
1794 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001795 mDevice->sendDown(secondPoint);
1796 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001797 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001798 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001799
1800 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001801 mDevice->sendMove(secondPoint + Point(1, 1));
1802 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001803 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1805
1806 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001807 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001808 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001809 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001810 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001811
1812 // ACTION_UP
1813 mDevice->sendSlot(FIRST_SLOT);
1814 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001815 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1817 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1818}
1819
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001820/**
1821 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1822 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1823 * data?
1824 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1825 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1826 * for Pointer 0 only is generated after.
1827 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1828 * events, we will not miss any information.
1829 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1830 * event generated afterwards that contains the newest movement of pointer 0.
1831 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1832 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1833 * losing information about non-palm pointers.
1834 */
Arpit Singh440bf652023-08-09 09:23:43 +00001835TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001836 NotifyMotionArgs args;
1837 const Point centerPoint = mDevice->getCenterPoint();
1838
1839 // ACTION_DOWN
1840 mDevice->sendSlot(FIRST_SLOT);
1841 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1842 mDevice->sendDown(centerPoint);
1843 mDevice->sendSync();
1844 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1845
1846 // ACTION_POINTER_DOWN (Second slot)
1847 const Point secondPoint = centerPoint + Point(100, 100);
1848 mDevice->sendSlot(SECOND_SLOT);
1849 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1850 mDevice->sendDown(secondPoint);
1851 mDevice->sendSync();
1852 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1853
1854 // ACTION_MOVE (First slot)
1855 mDevice->sendSlot(FIRST_SLOT);
1856 mDevice->sendMove(centerPoint + Point(5, 5));
1857 // ACTION_POINTER_UP (Second slot)
1858 mDevice->sendSlot(SECOND_SLOT);
1859 mDevice->sendPointerUp();
1860 // Send a single sync for the above 2 pointer updates
1861 mDevice->sendSync();
1862
1863 // First, we should get POINTER_UP for the second pointer
1864 assertReceivedMotion(ACTION_POINTER_1_UP,
1865 {/*first pointer */ centerPoint + Point(5, 5),
1866 /*second pointer*/ secondPoint});
1867
1868 // Next, the MOVE event for the first pointer
1869 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1870}
1871
1872/**
1873 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1874 * move, and then it will go up, all in the same frame.
1875 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1876 * gets sent to the listener.
1877 */
Arpit Singh440bf652023-08-09 09:23:43 +00001878TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001879 NotifyMotionArgs args;
1880 const Point centerPoint = mDevice->getCenterPoint();
1881
1882 // ACTION_DOWN
1883 mDevice->sendSlot(FIRST_SLOT);
1884 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1885 mDevice->sendDown(centerPoint);
1886 mDevice->sendSync();
1887 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1888
1889 // ACTION_POINTER_DOWN (Second slot)
1890 const Point secondPoint = centerPoint + Point(100, 100);
1891 mDevice->sendSlot(SECOND_SLOT);
1892 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1893 mDevice->sendDown(secondPoint);
1894 mDevice->sendSync();
1895 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1896
1897 // ACTION_MOVE (First slot)
1898 mDevice->sendSlot(FIRST_SLOT);
1899 mDevice->sendMove(centerPoint + Point(5, 5));
1900 // ACTION_POINTER_UP (Second slot)
1901 mDevice->sendSlot(SECOND_SLOT);
1902 mDevice->sendMove(secondPoint + Point(6, 6));
1903 mDevice->sendPointerUp();
1904 // Send a single sync for the above 2 pointer updates
1905 mDevice->sendSync();
1906
1907 // First, we should get POINTER_UP for the second pointer
1908 // The movement of the second pointer during the liftoff frame is ignored.
1909 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1910 assertReceivedMotion(ACTION_POINTER_1_UP,
1911 {/*first pointer */ centerPoint + Point(5, 5),
1912 /*second pointer*/ secondPoint});
1913
1914 // Next, the MOVE event for the first pointer
1915 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1916}
1917
Arpit Singh440bf652023-08-09 09:23:43 +00001918TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001919 NotifyMotionArgs args;
1920 const Point centerPoint = mDevice->getCenterPoint();
1921
1922 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001923 mDevice->sendSlot(FIRST_SLOT);
1924 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001925 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001926 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001927 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1928 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1929
arthurhungcc7f9802020-04-30 17:55:40 +08001930 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001931 const Point secondPoint = centerPoint + Point(100, 100);
1932 mDevice->sendSlot(SECOND_SLOT);
1933 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1934 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001935 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001936 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001937 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001938
arthurhungcc7f9802020-04-30 17:55:40 +08001939 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001940 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001941 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001942 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1943 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1944
arthurhungcc7f9802020-04-30 17:55:40 +08001945 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1946 // a palm event.
1947 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001948 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001949 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001950 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001951 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001952 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001953
arthurhungcc7f9802020-04-30 17:55:40 +08001954 // Send up to second slot, expect first slot send moving.
1955 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001956 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001957 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1958 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001959
arthurhungcc7f9802020-04-30 17:55:40 +08001960 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001961 mDevice->sendSlot(FIRST_SLOT);
1962 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001963 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001964
arthurhungcc7f9802020-04-30 17:55:40 +08001965 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1966 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001967}
1968
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001969/**
1970 * Some drivers historically have reported axis values outside of the range specified in the
1971 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1972 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1973 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1974 * and no units for pressure (resolution) is specified by the evdev documentation.
1975 */
1976TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1977 const Point centerPoint = mDevice->getCenterPoint();
1978
1979 // Down with pressure outside the reported range
1980 mDevice->sendSlot(FIRST_SLOT);
1981 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1982 mDevice->sendDown(centerPoint);
1983 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1984 mDevice->sendSync();
1985 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1986 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1987
1988 // Move to a point outside the reported range
1989 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1990 mDevice->sendSync();
1991 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1992 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1993
1994 // Up
1995 mDevice->sendUp();
1996 mDevice->sendSync();
1997 ASSERT_NO_FATAL_FAILURE(
1998 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1999}
2000
Arpit Singh440bf652023-08-09 09:23:43 +00002001TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00002002 const Point centerPoint = mDevice->getCenterPoint();
2003
2004 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
2005 mDevice->sendSlot(FIRST_SLOT);
2006 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2007 mDevice->sendToolType(MT_TOOL_PEN);
2008 mDevice->sendDown(centerPoint);
2009 mDevice->sendSync();
2010 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2011 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002012 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002013
2014 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2015
2016 // Release the stylus touch.
2017 mDevice->sendUp();
2018 mDevice->sendSync();
2019 ASSERT_NO_FATAL_FAILURE(
2020 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2021
2022 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2023
2024 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2025 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2026 mDevice->sendToolType(MT_TOOL_FINGER);
2027 mDevice->sendDown(centerPoint);
2028 mDevice->sendSync();
2029 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2030 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002031 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002032
2033 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2034
2035 mDevice->sendUp();
2036 mDevice->sendSync();
2037 ASSERT_NO_FATAL_FAILURE(
2038 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2039
2040 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2041 // The policy should be notified of the stylus presence.
2042 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2043 mDevice->sendToolType(MT_TOOL_PEN);
2044 mDevice->sendMove(centerPoint);
2045 mDevice->sendSync();
2046 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2047 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002048 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002049
2050 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2051}
2052
Arpit Singh440bf652023-08-09 09:23:43 +00002053TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002054 const Point centerPoint = mDevice->getCenterPoint();
2055
2056 // Down
2057 mDevice->sendSlot(FIRST_SLOT);
2058 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2059 mDevice->sendDown(centerPoint);
2060 mDevice->sendSync();
2061 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2062 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
2063
2064 // Move
2065 mDevice->sendMove(centerPoint + Point(1, 1));
2066 mDevice->sendSync();
2067 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2068 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2069
2070 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2071 auto externalStylus = createUinputDevice<UinputExternalStylus>();
2072 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2073 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002074 const auto stylusInfo = waitForDevice(externalStylus->getName());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002075 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002076
2077 // Move
2078 mDevice->sendMove(centerPoint + Point(2, 2));
2079 mDevice->sendSync();
2080 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2081 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2082
2083 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2084 externalStylus.reset();
2085 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2086 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2087 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2088
2089 // Up
2090 mDevice->sendUp();
2091 mDevice->sendSync();
2092 ASSERT_NO_FATAL_FAILURE(
2093 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2094
2095 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2096}
2097
Arpit Singh440bf652023-08-09 09:23:43 +00002098INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2099 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2100 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2101 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2102
Prabir Pradhan124ea442022-10-28 20:27:44 +00002103// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002104
Prabir Pradhan124ea442022-10-28 20:27:44 +00002105// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2106// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2107// stylus.
2108template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002109class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002110protected:
2111 void SetUp() override {
2112#if !defined(__ANDROID__)
2113 GTEST_SKIP();
2114#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002115 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002116 mTouchscreen = mDevice.get();
2117 mTouchscreenInfo = mDeviceInfo;
2118
2119 setUpStylusDevice();
2120 }
2121
2122 UinputStylusDevice* mStylus{nullptr};
2123 InputDeviceInfo mStylusInfo{};
2124
2125 UinputTouchScreen* mTouchscreen{nullptr};
2126 InputDeviceInfo mTouchscreenInfo{};
2127
2128private:
2129 // When we are attempting to test stylus button events that are sent from the touchscreen,
2130 // use the same Uinput device for the touchscreen and the stylus.
2131 template <typename T = UinputStylusDevice>
2132 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2133 mStylus = mDevice.get();
2134 mStylusInfo = mDeviceInfo;
2135 }
2136
2137 // When we are attempting to stylus buttons from an external stylus being merged with touches
2138 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2139 template <typename T = UinputStylusDevice>
2140 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2141 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2142 mStylus = mStylusDeviceLifecycleTracker.get();
2143 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2144 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002145 const auto info = waitForDevice(mStylus->getName());
Prabir Pradhan124ea442022-10-28 20:27:44 +00002146 ASSERT_TRUE(info);
2147 mStylusInfo = *info;
2148 }
2149
2150 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2151
2152 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002153 using BaseTouchIntegrationTest::mDevice;
2154 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002155};
2156
2157using StylusButtonIntegrationTestTypes =
2158 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2159TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2160
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002161TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002162 const auto stylusId = TestFixture::mStylusInfo.getId();
2163
2164 TestFixture::mStylus->pressKey(BTN_STYLUS);
2165 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2166 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2167 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2168
2169 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2170 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002171 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002172 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002173}
2174
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002175TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002176 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2177 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2178 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002179
2180 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002181 TestFixture::mStylus->pressKey(BTN_STYLUS);
2182 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002183 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002184 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002185
2186 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002187 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2188 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2189 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2190 TestFixture::mTouchscreen->sendDown(centerPoint);
2191 TestFixture::mTouchscreen->sendSync();
2192 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002193 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002194 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002195 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2196 WithDeviceId(touchscreenId))));
2197 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002198 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002199 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002200 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2201 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002202
Prabir Pradhan124ea442022-10-28 20:27:44 +00002203 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2204 TestFixture::mTouchscreen->sendSync();
2205 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002206 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002207 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002208 WithDeviceId(touchscreenId))));
2209 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002210 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002211 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002212 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002213
2214 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002215 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2216 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002217 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002218 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002219}
2220
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002221TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002222 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2223 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2224 const auto stylusId = TestFixture::mStylusInfo.getId();
2225 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002226 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002227
2228 // Press the stylus button.
2229 TestFixture::mStylus->pressKey(BTN_STYLUS);
2230 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2231 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2232 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2233
2234 // Start hovering with the stylus.
2235 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2236 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2237 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2238 TestFixture::mTouchscreen->sendMove(centerPoint);
2239 TestFixture::mTouchscreen->sendSync();
2240 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2241 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2242 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2243 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2244 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2245 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2246 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2247 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2248 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2249
2250 // Touch down with the stylus.
2251 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2252 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2253 TestFixture::mTouchscreen->sendDown(centerPoint);
2254 TestFixture::mTouchscreen->sendSync();
2255 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2256 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2257 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2258
2259 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2260 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2261 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2262
2263 // Stop touching with the stylus, and start hovering.
2264 TestFixture::mTouchscreen->sendUp();
2265 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2266 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2267 TestFixture::mTouchscreen->sendMove(centerPoint);
2268 TestFixture::mTouchscreen->sendSync();
2269 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2270 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2271 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2272 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2273 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2274 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2275 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2276 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2277 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2278
2279 // Stop hovering.
2280 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2281 TestFixture::mTouchscreen->sendSync();
2282 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2283 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2284 WithButtonState(0))));
2285 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2286 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2287 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2288 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2289
2290 // Release the stylus button.
2291 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2292 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2293 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2294 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2295}
2296
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002297TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002298 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2299 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2300 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002301
2302 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002303 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2304 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2305 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2306 TestFixture::mTouchscreen->sendDown(centerPoint);
2307 TestFixture::mTouchscreen->sendSync();
2308 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002309 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002310 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002311 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002312
2313 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002314 TestFixture::mStylus->pressKey(BTN_STYLUS);
2315 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002316 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002317 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2318 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002319 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002320 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002321 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2322 WithDeviceId(touchscreenId))));
2323 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002324 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002325 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002326 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2327 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002328
Prabir Pradhan124ea442022-10-28 20:27:44 +00002329 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2330 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002331 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002332 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2333 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002334 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002335 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002336 WithDeviceId(touchscreenId))));
2337 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002338 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002339 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002340 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002341
2342 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002343 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2344 TestFixture::mTouchscreen->sendSync();
2345 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002346 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002347 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002348 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002349}
2350
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002351TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002352 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2353 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002354 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002355
2356 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2357 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2358 const auto stylusId = TestFixture::mStylusInfo.getId();
2359
2360 // Start a stylus gesture. By the time this event is processed, the configuration change that
2361 // was requested is guaranteed to be completed.
2362 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2363 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2364 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2365 TestFixture::mTouchscreen->sendDown(centerPoint);
2366 TestFixture::mTouchscreen->sendSync();
2367 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2368 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002369 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002370 WithDeviceId(touchscreenId))));
2371
2372 // Press and release a stylus button. Each change only generates a MOVE motion event.
2373 // Key events are unaffected.
2374 TestFixture::mStylus->pressKey(BTN_STYLUS);
2375 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2376 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2377 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2378 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2379 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002380 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002381 WithDeviceId(touchscreenId))));
2382
2383 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2384 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2385 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2386 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2387 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2388 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002389 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002390 WithDeviceId(touchscreenId))));
2391
2392 // Finish the stylus gesture.
2393 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2394 TestFixture::mTouchscreen->sendSync();
2395 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2396 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002397 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002398 WithDeviceId(touchscreenId))));
2399}
2400
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002401// --- ExternalStylusIntegrationTest ---
2402
2403// Verify the behavior of an external stylus. An external stylus can report pressure or button
2404// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2405// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002406using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002407
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002408TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2409 // Create an external stylus capable of reporting pressure data that
2410 // should be fused with a touch pointer.
2411 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2412 createUinputDevice<UinputExternalStylusWithPressure>();
2413 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2414 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002415 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002416 ASSERT_TRUE(stylusInfo);
2417
2418 // Connecting an external stylus changes the source of the touchscreen.
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002419 const auto deviceInfo = waitForDevice(mDevice->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002420 ASSERT_TRUE(deviceInfo);
2421 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2422}
2423
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002424TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002425 const Point centerPoint = mDevice->getCenterPoint();
2426
2427 // Create an external stylus capable of reporting pressure data that
2428 // should be fused with a touch pointer.
2429 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2430 createUinputDevice<UinputExternalStylusWithPressure>();
2431 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2432 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002433 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002434 ASSERT_TRUE(stylusInfo);
2435
2436 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2437
2438 const auto touchscreenId = mDeviceInfo.getId();
2439
2440 // Set a pressure value on the stylus. It doesn't generate any events.
2441 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2442 stylus->setPressure(100);
2443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2444
2445 // Start a finger gesture, and ensure it shows up as stylus gesture
2446 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002447 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002448 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002449 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002450 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451 mDevice->sendSync();
2452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002453 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2454 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2455 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002456
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002457 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2458 // event with the updated pressure.
2459 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002460 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002461 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2462 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2463 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002464
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002465 // The external stylus did not generate any events.
2466 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2468}
2469
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002470TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002471 const Point centerPoint = mDevice->getCenterPoint();
2472
2473 // Create an external stylus capable of reporting pressure data that
2474 // should be fused with a touch pointer.
2475 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2476 createUinputDevice<UinputExternalStylusWithPressure>();
2477 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002479 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002480 ASSERT_TRUE(stylusInfo);
2481
2482 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2483
2484 const auto touchscreenId = mDeviceInfo.getId();
2485
2486 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2487 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002488 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2489 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002490 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002492
2493 // Start a finger gesture. The touch device will withhold generating any touches for
2494 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2495 mDevice->sendSlot(FIRST_SLOT);
2496 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2497 mDevice->sendToolType(MT_TOOL_FINGER);
2498 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002499 const auto syncTime = std::chrono::system_clock::now();
2500 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2501 // is generated in that period, there will be a race condition between the event being generated
2502 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2503 // will reduce the liklihood of the race condition occurring.
2504 const auto waitUntilTimeForNoEvent =
2505 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002506 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002507 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002509 // Since the external stylus did not report a pressure value within the timeout,
2510 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002511 const auto waitUntilTimeForEvent = syncTime +
2512 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2513 ASSERT_NO_FATAL_FAILURE(
2514 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2515 AMOTION_EVENT_ACTION_DOWN),
2516 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2517 AINPUT_SOURCE_STYLUS),
2518 WithToolType(ToolType::FINGER),
2519 WithDeviceId(touchscreenId),
2520 WithPressure(1.f)),
2521 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002522
2523 // Change the pressure on the external stylus. Since the pressure was not present at the start
2524 // of the gesture, it is ignored for now.
2525 stylus->setPressure(200);
2526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2527
2528 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002529 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2530 mDevice->sendSync();
2531 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2532 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002533 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002534 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002535
2536 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2537 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2538 mDevice->sendToolType(MT_TOOL_FINGER);
2539 mDevice->sendDown(centerPoint);
2540 mDevice->sendSync();
2541 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002542 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2543 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2544 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002545
2546 // The external stylus did not generate any events.
2547 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2548 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002549}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002550
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002551TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002552 const Point centerPoint = mDevice->getCenterPoint();
2553
2554 // Create an external stylus device that does not support pressure. It should not affect any
2555 // touch pointers.
2556 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2557 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2558 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002559 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002560 ASSERT_TRUE(stylusInfo);
2561
2562 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2563
2564 const auto touchscreenId = mDeviceInfo.getId();
2565
2566 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2567 // pressure data from the external stylus.
2568 mDevice->sendSlot(FIRST_SLOT);
2569 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2570 mDevice->sendToolType(MT_TOOL_FINGER);
2571 mDevice->sendDown(centerPoint);
2572 auto waitUntil = std::chrono::system_clock::now() +
2573 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2574 mDevice->sendSync();
2575 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002576 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2577 AMOTION_EVENT_ACTION_DOWN),
2578 WithToolType(ToolType::FINGER),
2579 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2580 AINPUT_SOURCE_STYLUS),
2581 WithButtonState(0),
2582 WithDeviceId(touchscreenId),
2583 WithPressure(1.f)),
2584 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002585
2586 // The external stylus did not generate any events.
2587 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2588 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2589}
2590
Michael Wrightd02c5b62014-02-10 15:10:22 -08002591// --- InputDeviceTest ---
2592class InputDeviceTest : public testing::Test {
2593protected:
2594 static const char* DEVICE_NAME;
2595 static const char* DEVICE_LOCATION;
2596 static const int32_t DEVICE_ID;
2597 static const int32_t DEVICE_GENERATION;
2598 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002599 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002600 static const int32_t EVENTHUB_ID;
2601 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2602
2603 std::shared_ptr<FakeEventHub> mFakeEventHub;
2604 sp<FakeInputReaderPolicy> mFakePolicy;
2605 std::unique_ptr<TestInputListener> mFakeListener;
2606 std::unique_ptr<InstrumentedInputReader> mReader;
2607 std::shared_ptr<InputDevice> mDevice;
2608
2609 void SetUp() override {
2610 mFakeEventHub = std::make_unique<FakeEventHub>();
2611 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2612 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002613 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002615 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002616 identifier.name = DEVICE_NAME;
2617 identifier.location = DEVICE_LOCATION;
2618 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2619 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2620 identifier);
2621 mReader->pushNextDevice(mDevice);
2622 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002623 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002625
2626 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002627 mFakeListener.reset();
2628 mFakePolicy.clear();
2629 }
2630};
2631
2632const char* InputDeviceTest::DEVICE_NAME = "device";
2633const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2634const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2635const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002636const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002637const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2638 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002639const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2641
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002642TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002644 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2645 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002647
Michael Wrightd02c5b62014-02-10 15:10:22 -08002648TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2649 ASSERT_EQ(mDevice->isEnabled(), false);
2650}
2651
2652TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2653 // Configuration.
2654 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002655 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002656
2657 // Reset.
2658 unused += mDevice->reset(ARBITRARY_TIME);
2659
2660 NotifyDeviceResetArgs resetArgs;
2661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2662 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2663 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2664
2665 // Metadata.
2666 ASSERT_TRUE(mDevice->isIgnored());
2667 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2668
2669 InputDeviceInfo info = mDevice->getDeviceInfo();
2670 ASSERT_EQ(DEVICE_ID, info.getId());
2671 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2672 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2673 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2674
2675 // State queries.
2676 ASSERT_EQ(0, mDevice->getMetaState());
2677
2678 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2679 << "Ignored device should return unknown key code state.";
2680 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2681 << "Ignored device should return unknown scan code state.";
2682 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2683 << "Ignored device should return unknown switch state.";
2684
2685 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2686 uint8_t flags[2] = { 0, 1 };
2687 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2688 << "Ignored device should never mark any key codes.";
2689 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2690 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2691}
2692
2693TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2694 // Configuration.
2695 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2696
2697 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002698 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2699 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002700 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2701 mapper1.setMetaState(AMETA_ALT_ON);
2702 mapper1.addSupportedKeyCode(AKEYCODE_A);
2703 mapper1.addSupportedKeyCode(AKEYCODE_B);
2704 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2705 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2706 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2707 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2708 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2709
2710 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002711 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2712 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002713 mapper2.setMetaState(AMETA_SHIFT_ON);
2714
2715 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002716 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717
Harry Cuttsf13161a2023-03-08 14:15:49 +00002718 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2719 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002720 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002721 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002722
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002723 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2724 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002725
2726 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002727 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002728 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2729 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002730
2731 NotifyDeviceResetArgs resetArgs;
2732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2733 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2734 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2735
2736 // Metadata.
2737 ASSERT_FALSE(mDevice->isIgnored());
2738 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2739
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002740 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002742 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002743 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2744 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2745
2746 // State queries.
2747 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2748 << "Should query mappers and combine meta states.";
2749
2750 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2751 << "Should return unknown key code state when source not supported.";
2752 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2753 << "Should return unknown scan code state when source not supported.";
2754 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2755 << "Should return unknown switch state when source not supported.";
2756
2757 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2758 << "Should query mapper when source is supported.";
2759 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2760 << "Should query mapper when source is supported.";
2761 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2762 << "Should query mapper when source is supported.";
2763
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002764 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002765 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002766 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002767 << "Should do nothing when source is unsupported.";
2768 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2769 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2770 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2771 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2772
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002773 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002774 << "Should query mapper when source is supported.";
2775 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2776 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2777 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2778 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2779
2780 // Event handling.
2781 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002782 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002783 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002784
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002785 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2786 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002787}
2788
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002789TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2790 // Set some behavior to force the configuration to be update.
2791 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2792 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2793 AINPUT_SOURCE_KEYBOARD);
2794
2795 std::list<NotifyArgs> unused =
2796 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2797 /*changes=*/{});
2798
2799 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2800}
2801
2802TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2803 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2804 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2805 AINPUT_SOURCE_KEYBOARD);
2806
2807 std::list<NotifyArgs> unused =
2808 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2809 /*changes=*/{});
2810
2811 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2812}
2813
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002814TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2815 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2816 FakeInputMapper& mapper =
2817 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2818 AINPUT_SOURCE_KEYBOARD);
2819 NotifyMotionArgs args1;
2820 NotifySwitchArgs args2;
2821 NotifyKeyArgs args3;
2822 mapper.setProcessResult({args1, args2, args3});
2823
2824 InputReaderConfiguration config;
2825 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2826
2827 RawEvent event;
2828 event.deviceId = EVENTHUB_ID;
2829 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2830
2831 for (auto& arg : notifyArgs) {
2832 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2833 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2834 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2835 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2836 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2837 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2838 }
2839 }
2840}
2841
2842TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2843 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2844 FakeInputMapper& mapper =
2845 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2846 AINPUT_SOURCE_KEYBOARD);
2847 NotifyMotionArgs args;
2848 mapper.setProcessResult({args});
2849
2850 InputReaderConfiguration config;
2851 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2852
2853 RawEvent event;
2854 event.deviceId = EVENTHUB_ID;
2855 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2856
2857 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2858 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2859}
2860
2861TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2862 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2863 FakeInputMapper& mapper =
2864 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2865 AINPUT_SOURCE_KEYBOARD);
2866 NotifyMotionArgs args;
2867 args.policyFlags = POLICY_FLAG_WAKE;
2868 mapper.setProcessResult({args});
2869
2870 InputReaderConfiguration config;
2871 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2872
2873 RawEvent event;
2874 event.deviceId = EVENTHUB_ID;
2875 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2876
2877 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2878 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2879}
2880
Arthur Hung2c9a3342019-07-23 14:18:59 +08002881// A single input device is associated with a specific display. Check that:
2882// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002883// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002884TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002885 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2886 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002887
2888 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002889 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002890 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2891 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002892
2893 // Device should be enabled by default.
2894 ASSERT_TRUE(mDevice->isEnabled());
2895
2896 // Prepare associated info.
2897 constexpr uint8_t hdmi = 1;
2898 const std::string UNIQUE_ID = "local:1";
2899
2900 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002901 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002902 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002903 // Device should be disabled because it is associated with a specific display via
2904 // input port <-> display port association, but the corresponding display is not found
2905 ASSERT_FALSE(mDevice->isEnabled());
2906
2907 // Prepare displays.
2908 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002909 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002910 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002911 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002912 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002913 ASSERT_TRUE(mDevice->isEnabled());
2914
2915 // Device should be disabled after set disable.
2916 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002917 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002918 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002919 ASSERT_FALSE(mDevice->isEnabled());
2920
2921 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002922 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002923 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002924 ASSERT_FALSE(mDevice->isEnabled());
2925}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002926
Christine Franks1ba71cc2021-04-07 14:37:42 -07002927TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2928 // Device should be enabled by default.
2929 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002930 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2931 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002932 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002933 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2934 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002935 ASSERT_TRUE(mDevice->isEnabled());
2936
2937 // Device should be disabled because it is associated with a specific display, but the
2938 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002939 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002940 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002941 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002942 ASSERT_FALSE(mDevice->isEnabled());
2943
2944 // Device should be enabled when a display is found.
2945 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002946 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002947 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002948 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002949 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002950 ASSERT_TRUE(mDevice->isEnabled());
2951
2952 // Device should be disabled after set disable.
2953 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002954 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002955 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002956 ASSERT_FALSE(mDevice->isEnabled());
2957
2958 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002959 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002960 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002961 ASSERT_FALSE(mDevice->isEnabled());
2962}
2963
Christine Franks2a2293c2022-01-18 11:51:16 -08002964TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2965 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002966 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2967 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002968 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002969 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2970 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002971
Christine Franks2a2293c2022-01-18 11:51:16 -08002972 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2973 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002974 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002975 NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002976 const auto initialGeneration = mDevice->getGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002977 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002978 InputReaderConfiguration::Change::DISPLAY_INFO);
Antonio Kantek0ac5e092024-04-22 17:10:27 +00002979 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueIdByPort());
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002980 ASSERT_GT(mDevice->getGeneration(), initialGeneration);
2981 ASSERT_EQ(mDevice->getDeviceInfo().getAssociatedDisplayId(), SECONDARY_DISPLAY_ID);
Christine Franks2a2293c2022-01-18 11:51:16 -08002982}
2983
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002984/**
2985 * This test reproduces a crash caused by a dangling reference that remains after device is added
2986 * and removed. The reference is accessed in InputDevice::dump(..);
2987 */
2988TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2989 constexpr int32_t TEST_EVENTHUB_ID = 10;
2990 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2991
Harry Cutts33476232023-01-30 19:57:29 +00002992 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002993 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2994 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002995 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2996 std::string dumpStr, eventHubDevStr;
2997 device.dump(dumpStr, eventHubDevStr);
2998}
2999
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00003000TEST_F(InputDeviceTest, GetBluetoothAddress) {
3001 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
3002 ASSERT_TRUE(address);
3003 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
3004}
3005
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003006TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
3007 mFakePolicy->clearViewports();
3008 FakeInputMapper& mapper =
3009 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
3010 AINPUT_SOURCE_KEYBOARD);
3011 std::list<NotifyArgs> unused =
3012 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3013 /*changes=*/{});
3014
3015 mapper.assertConfigureWasCalled();
3016 mapper.assertResetWasNotCalled();
3017
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08003018 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003019 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08003020 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003021 .type = EV_SYN,
3022 .code = SYN_REPORT,
3023 .value = 0};
3024
3025 // Events are processed normally.
3026 unused = mDevice->process(&event, /*count=*/1);
3027 mapper.assertProcessWasCalled();
3028
3029 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003030 event.type = EV_SYN;
3031 event.code = SYN_DROPPED;
3032 event.value = 0;
3033 unused = mDevice->process(&event, /*count=*/1);
3034 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003035
3036 // All events until the next SYN_REPORT should be dropped.
3037 event.type = EV_KEY;
3038 event.code = KEY_A;
3039 event.value = 1;
3040 unused = mDevice->process(&event, /*count=*/1);
3041 mapper.assertProcessWasNotCalled();
3042
3043 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00003044 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003045 event.type = EV_SYN;
3046 event.code = SYN_REPORT;
3047 event.value = 0;
3048 unused = mDevice->process(&event, /*count=*/1);
3049 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00003050 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003051
3052 // The mapper receives events normally now.
3053 event.type = EV_KEY;
3054 event.code = KEY_B;
3055 event.value = 1;
3056 unused = mDevice->process(&event, /*count=*/1);
3057 mapper.assertProcessWasCalled();
3058}
3059
Michael Wrightd02c5b62014-02-10 15:10:22 -08003060// --- SwitchInputMapperTest ---
3061
3062class SwitchInputMapperTest : public InputMapperTest {
3063protected:
3064};
3065
3066TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003067 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003068
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003069 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003070}
3071
3072TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003073 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003075 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003076 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003078 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003079 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080}
3081
3082TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003083 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003084 std::list<NotifyArgs> out;
3085 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3086 ASSERT_TRUE(out.empty());
3087 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3088 ASSERT_TRUE(out.empty());
3089 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3090 ASSERT_TRUE(out.empty());
3091 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003092
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003093 ASSERT_EQ(1u, out.size());
3094 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003096 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3097 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003098 args.switchMask);
3099 ASSERT_EQ(uint32_t(0), args.policyFlags);
3100}
3101
Chris Ye87143712020-11-10 05:05:58 +00003102// --- VibratorInputMapperTest ---
3103class VibratorInputMapperTest : public InputMapperTest {
3104protected:
3105 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3106};
3107
3108TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003109 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003110
3111 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3112}
3113
3114TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003115 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003116
3117 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3118}
3119
3120TEST_F(VibratorInputMapperTest, Vibrate) {
3121 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003122 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003123 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003124
3125 VibrationElement pattern(2);
3126 VibrationSequence sequence(2);
3127 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003128 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3129 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003130 sequence.addElement(pattern);
3131 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003132 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3133 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003134 sequence.addElement(pattern);
3135
3136 std::vector<int64_t> timings = {0, 1};
3137 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3138
3139 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003140 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003141 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003142 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003143 // Verify vibrator state listener was notified.
3144 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003145 ASSERT_EQ(1u, out.size());
3146 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3147 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3148 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003149 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003150 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003151 ASSERT_FALSE(mapper.isVibrating());
3152 // Verify vibrator state listener was notified.
3153 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003154 ASSERT_EQ(1u, out.size());
3155 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3156 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3157 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003158}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003159
Chris Yef59a2f42020-10-16 12:55:26 -07003160// --- SensorInputMapperTest ---
3161
3162class SensorInputMapperTest : public InputMapperTest {
3163protected:
3164 static const int32_t ACCEL_RAW_MIN;
3165 static const int32_t ACCEL_RAW_MAX;
3166 static const int32_t ACCEL_RAW_FUZZ;
3167 static const int32_t ACCEL_RAW_FLAT;
3168 static const int32_t ACCEL_RAW_RESOLUTION;
3169
3170 static const int32_t GYRO_RAW_MIN;
3171 static const int32_t GYRO_RAW_MAX;
3172 static const int32_t GYRO_RAW_FUZZ;
3173 static const int32_t GYRO_RAW_FLAT;
3174 static const int32_t GYRO_RAW_RESOLUTION;
3175
3176 static const float GRAVITY_MS2_UNIT;
3177 static const float DEGREE_RADIAN_UNIT;
3178
3179 void prepareAccelAxes();
3180 void prepareGyroAxes();
3181 void setAccelProperties();
3182 void setGyroProperties();
3183 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3184};
3185
3186const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3187const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3188const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3189const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3190const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3191
3192const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3193const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3194const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3195const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3196const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3197
3198const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3199const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3200
3201void SensorInputMapperTest::prepareAccelAxes() {
3202 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3203 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3204 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3205 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3206 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3207 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3208}
3209
3210void SensorInputMapperTest::prepareGyroAxes() {
3211 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3212 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3213 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3214 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3215 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3216 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3217}
3218
3219void SensorInputMapperTest::setAccelProperties() {
3220 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3221 /* sensorDataIndex */ 0);
3222 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3223 /* sensorDataIndex */ 1);
3224 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3225 /* sensorDataIndex */ 2);
3226 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3227 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3228 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3229 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3230 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3231}
3232
3233void SensorInputMapperTest::setGyroProperties() {
3234 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3235 /* sensorDataIndex */ 0);
3236 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3237 /* sensorDataIndex */ 1);
3238 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3239 /* sensorDataIndex */ 2);
3240 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3241 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3242 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3243 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3244 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3245}
3246
3247TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003248 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003249
3250 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3251}
3252
3253TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3254 setAccelProperties();
3255 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003256 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003257
3258 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3259 std::chrono::microseconds(10000),
3260 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003261 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003267
3268 NotifySensorArgs args;
3269 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3270 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3271 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3272
3273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3274 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3275 ASSERT_EQ(args.deviceId, DEVICE_ID);
3276 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3277 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3278 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3279 ASSERT_EQ(args.values, values);
3280 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3281}
3282
3283TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3284 setGyroProperties();
3285 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003286 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003287
3288 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3289 std::chrono::microseconds(10000),
3290 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003291 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3296 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003297
3298 NotifySensorArgs args;
3299 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3300 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3301 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3302
3303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3304 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3305 ASSERT_EQ(args.deviceId, DEVICE_ID);
3306 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3307 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3308 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3309 ASSERT_EQ(args.values, values);
3310 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3311}
3312
Michael Wrightd02c5b62014-02-10 15:10:22 -08003313// --- KeyboardInputMapperTest ---
3314
3315class KeyboardInputMapperTest : public InputMapperTest {
3316protected:
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003317 void SetUp() override {
3318 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
3319 InputDeviceClass::ALPHAKEY);
3320 }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003321 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003322 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003323 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003324
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003325 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003326 int32_t originalKeyCode, int32_t rotatedKeyCode,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003327 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003328};
3329
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003330/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3331 * orientation.
3332 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003333void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003334 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3335 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003336}
3337
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003338void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003339 int32_t originalScanCode, int32_t originalKeyCode,
Linnan Li13bf76a2024-05-05 19:18:02 +08003340 int32_t rotatedKeyCode,
3341 ui::LogicalDisplayId displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003342 NotifyKeyArgs args;
3343
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3346 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3347 ASSERT_EQ(originalScanCode, args.scanCode);
3348 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003349 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003350
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3354 ASSERT_EQ(originalScanCode, args.scanCode);
3355 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003356 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003357}
3358
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003361 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003362
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364}
3365
3366TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3367 const int32_t USAGE_A = 0x070004;
3368 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3370 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003371 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3372 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3373 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003375 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003376 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003377 // Initial metastate is AMETA_NONE.
3378 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379
3380 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003382 NotifyKeyArgs args;
3383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3384 ASSERT_EQ(DEVICE_ID, args.deviceId);
3385 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3386 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3387 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3388 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3389 ASSERT_EQ(KEY_HOME, args.scanCode);
3390 ASSERT_EQ(AMETA_NONE, args.metaState);
3391 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3392 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3393 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3394
3395 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003396 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3398 ASSERT_EQ(DEVICE_ID, args.deviceId);
3399 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3400 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3401 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3402 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3403 ASSERT_EQ(KEY_HOME, args.scanCode);
3404 ASSERT_EQ(AMETA_NONE, args.metaState);
3405 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3406 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3407 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3408
3409 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003410 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3411 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3413 ASSERT_EQ(DEVICE_ID, args.deviceId);
3414 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3415 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3416 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3417 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3418 ASSERT_EQ(0, args.scanCode);
3419 ASSERT_EQ(AMETA_NONE, args.metaState);
3420 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3421 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3422 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3423
3424 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3426 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3428 ASSERT_EQ(DEVICE_ID, args.deviceId);
3429 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3430 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3431 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3432 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3433 ASSERT_EQ(0, args.scanCode);
3434 ASSERT_EQ(AMETA_NONE, args.metaState);
3435 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3436 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3437 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3438
3439 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3443 ASSERT_EQ(DEVICE_ID, args.deviceId);
3444 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3445 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3446 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3447 ASSERT_EQ(0, args.keyCode);
3448 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3449 ASSERT_EQ(AMETA_NONE, args.metaState);
3450 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3451 ASSERT_EQ(0U, args.policyFlags);
3452 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3453
3454 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3456 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3458 ASSERT_EQ(DEVICE_ID, args.deviceId);
3459 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3460 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3461 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3462 ASSERT_EQ(0, args.keyCode);
3463 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3464 ASSERT_EQ(AMETA_NONE, args.metaState);
3465 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3466 ASSERT_EQ(0U, args.policyFlags);
3467 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3468}
3469
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003470TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3471 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3473 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3474
3475 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003476 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003477
3478 // Key down by scan code.
3479 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3480 NotifyKeyArgs args;
3481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3482 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3483
3484 // Key up by scan code.
3485 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3487 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3488}
3489
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003490/**
3491 * Ensure that the readTime is set to the time when the EV_KEY is received.
3492 */
3493TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3495
3496 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003497 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003498 NotifyKeyArgs args;
3499
3500 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003501 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3503 ASSERT_EQ(12, args.readTime);
3504
3505 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003506 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3508 ASSERT_EQ(15, args.readTime);
3509}
3510
Michael Wrightd02c5b62014-02-10 15:10:22 -08003511TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003512 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3513 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003514 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3515 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3516 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003518 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003519 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520
Arthur Hung95f68612022-04-07 14:08:22 +08003521 // Initial metastate is AMETA_NONE.
3522 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003523
3524 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003526 NotifyKeyArgs args;
3527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3528 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003530 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003531
3532 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003533 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3535 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003537
3538 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003539 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3541 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003542 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003543
3544 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003545 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3547 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003549 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550}
3551
3552TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003553 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3554 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3555 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003558 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003559 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003560
Michael Wrighta9cf4192022-12-01 23:46:39 +00003561 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3563 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3564 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3565 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3567 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3568 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3569 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3570}
3571
3572TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003573 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577
Michael Wrightd02c5b62014-02-10 15:10:22 -08003578 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003579 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003580 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003581
Michael Wrighta9cf4192022-12-01 23:46:39 +00003582 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003583 ASSERT_NO_FATAL_FAILURE(
3584 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3585 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3586 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3587 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3588 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3589 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3590 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003591
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003592 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003593 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003594 ASSERT_NO_FATAL_FAILURE(
3595 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3596 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3597 AKEYCODE_DPAD_UP, DISPLAY_ID));
3598 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3599 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3601 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003602
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003603 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003604 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003605 ASSERT_NO_FATAL_FAILURE(
3606 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3607 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3608 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3610 AKEYCODE_DPAD_UP, DISPLAY_ID));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3612 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003613
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003614 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003615 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003616 ASSERT_NO_FATAL_FAILURE(
3617 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3618 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3619 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3620 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3621 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3622 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3623 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624
3625 // Special case: if orientation changes while key is down, we still emit the same keycode
3626 // in the key up as we did in the key down.
3627 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003628 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003629 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3632 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3633 ASSERT_EQ(KEY_UP, args.scanCode);
3634 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3635
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003636 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003637 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003638 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3640 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3641 ASSERT_EQ(KEY_UP, args.scanCode);
3642 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3643}
3644
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003645TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3646 // If the keyboard is not orientation aware,
3647 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003648 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003649
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003650 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003651 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003652 NotifyKeyArgs args;
3653
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003654 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003655 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003659 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660
Michael Wrighta9cf4192022-12-01 23:46:39 +00003661 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003662 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003666 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003667}
3668
3669TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3670 // If the keyboard is orientation aware,
3671 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003672 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003673
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003674 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003675 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003676 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003677 NotifyKeyArgs args;
3678
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003679 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003680 // ^--- already checked by the previous test
3681
Michael Wrighta9cf4192022-12-01 23:46:39 +00003682 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003683 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003684 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3688 ASSERT_EQ(DISPLAY_ID, args.displayId);
3689
Linnan Li13bf76a2024-05-05 19:18:02 +08003690 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003691 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003692 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003693 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3698 ASSERT_EQ(newDisplayId, args.displayId);
3699}
3700
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003702 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003703 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003704
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003705 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003706 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003708 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003709 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710}
3711
Philip Junker4af3b3d2021-12-14 10:36:55 +01003712TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3713 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003714 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Philip Junker4af3b3d2021-12-14 10:36:55 +01003715
3716 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3717 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3718 << "If a mapping is available, the result is equal to the mapping";
3719
3720 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3721 << "If no mapping is available, the result is the key location";
3722}
3723
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003726 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003727
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003728 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003729 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003731 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733}
3734
3735TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003736 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003737 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003739 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003742 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743 ASSERT_TRUE(flags[0]);
3744 ASSERT_FALSE(flags[1]);
3745}
3746
3747TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3749 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3750 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3751 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3752 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3753 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003754
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003755 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003756 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003757 // Initial metastate is AMETA_NONE.
3758 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003759
3760 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003761 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3762 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3763 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003764
3765 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003768 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3769 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3770 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003771 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003772
3773 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3775 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003776 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3777 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3778 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003779 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003780
3781 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003784 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3785 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3786 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
3789 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3791 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003792 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3793 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3794 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796
3797 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003798 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003800 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3801 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3802 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804
3805 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003806 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3807 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003808 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3809 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3810 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003811 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003812}
3813
Chris Yea52ade12020-08-27 16:49:20 -07003814TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3815 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3816 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3817 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3818 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3819
3820 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003821 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Chris Yea52ade12020-08-27 16:49:20 -07003822
Chris Yea52ade12020-08-27 16:49:20 -07003823 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003824 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003825 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3826 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3827 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3828 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3829
3830 NotifyKeyArgs args;
3831 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3834 ASSERT_EQ(AMETA_NONE, args.metaState);
3835 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3836 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3837 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3838
3839 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003840 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3842 ASSERT_EQ(AMETA_NONE, args.metaState);
3843 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3844 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3845 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3846}
3847
Arthur Hung2c9a3342019-07-23 14:18:59 +08003848TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3849 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003850 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3851 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3852 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3853 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003854
3855 // keyboard 2.
3856 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003857 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003858 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003859 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003860 std::shared_ptr<InputDevice> device2 =
3861 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003862 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003864 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3865 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3866 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3867 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003868
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003869 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003870 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003871
Arpit Singh67ca6842023-04-26 14:43:16 +00003872 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003873 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003874 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3875 mFakePolicy
3876 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003877 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003878 std::list<NotifyArgs> unused =
3879 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003880 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003881 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003882
3883 // Prepared displays and associated info.
3884 constexpr uint8_t hdmi1 = 0;
3885 constexpr uint8_t hdmi2 = 1;
3886 const std::string SECONDARY_UNIQUE_ID = "local:1";
3887
3888 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3889 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3890
3891 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003892 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003893 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003894 ASSERT_FALSE(device2->isEnabled());
3895
3896 // Prepare second display.
Linnan Li13bf76a2024-05-05 19:18:02 +08003897 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Michael Wrighta9cf4192022-12-01 23:46:39 +00003898 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003899 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003900 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003901 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003902 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003903 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003904 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003905
3906 // Device should be enabled after the associated display is found.
3907 ASSERT_TRUE(mDevice->isEnabled());
3908 ASSERT_TRUE(device2->isEnabled());
3909
3910 // Test pad key events
3911 ASSERT_NO_FATAL_FAILURE(
3912 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3913 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3914 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3915 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3916 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3917 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3918 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3919
3920 ASSERT_NO_FATAL_FAILURE(
3921 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3922 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3923 AKEYCODE_DPAD_RIGHT, newDisplayId));
3924 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3925 AKEYCODE_DPAD_DOWN, newDisplayId));
3926 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3927 AKEYCODE_DPAD_LEFT, newDisplayId));
3928}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929
arthurhungc903df12020-08-11 15:08:42 +08003930TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3931 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3932 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3933 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3934 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3935 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3936 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3937
3938 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003939 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003940 // Initial metastate is AMETA_NONE.
3941 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003942
3943 // Initialization should have turned all of the lights off.
3944 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3945 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3946 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3947
3948 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003951 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3952 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3953
3954 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003955 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3956 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003957 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3958 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3959
3960 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003963 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3964 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3965
3966 mFakeEventHub->removeDevice(EVENTHUB_ID);
3967 mReader->loopOnce();
3968
3969 // keyboard 2 should default toggle keys.
3970 const std::string USB2 = "USB2";
3971 const std::string DEVICE_NAME2 = "KEYBOARD2";
3972 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3973 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3974 std::shared_ptr<InputDevice> device2 =
3975 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003976 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003977 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3978 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3979 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3980 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3981 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3982 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3983
Arpit Singh67ca6842023-04-26 14:43:16 +00003984 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003985 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003986 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3987 mFakePolicy
3988 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003989 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003990 std::list<NotifyArgs> unused =
3991 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003992 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003993 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003994
3995 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3996 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3997 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003998 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3999 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004000}
4001
Arthur Hungcb40a002021-08-03 14:31:01 +00004002TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4004 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4005 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4006
4007 // Suppose we have two mappers. (DPAD + KEYBOARD)
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004008 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD);
Arthur Hungcb40a002021-08-03 14:31:01 +00004009 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004010 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08004011 // Initial metastate is AMETA_NONE.
4012 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004013
4014 mReader->toggleCapsLockState(DEVICE_ID);
4015 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4016}
4017
Arthur Hungfb3cc112022-04-13 07:39:50 +00004018TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4019 // keyboard 1.
4020 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4021 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4022 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4024 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4026
4027 KeyboardInputMapper& mapper1 =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004028 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004029
4030 // keyboard 2.
4031 const std::string USB2 = "USB2";
4032 const std::string DEVICE_NAME2 = "KEYBOARD2";
4033 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4034 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4035 std::shared_ptr<InputDevice> device2 =
4036 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4037 ftl::Flags<InputDeviceClass>(0));
4038 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4039 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4040 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4041 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4042 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4043 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4044
Arpit Singh67ca6842023-04-26 14:43:16 +00004045 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004046 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00004047 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
4048 mFakePolicy
4049 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004050 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004051 std::list<NotifyArgs> unused =
4052 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004053 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004054 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004055
Arthur Hung95f68612022-04-07 14:08:22 +08004056 // Initial metastate is AMETA_NONE.
4057 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4058 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4059
4060 // Toggle num lock on and off.
4061 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4062 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004063 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4064 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4065 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4066
4067 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4068 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4069 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4070 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4071 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4072
4073 // Toggle caps lock on and off.
4074 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4075 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4076 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4077 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4078 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4079
4080 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4082 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4083 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4084 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4085
4086 // Toggle scroll lock on and off.
4087 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4088 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4089 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4090 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4091 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4092
4093 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4094 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4095 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4096 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4097 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4098}
4099
Arthur Hung2141d542022-08-23 07:45:21 +00004100TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4101 const int32_t USAGE_A = 0x070004;
4102 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4103 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4104
4105 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004106 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung2141d542022-08-23 07:45:21 +00004107 // Key down by scan code.
4108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4109 NotifyKeyArgs args;
4110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4111 ASSERT_EQ(DEVICE_ID, args.deviceId);
4112 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4113 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4114 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4115 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4116 ASSERT_EQ(KEY_HOME, args.scanCode);
4117 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4118
4119 // Disable device, it should synthesize cancellation events for down events.
4120 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004121 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004122
4123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4124 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4125 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4126 ASSERT_EQ(KEY_HOME, args.scanCode);
4127 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4128}
4129
Zixuan Qufecb6062022-11-12 04:44:31 +00004130TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004131 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Zixuan Qufecb6062022-11-12 04:44:31 +00004132 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004133 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4134 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004135
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004136 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004137 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4138
4139 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004140 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004141
4142 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4143 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4144 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4145 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4146 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004147 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4148
4149 // Call change layout association with the same values: Generation shouldn't change
4150 generation = mReader->getContext()->getGeneration();
4151 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4152 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4153 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4154 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004155}
4156
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004157TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4158 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4159 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4160
4161 // Configuration
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004162 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004163 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004164 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004165
4166 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4167 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4168}
4169
Justin Chung71ddb432023-03-27 04:29:07 +00004170TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4171 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4172 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004173 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Justin Chung71ddb432023-03-27 04:29:07 +00004174 NotifyKeyArgs args;
4175
4176 // Key down
4177 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4179 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4180}
4181
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004182// --- KeyboardInputMapperTest_ExternalAlphabeticDevice ---
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004183
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004184class KeyboardInputMapperTest_ExternalAlphabeticDevice : public InputMapperTest {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004185protected:
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004186 void SetUp() override {
4187 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
4188 InputDeviceClass::ALPHAKEY | InputDeviceClass::EXTERNAL);
4189 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004190};
4191
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004192// --- KeyboardInputMapperTest_ExternalNonAlphabeticDevice ---
4193
4194class KeyboardInputMapperTest_ExternalNonAlphabeticDevice : public InputMapperTest {
4195protected:
4196 void SetUp() override {
4197 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
4198 InputDeviceClass::EXTERNAL);
4199 }
4200};
4201
4202TEST_F(KeyboardInputMapperTest_ExternalAlphabeticDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004203 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4204 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004205
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004206 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4207 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4208 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4209 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004211 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004212 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Powei Fengd041c5d2019-05-03 17:11:33 -07004213
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004215 NotifyKeyArgs args;
4216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4217 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4218
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004219 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4221 ASSERT_EQ(uint32_t(0), args.policyFlags);
4222
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004225 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004226
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004227 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4229 ASSERT_EQ(uint32_t(0), args.policyFlags);
4230
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4233 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4234
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004235 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4237 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4238}
4239
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004240TEST_F(KeyboardInputMapperTest_ExternalNonAlphabeticDevice, WakeBehavior_NonAlphabeticKeyboard) {
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004241 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4242 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4243
4244 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4245 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4246 POLICY_FLAG_WAKE);
4247
4248 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004249 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004250
4251 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4252 NotifyKeyArgs args;
4253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4254 ASSERT_EQ(uint32_t(0), args.policyFlags);
4255
4256 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4258 ASSERT_EQ(uint32_t(0), args.policyFlags);
4259
4260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4262 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4263
4264 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4266 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4267}
4268
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004269TEST_F(KeyboardInputMapperTest_ExternalAlphabeticDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004270 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004271
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004272 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4273 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4274 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004275
Powei Fengd041c5d2019-05-03 17:11:33 -07004276 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004277 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004278 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Powei Fengd041c5d2019-05-03 17:11:33 -07004279
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004281 NotifyKeyArgs args;
4282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4283 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4284
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004285 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4287 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4288
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4291 ASSERT_EQ(uint32_t(0), args.policyFlags);
4292
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004293 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4295 ASSERT_EQ(uint32_t(0), args.policyFlags);
4296
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4299 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4300
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004301 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4303 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4304}
4305
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306// --- TouchInputMapperTest ---
4307
4308class TouchInputMapperTest : public InputMapperTest {
4309protected:
4310 static const int32_t RAW_X_MIN;
4311 static const int32_t RAW_X_MAX;
4312 static const int32_t RAW_Y_MIN;
4313 static const int32_t RAW_Y_MAX;
4314 static const int32_t RAW_TOUCH_MIN;
4315 static const int32_t RAW_TOUCH_MAX;
4316 static const int32_t RAW_TOOL_MIN;
4317 static const int32_t RAW_TOOL_MAX;
4318 static const int32_t RAW_PRESSURE_MIN;
4319 static const int32_t RAW_PRESSURE_MAX;
4320 static const int32_t RAW_ORIENTATION_MIN;
4321 static const int32_t RAW_ORIENTATION_MAX;
4322 static const int32_t RAW_DISTANCE_MIN;
4323 static const int32_t RAW_DISTANCE_MAX;
4324 static const int32_t RAW_TILT_MIN;
4325 static const int32_t RAW_TILT_MAX;
4326 static const int32_t RAW_ID_MIN;
4327 static const int32_t RAW_ID_MAX;
4328 static const int32_t RAW_SLOT_MIN;
4329 static const int32_t RAW_SLOT_MAX;
4330 static const float X_PRECISION;
4331 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004332 static const float X_PRECISION_VIRTUAL;
4333 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334
4335 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004336 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337
4338 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4339
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004340 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004341 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004342
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 enum Axes {
4344 POSITION = 1 << 0,
4345 TOUCH = 1 << 1,
4346 TOOL = 1 << 2,
4347 PRESSURE = 1 << 3,
4348 ORIENTATION = 1 << 4,
4349 MINOR = 1 << 5,
4350 ID = 1 << 6,
4351 DISTANCE = 1 << 7,
4352 TILT = 1 << 8,
4353 SLOT = 1 << 9,
4354 TOOL_TYPE = 1 << 10,
4355 };
4356
Michael Wrighta9cf4192022-12-01 23:46:39 +00004357 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004358 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004359 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004361 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 int32_t toRawX(float displayX);
4363 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004364 int32_t toRotatedRawX(float displayX);
4365 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004366 float toCookedX(float rawX, float rawY);
4367 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004368 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004369 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004371 float toDisplayY(int32_t rawY, int32_t displayHeight);
4372
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373};
4374
4375const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4376const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4377const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4378const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4379const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4380const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4381const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4382const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004383const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4384const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004385const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4386const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4387const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4388const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4389const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4390const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4391const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4392const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4393const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4394const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4395const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4396const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004397const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4398 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4399const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4400 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004401const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4402 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403
4404const float TouchInputMapperTest::GEOMETRIC_SCALE =
4405 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4406 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4407
4408const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4409 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4410 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4411};
4412
Michael Wrighta9cf4192022-12-01 23:46:39 +00004413void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004414 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4415 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004416}
4417
4418void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4419 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004420 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004421}
4422
Michael Wrighta9cf4192022-12-01 23:46:39 +00004423void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004424 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4425 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4426 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004427}
4428
Michael Wrightd02c5b62014-02-10 15:10:22 -08004429void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004430 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4431 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4432 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4433 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434}
4435
Jason Gerecke489fda82012-09-07 17:19:40 -07004436void TouchInputMapperTest::prepareLocationCalibration() {
4437 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4438}
4439
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440int32_t TouchInputMapperTest::toRawX(float displayX) {
4441 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4442}
4443
4444int32_t TouchInputMapperTest::toRawY(float displayY) {
4445 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4446}
4447
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004448int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4449 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4450}
4451
4452int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4453 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4454}
4455
Jason Gerecke489fda82012-09-07 17:19:40 -07004456float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4457 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4458 return rawX;
4459}
4460
4461float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4462 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4463 return rawY;
4464}
4465
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004467 return toDisplayX(rawX, DISPLAY_WIDTH);
4468}
4469
4470float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4471 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004472}
4473
4474float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004475 return toDisplayY(rawY, DISPLAY_HEIGHT);
4476}
4477
4478float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4479 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004480}
4481
4482
4483// --- SingleTouchInputMapperTest ---
4484
4485class SingleTouchInputMapperTest : public TouchInputMapperTest {
4486protected:
4487 void prepareButtons();
4488 void prepareAxes(int axes);
4489
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004490 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4491 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4492 void processUp(SingleTouchInputMapper& mappery);
4493 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4494 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4495 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4496 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4497 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4498 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499};
4500
4501void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004502 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503}
4504
4505void SingleTouchInputMapperTest::prepareAxes(int axes) {
4506 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004507 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4508 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 }
4510 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004511 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4512 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513 }
4514 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004515 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4516 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517 }
4518 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004519 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4520 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 }
4522 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004523 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4524 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525 }
4526}
4527
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004528void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532}
4533
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004534void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004537}
4538
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004539void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004541}
4542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004543void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545}
4546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004547void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4548 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550}
4551
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004552void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554}
4555
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004556void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4557 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004558 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560}
4561
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004562void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4563 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565}
4566
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004567void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569}
4570
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 prepareButtons();
4573 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004574 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004575
Josep del Río2d8c79a2023-01-23 19:33:50 +00004576 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577}
4578
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004580 prepareButtons();
4581 prepareAxes(POSITION);
4582 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004583 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004585 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586}
4587
4588TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004590 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 prepareButtons();
4592 prepareAxes(POSITION);
4593 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004594 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595
4596 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004597 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598
4599 // Virtual key is down.
4600 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4601 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4602 processDown(mapper, x, y);
4603 processSync(mapper);
4604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4605
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004606 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004607
4608 // Virtual key is up.
4609 processUp(mapper);
4610 processSync(mapper);
4611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4612
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004613 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004614}
4615
4616TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004617 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004618 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004619 prepareButtons();
4620 prepareAxes(POSITION);
4621 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004622 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004623
4624 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004625 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004626
4627 // Virtual key is down.
4628 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4629 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4630 processDown(mapper, x, y);
4631 processSync(mapper);
4632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004634 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635
4636 // Virtual key is up.
4637 processUp(mapper);
4638 processSync(mapper);
4639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4640
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004641 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642}
4643
4644TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004645 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004646 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647 prepareButtons();
4648 prepareAxes(POSITION);
4649 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004650 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004653 ASSERT_TRUE(
4654 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655 ASSERT_TRUE(flags[0]);
4656 ASSERT_FALSE(flags[1]);
4657}
4658
4659TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004661 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 prepareButtons();
4663 prepareAxes(POSITION);
4664 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004665 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004666
arthurhungdcef2dc2020-08-11 14:47:50 +08004667 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668
4669 NotifyKeyArgs args;
4670
4671 // Press virtual key.
4672 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4673 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4674 processDown(mapper, x, y);
4675 processSync(mapper);
4676
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4678 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4679 ASSERT_EQ(DEVICE_ID, args.deviceId);
4680 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4681 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4682 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4683 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4684 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4685 ASSERT_EQ(KEY_HOME, args.scanCode);
4686 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4687 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4688
4689 // Release virtual key.
4690 processUp(mapper);
4691 processSync(mapper);
4692
4693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4694 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4695 ASSERT_EQ(DEVICE_ID, args.deviceId);
4696 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4697 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4698 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4699 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4700 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4701 ASSERT_EQ(KEY_HOME, args.scanCode);
4702 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4703 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4704
4705 // Should not have sent any motions.
4706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4707}
4708
4709TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004711 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 prepareButtons();
4713 prepareAxes(POSITION);
4714 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004715 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004716
arthurhungdcef2dc2020-08-11 14:47:50 +08004717 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004718
4719 NotifyKeyArgs keyArgs;
4720
4721 // Press virtual key.
4722 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4723 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4724 processDown(mapper, x, y);
4725 processSync(mapper);
4726
4727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4728 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4729 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4730 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4731 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4732 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4733 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4734 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4735 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4736 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4737 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4738
4739 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4740 // into the display area.
4741 y -= 100;
4742 processMove(mapper, x, y);
4743 processSync(mapper);
4744
4745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4746 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4747 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4748 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4749 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4750 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4751 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4752 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4753 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4754 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4755 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4756 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4757
4758 NotifyMotionArgs motionArgs;
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4760 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4761 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4762 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4763 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4764 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4765 ASSERT_EQ(0, motionArgs.flags);
4766 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4767 ASSERT_EQ(0, motionArgs.buttonState);
4768 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004769 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004771 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004772 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4773 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4774 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4775 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4776 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4777
4778 // Keep moving out of bounds. Should generate a pointer move.
4779 y -= 50;
4780 processMove(mapper, x, y);
4781 processSync(mapper);
4782
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4784 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4785 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4786 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4787 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4789 ASSERT_EQ(0, motionArgs.flags);
4790 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4791 ASSERT_EQ(0, motionArgs.buttonState);
4792 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004793 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004794 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004795 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004796 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4797 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4798 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4799 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4800 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4801
4802 // Release out of bounds. Should generate a pointer up.
4803 processUp(mapper);
4804 processSync(mapper);
4805
4806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4807 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4808 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4809 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4810 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4811 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4812 ASSERT_EQ(0, motionArgs.flags);
4813 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4814 ASSERT_EQ(0, motionArgs.buttonState);
4815 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004816 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004818 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004819 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4820 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4821 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4822 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4823 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4824
4825 // Should not have sent any more keys or motions.
4826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4828}
4829
4830TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004832 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 prepareButtons();
4834 prepareAxes(POSITION);
4835 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004836 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004837
arthurhungdcef2dc2020-08-11 14:47:50 +08004838 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004839
4840 NotifyMotionArgs motionArgs;
4841
4842 // Initially go down out of bounds.
4843 int32_t x = -10;
4844 int32_t y = -10;
4845 processDown(mapper, x, y);
4846 processSync(mapper);
4847
4848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4849
4850 // Move into the display area. Should generate a pointer down.
4851 x = 50;
4852 y = 75;
4853 processMove(mapper, x, y);
4854 processSync(mapper);
4855
4856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4857 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4858 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4859 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4860 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4862 ASSERT_EQ(0, motionArgs.flags);
4863 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4864 ASSERT_EQ(0, motionArgs.buttonState);
4865 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004866 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004868 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4870 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4871 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4872 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4873 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4874
4875 // Release. Should generate a pointer up.
4876 processUp(mapper);
4877 processSync(mapper);
4878
4879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4880 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4881 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4882 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4883 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4884 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4885 ASSERT_EQ(0, motionArgs.flags);
4886 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4887 ASSERT_EQ(0, motionArgs.buttonState);
4888 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004889 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004890 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004891 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4893 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4894 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4895 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4896 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4897
4898 // Should not have sent any more keys or motions.
4899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4901}
4902
Santos Cordonfa5cf462017-04-05 10:37:00 -07004903TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004904 addConfigurationProperty("touch.deviceType", "touchScreen");
4905 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4906
Michael Wrighta9cf4192022-12-01 23:46:39 +00004907 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004908 prepareButtons();
4909 prepareAxes(POSITION);
4910 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004911 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004912
arthurhungdcef2dc2020-08-11 14:47:50 +08004913 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004914
4915 NotifyMotionArgs motionArgs;
4916
4917 // Down.
4918 int32_t x = 100;
4919 int32_t y = 125;
4920 processDown(mapper, x, y);
4921 processSync(mapper);
4922
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4924 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4925 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4926 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4927 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4928 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4929 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4930 ASSERT_EQ(0, motionArgs.flags);
4931 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4932 ASSERT_EQ(0, motionArgs.buttonState);
4933 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004934 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004935 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004936 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4938 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4939 1, 0, 0, 0, 0, 0, 0, 0));
4940 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4941 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4942 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4943
4944 // Move.
4945 x += 50;
4946 y += 75;
4947 processMove(mapper, x, y);
4948 processSync(mapper);
4949
4950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4951 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4952 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4953 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4954 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4955 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4957 ASSERT_EQ(0, motionArgs.flags);
4958 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4959 ASSERT_EQ(0, motionArgs.buttonState);
4960 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004961 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004962 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004963 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4965 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4966 1, 0, 0, 0, 0, 0, 0, 0));
4967 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4968 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4969 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4970
4971 // Up.
4972 processUp(mapper);
4973 processSync(mapper);
4974
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4976 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4977 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4978 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4979 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4980 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4981 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4982 ASSERT_EQ(0, motionArgs.flags);
4983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4984 ASSERT_EQ(0, motionArgs.buttonState);
4985 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004986 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004987 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004988 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4990 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4991 1, 0, 0, 0, 0, 0, 0, 0));
4992 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4993 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4994 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4995
4996 // Should not have sent any more keys or motions.
4997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4999}
5000
Michael Wrightd02c5b62014-02-10 15:10:22 -08005001TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005003 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 prepareButtons();
5005 prepareAxes(POSITION);
5006 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005007 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008
arthurhungdcef2dc2020-08-11 14:47:50 +08005009 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010
5011 NotifyMotionArgs motionArgs;
5012
5013 // Down.
5014 int32_t x = 100;
5015 int32_t y = 125;
5016 processDown(mapper, x, y);
5017 processSync(mapper);
5018
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5020 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5021 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5022 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5023 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5024 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5025 ASSERT_EQ(0, motionArgs.flags);
5026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5027 ASSERT_EQ(0, motionArgs.buttonState);
5028 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005029 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005031 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5033 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5034 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5035 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5036 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5037
5038 // Move.
5039 x += 50;
5040 y += 75;
5041 processMove(mapper, x, y);
5042 processSync(mapper);
5043
5044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5045 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5046 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5047 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5048 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5050 ASSERT_EQ(0, motionArgs.flags);
5051 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5052 ASSERT_EQ(0, motionArgs.buttonState);
5053 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005054 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005056 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5058 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5059 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5060 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5061 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5062
5063 // Up.
5064 processUp(mapper);
5065 processSync(mapper);
5066
5067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5068 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5069 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5070 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5071 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5072 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5073 ASSERT_EQ(0, motionArgs.flags);
5074 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5075 ASSERT_EQ(0, motionArgs.buttonState);
5076 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005077 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005078 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005079 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5081 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5082 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5083 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5084 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5085
5086 // Should not have sent any more keys or motions.
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5089}
5090
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005091TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005092 addConfigurationProperty("touch.deviceType", "touchScreen");
5093 prepareButtons();
5094 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005095 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5096 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005097 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098
5099 NotifyMotionArgs args;
5100
5101 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005102 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005103 processDown(mapper, toRawX(50), toRawY(75));
5104 processSync(mapper);
5105
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5107 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5108 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5109
5110 processUp(mapper);
5111 processSync(mapper);
5112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5113}
5114
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005115TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116 addConfigurationProperty("touch.deviceType", "touchScreen");
5117 prepareButtons();
5118 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005119 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5120 // orientation-aware are affected by display rotation.
5121 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005122 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123
5124 NotifyMotionArgs args;
5125
5126 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005127 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005128 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129 processDown(mapper, toRawX(50), toRawY(75));
5130 processSync(mapper);
5131
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5133 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5134 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5135
5136 processUp(mapper);
5137 processSync(mapper);
5138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5139
5140 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005141 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005142 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005143 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005144 processSync(mapper);
5145
5146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5147 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5148 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5149
5150 processUp(mapper);
5151 processSync(mapper);
5152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5153
5154 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005155 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005156 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5158 processSync(mapper);
5159
5160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5161 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5162 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5163
5164 processUp(mapper);
5165 processSync(mapper);
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5167
5168 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005169 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005170 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005171 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005172 processSync(mapper);
5173
5174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5175 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5176 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5177
5178 processUp(mapper);
5179 processSync(mapper);
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5181}
5182
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005183TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5184 addConfigurationProperty("touch.deviceType", "touchScreen");
5185 prepareButtons();
5186 prepareAxes(POSITION);
5187 addConfigurationProperty("touch.orientationAware", "1");
5188 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5189 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005190 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005191 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005192 NotifyMotionArgs args;
5193
5194 // Orientation 0.
5195 processDown(mapper, toRawX(50), toRawY(75));
5196 processSync(mapper);
5197
5198 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5199 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5200 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5201
5202 processUp(mapper);
5203 processSync(mapper);
5204 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5205}
5206
5207TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5208 addConfigurationProperty("touch.deviceType", "touchScreen");
5209 prepareButtons();
5210 prepareAxes(POSITION);
5211 addConfigurationProperty("touch.orientationAware", "1");
5212 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5213 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005214 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005215 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005216 NotifyMotionArgs args;
5217
5218 // Orientation 90.
5219 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5220 processSync(mapper);
5221
5222 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5223 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5224 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5225
5226 processUp(mapper);
5227 processSync(mapper);
5228 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5229}
5230
5231TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5232 addConfigurationProperty("touch.deviceType", "touchScreen");
5233 prepareButtons();
5234 prepareAxes(POSITION);
5235 addConfigurationProperty("touch.orientationAware", "1");
5236 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5237 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005238 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005239 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005240 NotifyMotionArgs args;
5241
5242 // Orientation 180.
5243 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5244 processSync(mapper);
5245
5246 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5247 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5248 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5249
5250 processUp(mapper);
5251 processSync(mapper);
5252 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5253}
5254
5255TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5256 addConfigurationProperty("touch.deviceType", "touchScreen");
5257 prepareButtons();
5258 prepareAxes(POSITION);
5259 addConfigurationProperty("touch.orientationAware", "1");
5260 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5261 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005262 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005263 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005264 NotifyMotionArgs args;
5265
5266 // Orientation 270.
5267 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5268 processSync(mapper);
5269
5270 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5271 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5272 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5273
5274 processUp(mapper);
5275 processSync(mapper);
5276 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5277}
5278
5279TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5280 addConfigurationProperty("touch.deviceType", "touchScreen");
5281 prepareButtons();
5282 prepareAxes(POSITION);
5283 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5284 // orientation-aware are affected by display rotation.
5285 addConfigurationProperty("touch.orientationAware", "0");
5286 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005287 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005288
5289 NotifyMotionArgs args;
5290
5291 // Orientation 90, Rotation 0.
5292 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005293 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005294 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5295 processSync(mapper);
5296
5297 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5298 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5299 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5300
5301 processUp(mapper);
5302 processSync(mapper);
5303 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5304
5305 // Orientation 90, Rotation 90.
5306 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005307 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005308 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005309 processSync(mapper);
5310
5311 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5312 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5313 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5314
5315 processUp(mapper);
5316 processSync(mapper);
5317 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5318
5319 // Orientation 90, Rotation 180.
5320 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005321 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005322 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5323 processSync(mapper);
5324
5325 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5326 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5327 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5328
5329 processUp(mapper);
5330 processSync(mapper);
5331 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5332
5333 // Orientation 90, Rotation 270.
5334 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005335 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005336 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 -07005337 processSync(mapper);
5338
5339 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5340 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5341 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5342
5343 processUp(mapper);
5344 processSync(mapper);
5345 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5346}
5347
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005348TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5349 addConfigurationProperty("touch.deviceType", "touchScreen");
5350 prepareButtons();
5351 prepareAxes(POSITION);
5352 addConfigurationProperty("touch.orientationAware", "1");
5353 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005354 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005355
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);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005363 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005364
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 be ignored.
5370 const std::array<Point, 6> outsidePoints = {
5371 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5372 for (const auto& p : outsidePoints) {
5373 processMove(mapper, toRawX(p.x), toRawY(p.y));
5374 processSync(mapper);
5375 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5376 }
5377
5378 // Move the touch into the physical frame.
5379 processMove(mapper, toRawX(25), toRawY(605));
5380 processSync(mapper);
5381 NotifyMotionArgs args;
5382 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5383 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5384 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5385 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5386
5387 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5388 for (const auto& p : outsidePoints) {
5389 processMove(mapper, toRawX(p.x), toRawY(p.y));
5390 processSync(mapper);
5391 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5392 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5393 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5394 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5395 }
5396
5397 processUp(mapper);
5398 processSync(mapper);
5399 EXPECT_NO_FATAL_FAILURE(
5400 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5401}
5402
Harry Cutts1db43992023-06-19 17:05:07 +00005403TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
Harry Cutts1db43992023-06-19 17:05:07 +00005404 addConfigurationProperty("touch.deviceType", "pointer");
5405 prepareAxes(POSITION);
5406 prepareDisplay(ui::ROTATION_0);
5407 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5408
5409 // Set a physical frame in the display viewport.
5410 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5411 viewport->physicalLeft = 20;
5412 viewport->physicalTop = 600;
5413 viewport->physicalRight = 30;
5414 viewport->physicalBottom = 610;
5415 mFakePolicy->updateViewport(*viewport);
5416 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5417
5418 // Start the touch.
5419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5420 processSync(mapper);
5421
5422 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5423 // produced.
5424 const std::array<Point, 6> outsidePoints = {
5425 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5426 for (const auto& p : outsidePoints) {
5427 processMove(mapper, toRawX(p.x), toRawY(p.y));
5428 processSync(mapper);
5429 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5430 }
5431}
5432
Michael Wrightd02c5b62014-02-10 15:10:22 -08005433TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005434 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005435 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436 prepareButtons();
5437 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005438 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439
5440 // These calculations are based on the input device calibration documentation.
5441 int32_t rawX = 100;
5442 int32_t rawY = 200;
5443 int32_t rawPressure = 10;
5444 int32_t rawToolMajor = 12;
5445 int32_t rawDistance = 2;
5446 int32_t rawTiltX = 30;
5447 int32_t rawTiltY = 110;
5448
5449 float x = toDisplayX(rawX);
5450 float y = toDisplayY(rawY);
5451 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5452 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5453 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5454 float distance = float(rawDistance);
5455
5456 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5457 float tiltScale = M_PI / 180;
5458 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5459 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5460 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5461 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5462
5463 processDown(mapper, rawX, rawY);
5464 processPressure(mapper, rawPressure);
5465 processToolMajor(mapper, rawToolMajor);
5466 processDistance(mapper, rawDistance);
5467 processTilt(mapper, rawTiltX, rawTiltY);
5468 processSync(mapper);
5469
5470 NotifyMotionArgs args;
5471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5473 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5474 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5475}
5476
Jason Gerecke489fda82012-09-07 17:19:40 -07005477TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005478 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005479 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005480 prepareLocationCalibration();
5481 prepareButtons();
5482 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005483 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005484
5485 int32_t rawX = 100;
5486 int32_t rawY = 200;
5487
5488 float x = toDisplayX(toCookedX(rawX, rawY));
5489 float y = toDisplayY(toCookedY(rawX, rawY));
5490
5491 processDown(mapper, rawX, rawY);
5492 processSync(mapper);
5493
5494 NotifyMotionArgs args;
5495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5497 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5498}
5499
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005502 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503 prepareButtons();
5504 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005505 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506
5507 NotifyMotionArgs motionArgs;
5508 NotifyKeyArgs keyArgs;
5509
5510 processDown(mapper, 100, 200);
5511 processSync(mapper);
5512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5513 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5514 ASSERT_EQ(0, motionArgs.buttonState);
5515
5516 // press BTN_LEFT, release BTN_LEFT
5517 processKey(mapper, BTN_LEFT, 1);
5518 processSync(mapper);
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5521 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5522
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5525 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5526
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527 processKey(mapper, BTN_LEFT, 0);
5528 processSync(mapper);
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005535 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536
5537 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5538 processKey(mapper, BTN_RIGHT, 1);
5539 processKey(mapper, BTN_MIDDLE, 1);
5540 processSync(mapper);
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5542 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5543 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5544 motionArgs.buttonState);
5545
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5548 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5549
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5551 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5552 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5553 motionArgs.buttonState);
5554
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555 processKey(mapper, BTN_RIGHT, 0);
5556 processSync(mapper);
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005560
5561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005563 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005564
5565 processKey(mapper, BTN_MIDDLE, 0);
5566 processSync(mapper);
5567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005568 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005569 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005570
5571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005572 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005573 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005574
5575 // press BTN_BACK, release BTN_BACK
5576 processKey(mapper, BTN_BACK, 1);
5577 processSync(mapper);
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5579 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5580 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005581
Michael Wrightd02c5b62014-02-10 15:10:22 -08005582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005584 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5585
5586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5587 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5588 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589
5590 processKey(mapper, BTN_BACK, 0);
5591 processSync(mapper);
5592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005593 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005595
5596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005598 ASSERT_EQ(0, motionArgs.buttonState);
5599
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5601 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5602 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5603
5604 // press BTN_SIDE, release BTN_SIDE
5605 processKey(mapper, BTN_SIDE, 1);
5606 processSync(mapper);
5607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5608 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5609 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005610
Michael Wrightd02c5b62014-02-10 15:10:22 -08005611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005613 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5614
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5617 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005618
5619 processKey(mapper, BTN_SIDE, 0);
5620 processSync(mapper);
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005622 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005624
5625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005627 ASSERT_EQ(0, motionArgs.buttonState);
5628
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5630 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5631 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5632
5633 // press BTN_FORWARD, release BTN_FORWARD
5634 processKey(mapper, BTN_FORWARD, 1);
5635 processSync(mapper);
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5637 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5638 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005639
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005642 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5643
5644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5645 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5646 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005647
5648 processKey(mapper, BTN_FORWARD, 0);
5649 processSync(mapper);
5650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005651 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005652 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005653
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005656 ASSERT_EQ(0, motionArgs.buttonState);
5657
Michael Wrightd02c5b62014-02-10 15:10:22 -08005658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5659 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5660 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5661
5662 // press BTN_EXTRA, release BTN_EXTRA
5663 processKey(mapper, BTN_EXTRA, 1);
5664 processSync(mapper);
5665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5666 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5667 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005668
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005671 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5672
5673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5674 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5675 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005676
5677 processKey(mapper, BTN_EXTRA, 0);
5678 processSync(mapper);
5679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005680 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005682
5683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005685 ASSERT_EQ(0, motionArgs.buttonState);
5686
Michael Wrightd02c5b62014-02-10 15:10:22 -08005687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5688 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5689 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5690
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5692
Michael Wrightd02c5b62014-02-10 15:10:22 -08005693 // press BTN_STYLUS, release BTN_STYLUS
5694 processKey(mapper, BTN_STYLUS, 1);
5695 processSync(mapper);
5696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005698 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5699
5700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5701 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5702 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703
5704 processKey(mapper, BTN_STYLUS, 0);
5705 processSync(mapper);
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005707 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005708 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005709
5710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005712 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005713
5714 // press BTN_STYLUS2, release BTN_STYLUS2
5715 processKey(mapper, BTN_STYLUS2, 1);
5716 processSync(mapper);
5717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005719 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5720
5721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5722 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5723 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724
5725 processKey(mapper, BTN_STYLUS2, 0);
5726 processSync(mapper);
5727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005728 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005730
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005733 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734
5735 // release touch
5736 processUp(mapper);
5737 processSync(mapper);
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5740 ASSERT_EQ(0, motionArgs.buttonState);
5741}
5742
5743TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005744 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005745 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005746 prepareButtons();
5747 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005748 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749
5750 NotifyMotionArgs motionArgs;
5751
5752 // default tool type is finger
5753 processDown(mapper, 100, 200);
5754 processSync(mapper);
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5756 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005757 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758
5759 // eraser
5760 processKey(mapper, BTN_TOOL_RUBBER, 1);
5761 processSync(mapper);
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005764 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765
5766 // stylus
5767 processKey(mapper, BTN_TOOL_RUBBER, 0);
5768 processKey(mapper, BTN_TOOL_PEN, 1);
5769 processSync(mapper);
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005772 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773
5774 // brush
5775 processKey(mapper, BTN_TOOL_PEN, 0);
5776 processKey(mapper, BTN_TOOL_BRUSH, 1);
5777 processSync(mapper);
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005780 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781
5782 // pencil
5783 processKey(mapper, BTN_TOOL_BRUSH, 0);
5784 processKey(mapper, BTN_TOOL_PENCIL, 1);
5785 processSync(mapper);
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005788 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005790 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005791 processKey(mapper, BTN_TOOL_PENCIL, 0);
5792 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5793 processSync(mapper);
5794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5795 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005796 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005797
5798 // mouse
5799 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5800 processKey(mapper, BTN_TOOL_MOUSE, 1);
5801 processSync(mapper);
5802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005804 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805
5806 // lens
5807 processKey(mapper, BTN_TOOL_MOUSE, 0);
5808 processKey(mapper, BTN_TOOL_LENS, 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 // double-tap
5815 processKey(mapper, BTN_TOOL_LENS, 0);
5816 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5817 processSync(mapper);
5818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005820 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005821
5822 // triple-tap
5823 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5824 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5825 processSync(mapper);
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005828 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829
5830 // quad-tap
5831 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5832 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5833 processSync(mapper);
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005836 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837
5838 // finger
5839 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5840 processKey(mapper, BTN_TOOL_FINGER, 1);
5841 processSync(mapper);
5842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5843 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005844 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845
5846 // stylus trumps finger
5847 processKey(mapper, BTN_TOOL_PEN, 1);
5848 processSync(mapper);
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5850 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005851 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852
5853 // eraser trumps stylus
5854 processKey(mapper, BTN_TOOL_RUBBER, 1);
5855 processSync(mapper);
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005858 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005859
5860 // mouse trumps eraser
5861 processKey(mapper, BTN_TOOL_MOUSE, 1);
5862 processSync(mapper);
5863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005865 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866
5867 // back to default tool type
5868 processKey(mapper, BTN_TOOL_MOUSE, 0);
5869 processKey(mapper, BTN_TOOL_RUBBER, 0);
5870 processKey(mapper, BTN_TOOL_PEN, 0);
5871 processKey(mapper, BTN_TOOL_FINGER, 0);
5872 processSync(mapper);
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005875 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005876}
5877
5878TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005880 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005881 prepareButtons();
5882 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005883 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005884 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885
5886 NotifyMotionArgs motionArgs;
5887
5888 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5889 processKey(mapper, BTN_TOOL_FINGER, 1);
5890 processMove(mapper, 100, 200);
5891 processSync(mapper);
5892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5893 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5894 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5895 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5896
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5898 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5899 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5900 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5901
5902 // move a little
5903 processMove(mapper, 150, 250);
5904 processSync(mapper);
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5906 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5908 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5909
5910 // down when BTN_TOUCH is pressed, pressure defaults to 1
5911 processKey(mapper, BTN_TOUCH, 1);
5912 processSync(mapper);
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5914 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5916 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5917
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5919 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5920 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5921 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5922
5923 // up when BTN_TOUCH is released, hover restored
5924 processKey(mapper, BTN_TOUCH, 0);
5925 processSync(mapper);
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5927 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5929 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5930
5931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5932 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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_HOVER_MOVE, motionArgs.action);
5938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5939 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5940
5941 // exit hover when pointer goes away
5942 processKey(mapper, BTN_TOOL_FINGER, 0);
5943 processSync(mapper);
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5945 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5947 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5948}
5949
5950TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005952 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005953 prepareButtons();
5954 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005955 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005956
5957 NotifyMotionArgs motionArgs;
5958
5959 // initially hovering because pressure is 0
5960 processDown(mapper, 100, 200);
5961 processPressure(mapper, 0);
5962 processSync(mapper);
5963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5964 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5966 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5967
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5969 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5971 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5972
5973 // move a little
5974 processMove(mapper, 150, 250);
5975 processSync(mapper);
5976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5977 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5978 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5979 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5980
5981 // down when pressure is non-zero
5982 processPressure(mapper, RAW_PRESSURE_MAX);
5983 processSync(mapper);
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5985 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5987 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5988
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5990 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5991 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5992 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5993
5994 // up when pressure becomes 0, hover restored
5995 processPressure(mapper, 0);
5996 processSync(mapper);
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5998 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5999 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6000 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6001
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6003 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6004 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6005 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6006
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6008 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6010 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6011
6012 // exit hover when pointer goes away
6013 processUp(mapper);
6014 processSync(mapper);
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6016 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6018 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6019}
6020
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006021TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6022 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006023 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006024 prepareButtons();
6025 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006026 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006027
6028 // Touch down.
6029 processDown(mapper, 100, 200);
6030 processPressure(mapper, 1);
6031 processSync(mapper);
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6033 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6034
6035 // Reset the mapper. This should cancel the ongoing gesture.
6036 resetMapper(mapper, ARBITRARY_TIME);
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6038 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6039
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6041}
6042
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006043TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6044 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006045 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006046 prepareButtons();
6047 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006048 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006049
6050 // Set the initial state for the touch pointer.
6051 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6052 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6053 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6054 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6055
6056 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006057 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6058 // does not generate any events.
6059 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006060
6061 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6062 // the recreated touch state to generate a down event.
6063 processSync(mapper);
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6065 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6066
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6068}
6069
lilinnan687e58f2022-07-19 16:00:50 +08006070TEST_F(SingleTouchInputMapperTest,
6071 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6072 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006073 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006074 prepareButtons();
6075 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006076 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006077 NotifyMotionArgs motionArgs;
6078
6079 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006080 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006081 processSync(mapper);
6082
6083 // We should receive a down event
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6086
6087 // Change display id
6088 clearViewports();
6089 prepareSecondaryDisplay(ViewportType::INTERNAL);
6090
6091 // We should receive a cancel event
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6093 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6094 // Then receive reset called
6095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6096}
6097
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006098TEST_F(SingleTouchInputMapperTest,
6099 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6100 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006101 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006102 prepareButtons();
6103 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006104 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6106 NotifyMotionArgs motionArgs;
6107
6108 // Start a new gesture.
6109 processDown(mapper, 100, 200);
6110 processSync(mapper);
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6112 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6113
6114 // Make the viewport inactive. This will put the device in disabled mode.
6115 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6116 viewport->isActive = false;
6117 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006118 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006119
6120 // We should receive a cancel event for the ongoing gesture.
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6123 // Then we should be notified that the device was reset.
6124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6125
6126 // No events are generated while the viewport is inactive.
6127 processMove(mapper, 101, 201);
6128 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006129 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006130 processSync(mapper);
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6132
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006133 // Start a new gesture while the viewport is still inactive.
6134 processDown(mapper, 300, 400);
6135 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6136 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6137 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6138 processSync(mapper);
6139
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006140 // Make the viewport active again. The device should resume processing events.
6141 viewport->isActive = true;
6142 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006143 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006144
6145 // The device is reset because it changes back to direct mode, without generating any events.
6146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6148
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006149 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006150 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6152 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006153
6154 // No more events.
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6157}
6158
Prabir Pradhan211ba622022-10-31 21:09:21 +00006159TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6160 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006161 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006162 prepareButtons();
6163 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006164 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6166
6167 // Press a stylus button.
6168 processKey(mapper, BTN_STYLUS, 1);
6169 processSync(mapper);
6170
6171 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6172 processDown(mapper, 100, 200);
6173 processSync(mapper);
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6175 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6176 WithCoords(toDisplayX(100), toDisplayY(200)),
6177 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6179 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6180 WithCoords(toDisplayX(100), toDisplayY(200)),
6181 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6182
6183 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6184 // the button has not actually been released, since there will be no pointers through which the
6185 // button state can be reported. The event is generated at the location of the pointer before
6186 // it went up.
6187 processUp(mapper);
6188 processSync(mapper);
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6190 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6191 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6193 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6194 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6195}
6196
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006197TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6198 addConfigurationProperty("touch.deviceType", "touchScreen");
6199 prepareDisplay(ui::ROTATION_0);
6200 prepareButtons();
6201 prepareAxes(POSITION);
6202
6203 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6204
Arpit Singha8c236b2023-04-25 13:56:05 +00006205 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6207
6208 // Press a stylus button.
6209 processKey(mapper, BTN_STYLUS, 1);
6210 processSync(mapper);
6211
6212 // Start a touch gesture and ensure that the stylus button is not reported.
6213 processDown(mapper, 100, 200);
6214 processSync(mapper);
6215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6216 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6217
6218 // Release and press the stylus button again.
6219 processKey(mapper, BTN_STYLUS, 0);
6220 processSync(mapper);
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6222 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6223 processKey(mapper, BTN_STYLUS, 1);
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6226 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6227
6228 // Release the touch gesture.
6229 processUp(mapper);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6233
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6235}
6236
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006237TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6238 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6239 prepareDisplay(ui::ROTATION_0);
6240 prepareButtons();
6241 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006242 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6244
6245 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6246}
6247
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006248TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6249 // Initialize the device without setting device source to touch navigation.
6250 addConfigurationProperty("touch.deviceType", "touchScreen");
6251 prepareDisplay(ui::ROTATION_0);
6252 prepareButtons();
6253 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006254 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006255
6256 // Ensure that the device is created as a touchscreen, not touch navigation.
6257 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6258
6259 // Add device type association after the device was created.
6260 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6261
6262 // Send update to the mapper.
6263 std::list<NotifyArgs> unused2 =
6264 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006265 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006266
6267 // Check whether device type update was successful.
6268 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6269}
6270
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006271TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6272 // Initialize the device without setting device source to touch navigation.
6273 addConfigurationProperty("touch.deviceType", "touchScreen");
6274 prepareDisplay(ui::ROTATION_0);
6275 prepareButtons();
6276 prepareAxes(POSITION);
6277 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6278
6279 // Set a physical frame in the display viewport.
6280 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6281 viewport->physicalLeft = 0;
6282 viewport->physicalTop = 0;
6283 viewport->physicalRight = DISPLAY_WIDTH / 2;
6284 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6285 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006286 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006287
Arpit Singha8c236b2023-04-25 13:56:05 +00006288 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006289
6290 // Hovering inside the physical frame produces events.
6291 processKey(mapper, BTN_TOOL_PEN, 1);
6292 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6293 processSync(mapper);
6294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6295 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6297 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6298
6299 // Leaving the physical frame ends the hovering gesture.
6300 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6301 processSync(mapper);
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6303 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6304
6305 // Moving outside the physical frame does not produce events.
6306 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6307 processSync(mapper);
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6309
6310 // Re-entering the physical frame produces events.
6311 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6314 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6316 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6317}
6318
Prabir Pradhan5632d622021-09-06 07:57:20 -07006319// --- TouchDisplayProjectionTest ---
6320
6321class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6322public:
6323 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6324 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6325 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006326 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6327 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6328 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006329 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006330 auto rotatedWidth = naturalDisplayWidth;
6331 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006332 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006333 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006334 inverseRotationFlags = ui::Transform::ROT_270;
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_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006338 inverseRotationFlags = ui::Transform::ROT_180;
6339 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006340 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006341 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006342 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006343 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006344 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006345 inverseRotationFlags = ui::Transform::ROT_0;
6346 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006347 }
6348
Prabir Pradhana9df3162022-12-05 23:57:27 +00006349 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006350 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6351
6352 std::optional<DisplayViewport> internalViewport =
6353 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6354 DisplayViewport& v = *internalViewport;
6355 v.displayId = DISPLAY_ID;
6356 v.orientation = orientation;
6357
6358 v.logicalLeft = 0;
6359 v.logicalTop = 0;
6360 v.logicalRight = 100;
6361 v.logicalBottom = 100;
6362
6363 v.physicalLeft = rotatedPhysicalDisplay.left;
6364 v.physicalTop = rotatedPhysicalDisplay.top;
6365 v.physicalRight = rotatedPhysicalDisplay.right;
6366 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6367
Prabir Pradhana9df3162022-12-05 23:57:27 +00006368 v.deviceWidth = rotatedWidth;
6369 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006370
6371 v.isActive = true;
6372 v.uniqueId = UNIQUE_ID;
6373 v.type = ViewportType::INTERNAL;
6374 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006375 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006376 }
6377
6378 void assertReceivedMove(const Point& point) {
6379 NotifyMotionArgs motionArgs;
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006382 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6384 1, 0, 0, 0, 0, 0, 0, 0));
6385 }
6386};
6387
6388TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6389 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006390 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006391
6392 prepareButtons();
6393 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006394 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006395
6396 NotifyMotionArgs motionArgs;
6397
6398 // Configure the DisplayViewport such that the logical display maps to a subsection of
6399 // the display panel called the physical display. Here, the physical display is bounded by the
6400 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6401 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6402 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6403 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6404
Michael Wrighta9cf4192022-12-01 23:46:39 +00006405 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006406 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6407
6408 // Touches outside the physical display should be ignored, and should not generate any
6409 // events. Ensure touches at the following points that lie outside of the physical display
6410 // area do not generate any events.
6411 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6412 processDown(mapper, toRawX(point.x), toRawY(point.y));
6413 processSync(mapper);
6414 processUp(mapper);
6415 processSync(mapper);
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6417 << "Unexpected event generated for touch outside physical display at point: "
6418 << point.x << ", " << point.y;
6419 }
6420 }
6421}
6422
6423TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6424 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006425 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006426
6427 prepareButtons();
6428 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006429 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006430
6431 NotifyMotionArgs motionArgs;
6432
6433 // Configure the DisplayViewport such that the logical display maps to a subsection of
6434 // the display panel called the physical display. Here, the physical display is bounded by the
6435 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6436 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6437
Michael Wrighta9cf4192022-12-01 23:46:39 +00006438 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006439 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6440
6441 // Touches that start outside the physical display should be ignored until it enters the
6442 // physical display bounds, at which point it should generate a down event. Start a touch at
6443 // the point (5, 100), which is outside the physical display bounds.
6444 static const Point kOutsidePoint{5, 100};
6445 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6448
6449 // Move the touch into the physical display area. This should generate a pointer down.
6450 processMove(mapper, toRawX(11), toRawY(21));
6451 processSync(mapper);
6452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6453 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006454 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006455 ASSERT_NO_FATAL_FAILURE(
6456 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6457
6458 // Move the touch inside the physical display area. This should generate a pointer move.
6459 processMove(mapper, toRawX(69), toRawY(159));
6460 processSync(mapper);
6461 assertReceivedMove({69, 159});
6462
6463 // Move outside the physical display area. Since the pointer is already down, this should
6464 // now continue generating events.
6465 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6466 processSync(mapper);
6467 assertReceivedMove(kOutsidePoint);
6468
6469 // Release. This should generate a pointer up.
6470 processUp(mapper);
6471 processSync(mapper);
6472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6473 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6475 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6476
6477 // Ensure no more events were generated.
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6480 }
6481}
6482
Prabir Pradhana9df3162022-12-05 23:57:27 +00006483// --- TouchscreenPrecisionTests ---
6484
6485// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6486// in various orientations and with different display rotations. We configure the touchscreen to
6487// have a higher resolution than that of the display by an integer scale factor in each axis so that
6488// we can enforce that coordinates match precisely as expected.
6489class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6490 public ::testing::WithParamInterface<ui::Rotation> {
6491public:
6492 void SetUp() override {
6493 SingleTouchInputMapperTest::SetUp();
6494
6495 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6496 // four times the resolution of the display in the Y axis.
6497 prepareButtons();
6498 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006499 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6500 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006501 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006502 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6503 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006504 }
6505
6506 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6507 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6508 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6509 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6510
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006511 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6512 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6513
6514 static const int32_t PRECISION_RAW_X_FLAT = 16;
6515 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6516
6517 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6518 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6519
Prabir Pradhana9df3162022-12-05 23:57:27 +00006520 static const std::array<Point, 4> kRawCorners;
6521};
6522
6523const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6524 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6525 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6526 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6527 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6528}};
6529
6530// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6531// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6532// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6533TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6534 enum class Orientation {
6535 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6536 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6537 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6538 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6539 ftl_last = ORIENTATION_270,
6540 };
6541 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6542 Orientation::ORIENTATION_270;
6543 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6544 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6545 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6546 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6547 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6548 };
6549
6550 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6551
6552 // Configure the touchscreen as being installed in the one of the four different orientations
6553 // relative to the display.
6554 addConfigurationProperty("touch.deviceType", "touchScreen");
6555 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6556 prepareDisplay(ui::ROTATION_0);
6557
Arpit Singha8c236b2023-04-25 13:56:05 +00006558 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006559
6560 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6561 // orientations of either 90 or 270) this means the display's natural resolution will be
6562 // flipped.
6563 const bool displayRotated =
6564 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6565 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6566 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6567 const Rect physicalFrame{0, 0, width, height};
6568 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6569
6570 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6571 const float expectedPrecisionX = displayRotated ? 4 : 2;
6572 const float expectedPrecisionY = displayRotated ? 2 : 4;
6573
6574 // Test all four corners.
6575 for (int i = 0; i < 4; i++) {
6576 const auto& raw = kRawCorners[i];
6577 processDown(mapper, raw.x, raw.y);
6578 processSync(mapper);
6579 const auto& expected = expectedPoints[i];
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6581 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6582 WithCoords(expected.x, expected.y),
6583 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6584 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6585 << "with touchscreen orientation "
6586 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6587 << expected.x << ", " << expected.y << ").";
6588 processUp(mapper);
6589 processSync(mapper);
6590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6591 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6592 WithCoords(expected.x, expected.y))));
6593 }
6594}
6595
Prabir Pradhan82687402022-12-06 01:32:53 +00006596TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6597 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6598 kMappedCorners = {
6599 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6600 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6601 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6602 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6603 };
6604
6605 const ui::Rotation displayRotation = GetParam();
6606
6607 addConfigurationProperty("touch.deviceType", "touchScreen");
6608 prepareDisplay(displayRotation);
6609
Arpit Singha8c236b2023-04-25 13:56:05 +00006610 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006611
6612 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6613
6614 // Test all four corners.
6615 for (int i = 0; i < 4; i++) {
6616 const auto& expected = expectedPoints[i];
6617 const auto& raw = kRawCorners[i];
6618 processDown(mapper, raw.x, raw.y);
6619 processSync(mapper);
6620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6621 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6622 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6623 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6624 << "with display rotation " << ui::toCString(displayRotation)
6625 << ", expected point (" << expected.x << ", " << expected.y << ").";
6626 processUp(mapper);
6627 processSync(mapper);
6628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6629 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6630 WithCoords(expected.x, expected.y))));
6631 }
6632}
6633
Prabir Pradhan3e798762022-12-02 21:02:11 +00006634TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6635 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6636 kMappedCorners = {
6637 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6638 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6639 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6640 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6641 };
6642
6643 const ui::Rotation displayRotation = GetParam();
6644
6645 addConfigurationProperty("touch.deviceType", "touchScreen");
6646 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6647
Arpit Singha8c236b2023-04-25 13:56:05 +00006648 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006649
6650 // Ori 270, so width and height swapped
6651 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6652 prepareDisplay(displayRotation);
6653 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6654
6655 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6656
6657 // Test all four corners.
6658 for (int i = 0; i < 4; i++) {
6659 const auto& expected = expectedPoints[i];
6660 const auto& raw = kRawCorners[i];
6661 processDown(mapper, raw.x, raw.y);
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6664 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6665 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6666 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6667 << "with display rotation " << ui::toCString(displayRotation)
6668 << ", expected point (" << expected.x << ", " << expected.y << ").";
6669 processUp(mapper);
6670 processSync(mapper);
6671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6672 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6673 WithCoords(expected.x, expected.y))));
6674 }
6675}
6676
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006677TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6678 const ui::Rotation displayRotation = GetParam();
6679
6680 addConfigurationProperty("touch.deviceType", "touchScreen");
6681 prepareDisplay(displayRotation);
6682
6683 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006684 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006685
6686 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6687 // MotionRanges use display pixels as their units
6688 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6689 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6690
6691 // The MotionRanges should be oriented in the rotated display's coordinate space
6692 const bool displayRotated =
6693 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6694
6695 constexpr float MAX_X = 479.5;
6696 constexpr float MAX_Y = 799.75;
6697 EXPECT_EQ(xRange->min, 0.f);
6698 EXPECT_EQ(yRange->min, 0.f);
6699 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6700 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6701
6702 EXPECT_EQ(xRange->flat, 8.f);
6703 EXPECT_EQ(yRange->flat, 8.f);
6704
6705 EXPECT_EQ(xRange->fuzz, 2.f);
6706 EXPECT_EQ(yRange->fuzz, 2.f);
6707
6708 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6709 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6710}
6711
Prabir Pradhana9df3162022-12-05 23:57:27 +00006712// Run the precision tests for all rotations.
6713INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6714 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6715 ui::ROTATION_270),
6716 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6717 return ftl::enum_string(testParamInfo.param);
6718 });
6719
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006720// --- ExternalStylusFusionTest ---
6721
6722class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6723public:
6724 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6725 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006726 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006727 prepareButtons();
6728 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006729 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006730
6731 mStylusState.when = ARBITRARY_TIME;
6732 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006733 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006734 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006735 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006736 processExternalStylusState(mapper);
6737 return mapper;
6738 }
6739
6740 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6741 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6742 for (const NotifyArgs& args : generatedArgs) {
6743 mFakeListener->notify(args);
6744 }
6745 // Loop the reader to flush the input listener queue.
6746 mReader->loopOnce();
6747 return generatedArgs;
6748 }
6749
6750protected:
6751 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006752
6753 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6754 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006755 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006756
6757 // The first pointer is withheld.
6758 processDown(mapper, 100, 200);
6759 processSync(mapper);
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6761 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6762 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6763
6764 // The external stylus reports pressure. The withheld finger pointer is released as a
6765 // stylus.
6766 mStylusState.pressure = 1.f;
6767 processExternalStylusState(mapper);
6768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6769 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6770 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6771
6772 // Subsequent pointer events are not withheld.
6773 processMove(mapper, 101, 201);
6774 processSync(mapper);
6775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6776 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6777
6778 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6780 }
6781
6782 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6783 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6784
6785 // Releasing the touch pointer ends the gesture.
6786 processUp(mapper);
6787 processSync(mapper);
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006789 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006790 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006791
6792 mStylusState.pressure = 0.f;
6793 processExternalStylusState(mapper);
6794 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6796 }
6797
6798 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006799 // When stylus fusion is not successful, events should be reported with the original source.
6800 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006801 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006802 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006803
6804 // The first pointer is withheld when an external stylus is connected,
6805 // and a timeout is requested.
6806 processDown(mapper, 100, 200);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6809 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6810 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6811
6812 // If the timeout expires early, it is requested again.
6813 handleTimeout(mapper, ARBITRARY_TIME + 1);
6814 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6815 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6816
6817 // When the timeout expires, the withheld touch is released as a finger pointer.
6818 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6820 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6821
6822 // Subsequent pointer events are not withheld.
6823 processMove(mapper, 101, 201);
6824 processSync(mapper);
6825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6826 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6827 processUp(mapper);
6828 processSync(mapper);
6829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6830 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6831
6832 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6834 }
6835
6836private:
6837 InputDeviceInfo mExternalStylusDeviceInfo{};
6838};
6839
6840TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6841 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006842 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006843}
6844
6845TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6846 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6847 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6848}
6849
6850TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6851 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6852 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6853}
6854
6855// Test a successful stylus fusion gesture where the pressure is reported by the external
6856// before the touch is reported by the touchscreen.
6857TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6858 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006859 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006860
6861 // The external stylus reports pressure first. It is ignored for now.
6862 mStylusState.pressure = 1.f;
6863 processExternalStylusState(mapper);
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6865 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6866
6867 // When the touch goes down afterwards, it is reported as a stylus pointer.
6868 processDown(mapper, 100, 200);
6869 processSync(mapper);
6870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6871 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6872 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6873
6874 processMove(mapper, 101, 201);
6875 processSync(mapper);
6876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6877 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6878 processUp(mapper);
6879 processSync(mapper);
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6881 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6882
6883 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6885}
6886
6887TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6888 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6889
6890 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6891 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6892
6893 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6894 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6895 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6896 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6897}
6898
6899TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6900 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006901 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006902
6903 mStylusState.pressure = 0.8f;
6904 processExternalStylusState(mapper);
6905 processDown(mapper, 100, 200);
6906 processSync(mapper);
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6908 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6909 WithPressure(0.8f))));
6910 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6911
6912 // The external stylus reports a pressure change. We wait for some time for a touch event.
6913 mStylusState.pressure = 0.6f;
6914 processExternalStylusState(mapper);
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6916 ASSERT_NO_FATAL_FAILURE(
6917 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6918
6919 // If a touch is reported within the timeout, it reports the updated pressure.
6920 processMove(mapper, 101, 201);
6921 processSync(mapper);
6922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6923 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6924 WithPressure(0.6f))));
6925 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6926
6927 // There is another pressure change.
6928 mStylusState.pressure = 0.5f;
6929 processExternalStylusState(mapper);
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6931 ASSERT_NO_FATAL_FAILURE(
6932 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6933
6934 // If a touch is not reported within the timeout, a move event is generated to report
6935 // the new pressure.
6936 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6938 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6939 WithPressure(0.5f))));
6940
6941 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6942 // repeated indefinitely.
6943 mStylusState.pressure = 0.0f;
6944 processExternalStylusState(mapper);
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6946 ASSERT_NO_FATAL_FAILURE(
6947 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6948 processMove(mapper, 102, 202);
6949 processSync(mapper);
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6951 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6952 WithPressure(0.5f))));
6953 processMove(mapper, 103, 203);
6954 processSync(mapper);
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6956 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6957 WithPressure(0.5f))));
6958
6959 processUp(mapper);
6960 processSync(mapper);
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006962 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006963 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006964
6965 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6967}
6968
6969TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6970 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006971 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006972
6973 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006974 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006975 processExternalStylusState(mapper);
6976 processDown(mapper, 100, 200);
6977 processSync(mapper);
6978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6979 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006980 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006981 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6982
6983 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006984 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006985 processExternalStylusState(mapper);
6986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6987 ASSERT_NO_FATAL_FAILURE(
6988 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6989
6990 // If a touch is reported within the timeout, it reports the updated pressure.
6991 processMove(mapper, 101, 201);
6992 processSync(mapper);
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6994 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006995 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006996 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6997
6998 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006999 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007000 processExternalStylusState(mapper);
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7002 ASSERT_NO_FATAL_FAILURE(
7003 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7004
7005 // If a touch is not reported within the timeout, a move event is generated to report
7006 // the new tool type.
7007 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7009 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007010 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007011
7012 processUp(mapper);
7013 processSync(mapper);
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7015 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007016 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007017
7018 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7020}
7021
7022TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7023 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007024 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007025
7026 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7027
7028 // The external stylus reports a button change. We wait for some time for a touch event.
7029 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7030 processExternalStylusState(mapper);
7031 ASSERT_NO_FATAL_FAILURE(
7032 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7033
7034 // If a touch is reported within the timeout, it reports the updated button state.
7035 processMove(mapper, 101, 201);
7036 processSync(mapper);
7037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7038 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7039 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7041 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7042 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7043 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7044
7045 // The button is now released.
7046 mStylusState.buttons = 0;
7047 processExternalStylusState(mapper);
7048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7049 ASSERT_NO_FATAL_FAILURE(
7050 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7051
7052 // If a touch is not reported within the timeout, a move event is generated to report
7053 // the new button state.
7054 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7056 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7057 WithButtonState(0))));
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007059 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7060 WithButtonState(0))));
7061
7062 processUp(mapper);
7063 processSync(mapper);
7064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007065 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7066
7067 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7069}
7070
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071// --- MultiTouchInputMapperTest ---
7072
7073class MultiTouchInputMapperTest : public TouchInputMapperTest {
7074protected:
7075 void prepareAxes(int axes);
7076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007077 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7078 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7079 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7080 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7081 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7082 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7083 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7084 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7085 void processId(MultiTouchInputMapper& mapper, int32_t id);
7086 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7087 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7088 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007089 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007090 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007091 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7092 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007093};
7094
7095void MultiTouchInputMapperTest::prepareAxes(int axes) {
7096 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 }
7100 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007101 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7102 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007103 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007104 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7105 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106 }
7107 }
7108 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7110 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007112 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007113 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114 }
7115 }
7116 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007117 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7118 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007119 }
7120 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007121 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7122 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123 }
7124 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007125 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7126 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127 }
7128 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007129 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7130 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 }
7132 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7134 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135 }
7136 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007138 }
7139}
7140
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007141void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7142 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145}
7146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007147void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7148 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150}
7151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007152void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7153 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155}
7156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007157void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159}
7160
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007161void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163}
7164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007165void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7166 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168}
7169
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007170void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007171 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172}
7173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007174void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007175 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176}
7177
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007178void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180}
7181
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007182void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184}
7185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007186void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188}
7189
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007190void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7191 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007193}
7194
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007195void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7196 int32_t value) {
7197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7199}
7200
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007201void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203}
7204
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007205void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7206 nsecs_t readTime) {
7207 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208}
7209
Michael Wrightd02c5b62014-02-10 15:10:22 -08007210TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007212 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007213 prepareAxes(POSITION);
7214 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007215 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216
arthurhungdcef2dc2020-08-11 14:47:50 +08007217 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218
7219 NotifyMotionArgs motionArgs;
7220
7221 // Two fingers down at once.
7222 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7223 processPosition(mapper, x1, y1);
7224 processMTSync(mapper);
7225 processPosition(mapper, x2, y2);
7226 processMTSync(mapper);
7227 processSync(mapper);
7228
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7231 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7232 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7233 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7234 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7235 ASSERT_EQ(0, motionArgs.flags);
7236 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7237 ASSERT_EQ(0, motionArgs.buttonState);
7238 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007239 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007240 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007241 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007242 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7243 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7244 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7245 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7247
7248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7249 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7250 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7251 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7252 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007253 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007254 ASSERT_EQ(0, motionArgs.flags);
7255 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7256 ASSERT_EQ(0, motionArgs.buttonState);
7257 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007258 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007259 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007260 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007261 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007262 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7264 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7266 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7267 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7268 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7269 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7270
7271 // Move.
7272 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7273 processPosition(mapper, x1, y1);
7274 processMTSync(mapper);
7275 processPosition(mapper, x2, y2);
7276 processMTSync(mapper);
7277 processSync(mapper);
7278
7279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7280 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7281 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7282 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7283 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7285 ASSERT_EQ(0, motionArgs.flags);
7286 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7287 ASSERT_EQ(0, motionArgs.buttonState);
7288 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007289 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007290 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007291 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007292 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007293 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7295 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7297 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7298 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7299 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7300 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7301
7302 // First finger up.
7303 x2 += 15; y2 -= 20;
7304 processPosition(mapper, x2, y2);
7305 processMTSync(mapper);
7306 processSync(mapper);
7307
7308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7310 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7311 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7312 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007313 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007314 ASSERT_EQ(0, motionArgs.flags);
7315 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7316 ASSERT_EQ(0, motionArgs.buttonState);
7317 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007318 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007319 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007320 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007321 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007322 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7324 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7326 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7327 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7328 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7329 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7330
7331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7332 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7333 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7334 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7335 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7337 ASSERT_EQ(0, motionArgs.flags);
7338 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7339 ASSERT_EQ(0, motionArgs.buttonState);
7340 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007341 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007342 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007343 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7345 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7346 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7347 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7349
7350 // Move.
7351 x2 += 20; y2 -= 25;
7352 processPosition(mapper, x2, y2);
7353 processMTSync(mapper);
7354 processSync(mapper);
7355
7356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7357 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7358 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7359 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7360 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7362 ASSERT_EQ(0, motionArgs.flags);
7363 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7364 ASSERT_EQ(0, motionArgs.buttonState);
7365 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007366 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007367 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007368 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7370 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7371 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7372 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7374
7375 // New finger down.
7376 int32_t x3 = 700, y3 = 300;
7377 processPosition(mapper, x2, y2);
7378 processMTSync(mapper);
7379 processPosition(mapper, x3, y3);
7380 processMTSync(mapper);
7381 processSync(mapper);
7382
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7384 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7385 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7386 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7387 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007388 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007389 ASSERT_EQ(0, motionArgs.flags);
7390 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7391 ASSERT_EQ(0, motionArgs.buttonState);
7392 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007393 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007394 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007395 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007397 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7399 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7401 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7402 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7403 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7404 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7405
7406 // Second finger up.
7407 x3 += 30; y3 -= 20;
7408 processPosition(mapper, x3, y3);
7409 processMTSync(mapper);
7410 processSync(mapper);
7411
7412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7413 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7414 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7415 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7416 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007417 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007418 ASSERT_EQ(0, motionArgs.flags);
7419 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7420 ASSERT_EQ(0, motionArgs.buttonState);
7421 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007422 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007423 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007424 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007425 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007426 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7428 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7430 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7431 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7432 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7433 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7434
7435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7436 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7437 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7438 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7439 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7441 ASSERT_EQ(0, motionArgs.flags);
7442 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7443 ASSERT_EQ(0, motionArgs.buttonState);
7444 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007445 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007446 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007447 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7449 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7450 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7451 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7453
7454 // Last finger up.
7455 processMTSync(mapper);
7456 processSync(mapper);
7457
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7460 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7461 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7462 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7463 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7464 ASSERT_EQ(0, motionArgs.flags);
7465 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7466 ASSERT_EQ(0, motionArgs.buttonState);
7467 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007468 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007469 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007470 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007471 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7472 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7473 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7474 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7476
7477 // Should not have sent any more keys or motions.
7478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7480}
7481
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007482TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7483 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007484 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007485
7486 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7487 /*fuzz*/ 0, /*resolution*/ 10);
7488 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7489 /*fuzz*/ 0, /*resolution*/ 11);
7490 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7491 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7492 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7493 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7494 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7495 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7496 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7497 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7498
Arpit Singha8c236b2023-04-25 13:56:05 +00007499 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007500
7501 // X and Y axes
7502 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7503 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7504 // Touch major and minor
7505 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7506 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7507 // Tool major and minor
7508 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7509 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7510}
7511
7512TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7513 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007514 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007515
7516 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7517 /*fuzz*/ 0, /*resolution*/ 10);
7518 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7519 /*fuzz*/ 0, /*resolution*/ 11);
7520
7521 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7522
Arpit Singha8c236b2023-04-25 13:56:05 +00007523 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007524
7525 // Touch major and minor
7526 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7527 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7528 // Tool major and minor
7529 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7530 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7531}
7532
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007534 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007535 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007536 prepareAxes(POSITION | ID);
7537 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007538 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007539
arthurhungdcef2dc2020-08-11 14:47:50 +08007540 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541
7542 NotifyMotionArgs motionArgs;
7543
7544 // Two fingers down at once.
7545 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7546 processPosition(mapper, x1, y1);
7547 processId(mapper, 1);
7548 processMTSync(mapper);
7549 processPosition(mapper, x2, y2);
7550 processId(mapper, 2);
7551 processMTSync(mapper);
7552 processSync(mapper);
7553
7554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7555 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007556 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007558 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7560 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7561
7562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007563 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007564 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007565 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007566 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007567 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007568 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7570 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7572 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7573
7574 // Move.
7575 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7576 processPosition(mapper, x1, y1);
7577 processId(mapper, 1);
7578 processMTSync(mapper);
7579 processPosition(mapper, x2, y2);
7580 processId(mapper, 2);
7581 processMTSync(mapper);
7582 processSync(mapper);
7583
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7585 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007586 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007588 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007590 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7592 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7594 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7595
7596 // First finger up.
7597 x2 += 15; y2 -= 20;
7598 processPosition(mapper, x2, y2);
7599 processId(mapper, 2);
7600 processMTSync(mapper);
7601 processSync(mapper);
7602
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007604 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007605 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007606 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007607 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007608 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007609 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7611 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7613 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7614
7615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007617 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007618 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007619 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7621 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7622
7623 // Move.
7624 x2 += 20; y2 -= 25;
7625 processPosition(mapper, x2, y2);
7626 processId(mapper, 2);
7627 processMTSync(mapper);
7628 processSync(mapper);
7629
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007632 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007633 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007634 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7636 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7637
7638 // New finger down.
7639 int32_t x3 = 700, y3 = 300;
7640 processPosition(mapper, x2, y2);
7641 processId(mapper, 2);
7642 processMTSync(mapper);
7643 processPosition(mapper, x3, y3);
7644 processId(mapper, 3);
7645 processMTSync(mapper);
7646 processSync(mapper);
7647
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007649 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007650 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007652 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007653 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007654 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007655 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7656 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7658 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7659
7660 // Second finger up.
7661 x3 += 30; y3 -= 20;
7662 processPosition(mapper, x3, y3);
7663 processId(mapper, 3);
7664 processMTSync(mapper);
7665 processSync(mapper);
7666
7667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007668 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007669 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007670 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007671 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007672 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007673 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007674 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7675 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7677 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7678
7679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007681 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007682 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007683 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7685 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7686
7687 // Last finger up.
7688 processMTSync(mapper);
7689 processSync(mapper);
7690
7691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7692 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007693 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007694 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007695 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7697 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7698
7699 // Should not have sent any more keys or motions.
7700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7702}
7703
7704TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007705 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007706 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007707 prepareAxes(POSITION | ID | SLOT);
7708 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007709 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007710
arthurhungdcef2dc2020-08-11 14:47:50 +08007711 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712
7713 NotifyMotionArgs motionArgs;
7714
7715 // Two fingers down at once.
7716 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7717 processPosition(mapper, x1, y1);
7718 processId(mapper, 1);
7719 processSlot(mapper, 1);
7720 processPosition(mapper, x2, y2);
7721 processId(mapper, 2);
7722 processSync(mapper);
7723
7724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7725 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007726 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007727 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007728 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7730 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7731
7732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007733 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007734 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007735 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007736 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007738 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7740 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7742 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7743
7744 // Move.
7745 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7746 processSlot(mapper, 0);
7747 processPosition(mapper, x1, y1);
7748 processSlot(mapper, 1);
7749 processPosition(mapper, x2, y2);
7750 processSync(mapper);
7751
7752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007754 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007755 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007756 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007757 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007758 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7760 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7762 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7763
7764 // First finger up.
7765 x2 += 15; y2 -= 20;
7766 processSlot(mapper, 0);
7767 processId(mapper, -1);
7768 processSlot(mapper, 1);
7769 processPosition(mapper, x2, y2);
7770 processSync(mapper);
7771
7772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007773 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007774 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007775 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007776 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007778 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7780 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7782 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7783
7784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007786 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007788 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7790 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7791
7792 // Move.
7793 x2 += 20; y2 -= 25;
7794 processPosition(mapper, x2, y2);
7795 processSync(mapper);
7796
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007799 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007800 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007801 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7803 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7804
7805 // New finger down.
7806 int32_t x3 = 700, y3 = 300;
7807 processPosition(mapper, x2, y2);
7808 processSlot(mapper, 0);
7809 processId(mapper, 3);
7810 processPosition(mapper, x3, y3);
7811 processSync(mapper);
7812
7813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007814 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007815 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007817 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007819 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7821 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7823 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7824
7825 // Second finger up.
7826 x3 += 30; y3 -= 20;
7827 processSlot(mapper, 1);
7828 processId(mapper, -1);
7829 processSlot(mapper, 0);
7830 processPosition(mapper, x3, y3);
7831 processSync(mapper);
7832
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007834 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007835 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007837 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007838 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007839 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7841 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7842 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7843 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7844
7845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007847 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007849 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7851 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7852
7853 // Last finger up.
7854 processId(mapper, -1);
7855 processSync(mapper);
7856
7857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7858 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007859 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007861 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7863 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7864
7865 // Should not have sent any more keys or motions.
7866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7868}
7869
7870TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007871 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007872 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007873 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007874 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007875
7876 // These calculations are based on the input device calibration documentation.
7877 int32_t rawX = 100;
7878 int32_t rawY = 200;
7879 int32_t rawTouchMajor = 7;
7880 int32_t rawTouchMinor = 6;
7881 int32_t rawToolMajor = 9;
7882 int32_t rawToolMinor = 8;
7883 int32_t rawPressure = 11;
7884 int32_t rawDistance = 0;
7885 int32_t rawOrientation = 3;
7886 int32_t id = 5;
7887
7888 float x = toDisplayX(rawX);
7889 float y = toDisplayY(rawY);
7890 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7891 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7892 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7893 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7894 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7895 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7896 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7897 float distance = float(rawDistance);
7898
7899 processPosition(mapper, rawX, rawY);
7900 processTouchMajor(mapper, rawTouchMajor);
7901 processTouchMinor(mapper, rawTouchMinor);
7902 processToolMajor(mapper, rawToolMajor);
7903 processToolMinor(mapper, rawToolMinor);
7904 processPressure(mapper, rawPressure);
7905 processOrientation(mapper, rawOrientation);
7906 processDistance(mapper, rawDistance);
7907 processId(mapper, id);
7908 processMTSync(mapper);
7909 processSync(mapper);
7910
7911 NotifyMotionArgs args;
7912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7913 ASSERT_EQ(0, args.pointerProperties[0].id);
7914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7915 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7916 orientation, distance));
7917}
7918
7919TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007920 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007921 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007922 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7923 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007924 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007925
7926 // These calculations are based on the input device calibration documentation.
7927 int32_t rawX = 100;
7928 int32_t rawY = 200;
7929 int32_t rawTouchMajor = 140;
7930 int32_t rawTouchMinor = 120;
7931 int32_t rawToolMajor = 180;
7932 int32_t rawToolMinor = 160;
7933
7934 float x = toDisplayX(rawX);
7935 float y = toDisplayY(rawY);
7936 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7937 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7938 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7939 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7940 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7941
7942 processPosition(mapper, rawX, rawY);
7943 processTouchMajor(mapper, rawTouchMajor);
7944 processTouchMinor(mapper, rawTouchMinor);
7945 processToolMajor(mapper, rawToolMajor);
7946 processToolMinor(mapper, rawToolMinor);
7947 processMTSync(mapper);
7948 processSync(mapper);
7949
7950 NotifyMotionArgs args;
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7953 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7954}
7955
7956TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007957 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007958 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007959 prepareAxes(POSITION | TOUCH | TOOL);
7960 addConfigurationProperty("touch.size.calibration", "diameter");
7961 addConfigurationProperty("touch.size.scale", "10");
7962 addConfigurationProperty("touch.size.bias", "160");
7963 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007964 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007965
7966 // These calculations are based on the input device calibration documentation.
7967 // Note: We only provide a single common touch/tool value because the device is assumed
7968 // not to emit separate values for each pointer (isSummed = 1).
7969 int32_t rawX = 100;
7970 int32_t rawY = 200;
7971 int32_t rawX2 = 150;
7972 int32_t rawY2 = 250;
7973 int32_t rawTouchMajor = 5;
7974 int32_t rawToolMajor = 8;
7975
7976 float x = toDisplayX(rawX);
7977 float y = toDisplayY(rawY);
7978 float x2 = toDisplayX(rawX2);
7979 float y2 = toDisplayY(rawY2);
7980 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7981 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7982 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7983
7984 processPosition(mapper, rawX, rawY);
7985 processTouchMajor(mapper, rawTouchMajor);
7986 processToolMajor(mapper, rawToolMajor);
7987 processMTSync(mapper);
7988 processPosition(mapper, rawX2, rawY2);
7989 processTouchMajor(mapper, rawTouchMajor);
7990 processToolMajor(mapper, rawToolMajor);
7991 processMTSync(mapper);
7992 processSync(mapper);
7993
7994 NotifyMotionArgs args;
7995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7996 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7997
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007999 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008000 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8002 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8004 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8005}
8006
8007TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008008 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008009 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008010 prepareAxes(POSITION | TOUCH | TOOL);
8011 addConfigurationProperty("touch.size.calibration", "area");
8012 addConfigurationProperty("touch.size.scale", "43");
8013 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008014 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015
8016 // These calculations are based on the input device calibration documentation.
8017 int32_t rawX = 100;
8018 int32_t rawY = 200;
8019 int32_t rawTouchMajor = 5;
8020 int32_t rawToolMajor = 8;
8021
8022 float x = toDisplayX(rawX);
8023 float y = toDisplayY(rawY);
8024 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8025 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8026 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8027
8028 processPosition(mapper, rawX, rawY);
8029 processTouchMajor(mapper, rawTouchMajor);
8030 processToolMajor(mapper, rawToolMajor);
8031 processMTSync(mapper);
8032 processSync(mapper);
8033
8034 NotifyMotionArgs args;
8035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8037 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8038}
8039
8040TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008042 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043 prepareAxes(POSITION | PRESSURE);
8044 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8045 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008046 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047
Michael Wrightaa449c92017-12-13 21:21:43 +00008048 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008049 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008050 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8051 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8052 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8053
Michael Wrightd02c5b62014-02-10 15:10:22 -08008054 // These calculations are based on the input device calibration documentation.
8055 int32_t rawX = 100;
8056 int32_t rawY = 200;
8057 int32_t rawPressure = 60;
8058
8059 float x = toDisplayX(rawX);
8060 float y = toDisplayY(rawY);
8061 float pressure = float(rawPressure) * 0.01f;
8062
8063 processPosition(mapper, rawX, rawY);
8064 processPressure(mapper, rawPressure);
8065 processMTSync(mapper);
8066 processSync(mapper);
8067
8068 NotifyMotionArgs args;
8069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8071 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8072}
8073
8074TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008076 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008078 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079
8080 NotifyMotionArgs motionArgs;
8081 NotifyKeyArgs keyArgs;
8082
8083 processId(mapper, 1);
8084 processPosition(mapper, 100, 200);
8085 processSync(mapper);
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8087 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8088 ASSERT_EQ(0, motionArgs.buttonState);
8089
8090 // press BTN_LEFT, release BTN_LEFT
8091 processKey(mapper, BTN_LEFT, 1);
8092 processSync(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8095 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8096
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8099 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8100
Michael Wrightd02c5b62014-02-10 15:10:22 -08008101 processKey(mapper, BTN_LEFT, 0);
8102 processSync(mapper);
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008105 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008106
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008109 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110
8111 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8112 processKey(mapper, BTN_RIGHT, 1);
8113 processKey(mapper, BTN_MIDDLE, 1);
8114 processSync(mapper);
8115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8117 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8118 motionArgs.buttonState);
8119
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8122 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8123
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8126 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8127 motionArgs.buttonState);
8128
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129 processKey(mapper, BTN_RIGHT, 0);
8130 processSync(mapper);
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008133 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008134
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138
8139 processKey(mapper, BTN_MIDDLE, 0);
8140 processSync(mapper);
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008143 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008144
8145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008147 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008148
8149 // press BTN_BACK, release BTN_BACK
8150 processKey(mapper, BTN_BACK, 1);
8151 processSync(mapper);
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8153 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8154 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008158 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8162 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163
8164 processKey(mapper, BTN_BACK, 0);
8165 processSync(mapper);
8166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008167 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008169
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008172 ASSERT_EQ(0, motionArgs.buttonState);
8173
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8175 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8176 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8177
8178 // press BTN_SIDE, release BTN_SIDE
8179 processKey(mapper, BTN_SIDE, 1);
8180 processSync(mapper);
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8182 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8183 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008184
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008187 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8188
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8191 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192
8193 processKey(mapper, BTN_SIDE, 0);
8194 processSync(mapper);
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008196 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008198
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008201 ASSERT_EQ(0, motionArgs.buttonState);
8202
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8204 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8205 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8206
8207 // press BTN_FORWARD, release BTN_FORWARD
8208 processKey(mapper, BTN_FORWARD, 1);
8209 processSync(mapper);
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8211 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8212 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008213
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008216 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8217
8218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8220 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221
8222 processKey(mapper, BTN_FORWARD, 0);
8223 processSync(mapper);
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008226 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008227
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008230 ASSERT_EQ(0, motionArgs.buttonState);
8231
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8233 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8234 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8235
8236 // press BTN_EXTRA, release BTN_EXTRA
8237 processKey(mapper, BTN_EXTRA, 1);
8238 processSync(mapper);
8239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8240 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8241 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008242
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008245 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8246
8247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8248 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8249 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250
8251 processKey(mapper, BTN_EXTRA, 0);
8252 processSync(mapper);
8253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008255 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008256
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008259 ASSERT_EQ(0, motionArgs.buttonState);
8260
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8262 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8263 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8264
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8266
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267 // press BTN_STYLUS, release BTN_STYLUS
8268 processKey(mapper, BTN_STYLUS, 1);
8269 processSync(mapper);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008272 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8273
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8275 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8276 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008277
8278 processKey(mapper, BTN_STYLUS, 0);
8279 processSync(mapper);
8280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008281 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008283
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008286 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287
8288 // press BTN_STYLUS2, release BTN_STYLUS2
8289 processKey(mapper, BTN_STYLUS2, 1);
8290 processSync(mapper);
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008293 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8294
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8296 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8297 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008298
8299 processKey(mapper, BTN_STYLUS2, 0);
8300 processSync(mapper);
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008303 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008304
8305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008307 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008308
8309 // release touch
8310 processId(mapper, -1);
8311 processSync(mapper);
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8313 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8314 ASSERT_EQ(0, motionArgs.buttonState);
8315}
8316
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008317TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8318 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008319 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008320 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008321 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008322
8323 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8324 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8325
8326 // Touch down.
8327 processId(mapper, 1);
8328 processPosition(mapper, 100, 200);
8329 processSync(mapper);
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8331 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8332
8333 // Press and release button mapped to the primary stylus button.
8334 processKey(mapper, BTN_A, 1);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8337 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8338 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8340 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8341 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8342
8343 processKey(mapper, BTN_A, 0);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8346 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8348 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8349
8350 // Press and release the HID usage mapped to the secondary stylus button.
8351 processHidUsage(mapper, 0xabcd, 1);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8354 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8355 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8357 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8358 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8359
8360 processHidUsage(mapper, 0xabcd, 0);
8361 processSync(mapper);
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8363 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8365 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8366
8367 // Release touch.
8368 processId(mapper, -1);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8371 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8372}
8373
Michael Wrightd02c5b62014-02-10 15:10:22 -08008374TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008376 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008377 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008378 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008379
8380 NotifyMotionArgs motionArgs;
8381
8382 // default tool type is finger
8383 processId(mapper, 1);
8384 processPosition(mapper, 100, 200);
8385 processSync(mapper);
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8387 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008388 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389
8390 // eraser
8391 processKey(mapper, BTN_TOOL_RUBBER, 1);
8392 processSync(mapper);
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008395 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396
8397 // stylus
8398 processKey(mapper, BTN_TOOL_RUBBER, 0);
8399 processKey(mapper, BTN_TOOL_PEN, 1);
8400 processSync(mapper);
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008403 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404
8405 // brush
8406 processKey(mapper, BTN_TOOL_PEN, 0);
8407 processKey(mapper, BTN_TOOL_BRUSH, 1);
8408 processSync(mapper);
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008411 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008412
8413 // pencil
8414 processKey(mapper, BTN_TOOL_BRUSH, 0);
8415 processKey(mapper, BTN_TOOL_PENCIL, 1);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008419 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008420
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008421 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008422 processKey(mapper, BTN_TOOL_PENCIL, 0);
8423 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8424 processSync(mapper);
8425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8426 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008427 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008428
8429 // mouse
8430 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8431 processKey(mapper, BTN_TOOL_MOUSE, 1);
8432 processSync(mapper);
8433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8434 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008435 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436
8437 // lens
8438 processKey(mapper, BTN_TOOL_MOUSE, 0);
8439 processKey(mapper, BTN_TOOL_LENS, 1);
8440 processSync(mapper);
8441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8442 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008443 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008444
8445 // double-tap
8446 processKey(mapper, BTN_TOOL_LENS, 0);
8447 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8448 processSync(mapper);
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8450 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008451 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008452
8453 // triple-tap
8454 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8455 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8456 processSync(mapper);
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008459 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008460
8461 // quad-tap
8462 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8463 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8464 processSync(mapper);
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008467 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008468
8469 // finger
8470 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8471 processKey(mapper, BTN_TOOL_FINGER, 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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476
8477 // stylus trumps finger
8478 processKey(mapper, BTN_TOOL_PEN, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483
8484 // eraser trumps stylus
8485 processKey(mapper, BTN_TOOL_RUBBER, 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::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490
8491 // mouse trumps eraser
8492 processKey(mapper, BTN_TOOL_MOUSE, 1);
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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008497
8498 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8499 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
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::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504
8505 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8506 processToolType(mapper, MT_TOOL_PEN);
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008510 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008511
8512 // back to default tool type
8513 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8514 processKey(mapper, BTN_TOOL_MOUSE, 0);
8515 processKey(mapper, BTN_TOOL_RUBBER, 0);
8516 processKey(mapper, BTN_TOOL_PEN, 0);
8517 processKey(mapper, BTN_TOOL_FINGER, 0);
8518 processSync(mapper);
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008521 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008522}
8523
8524TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008526 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008527 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008528 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008529 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008530
8531 NotifyMotionArgs motionArgs;
8532
8533 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8534 processId(mapper, 1);
8535 processPosition(mapper, 100, 200);
8536 processSync(mapper);
8537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8538 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8539 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8540 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8541
8542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8543 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8544 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8545 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8546
8547 // move a little
8548 processPosition(mapper, 150, 250);
8549 processSync(mapper);
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8553 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8554
8555 // down when BTN_TOUCH is pressed, pressure defaults to 1
8556 processKey(mapper, BTN_TOUCH, 1);
8557 processSync(mapper);
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8561 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8562
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8564 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8566 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8567
8568 // up when BTN_TOUCH is released, hover restored
8569 processKey(mapper, BTN_TOUCH, 0);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8575
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8579 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8580
8581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8582 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8584 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8585
8586 // exit hover when pointer goes away
8587 processId(mapper, -1);
8588 processSync(mapper);
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8593}
8594
8595TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008596 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008597 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008598 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008599 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008600
8601 NotifyMotionArgs motionArgs;
8602
8603 // initially hovering because pressure is 0
8604 processId(mapper, 1);
8605 processPosition(mapper, 100, 200);
8606 processPressure(mapper, 0);
8607 processSync(mapper);
8608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8609 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8611 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8612
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8614 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8615 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8616 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8617
8618 // move a little
8619 processPosition(mapper, 150, 250);
8620 processSync(mapper);
8621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8622 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8624 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8625
8626 // down when pressure becomes non-zero
8627 processPressure(mapper, RAW_PRESSURE_MAX);
8628 processSync(mapper);
8629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8630 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8632 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8633
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8637 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8638
8639 // up when pressure becomes 0, hover restored
8640 processPressure(mapper, 0);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8645 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8646
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8648 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8650 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8651
8652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8653 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8655 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8656
8657 // exit hover when pointer goes away
8658 processId(mapper, -1);
8659 processSync(mapper);
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8661 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8663 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8664}
8665
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008666/**
8667 * Set the input device port <--> display port associations, and check that the
8668 * events are routed to the display that matches the display port.
8669 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8670 */
8671TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008672 const std::string usb2 = "USB2";
8673 const uint8_t hdmi1 = 0;
8674 const uint8_t hdmi2 = 1;
8675 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008676 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008677
8678 addConfigurationProperty("touch.deviceType", "touchScreen");
8679 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008680 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008681
8682 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8683 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8684
8685 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8686 // for this input device is specified, and the matching viewport is not present,
8687 // the input device should be disabled (at the mapper level).
8688
8689 // Add viewport for display 2 on hdmi2
8690 prepareSecondaryDisplay(type, hdmi2);
8691 // Send a touch event
8692 processPosition(mapper, 100, 100);
8693 processSync(mapper);
8694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8695
8696 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008697 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008698 // Send a touch event again
8699 processPosition(mapper, 100, 100);
8700 processSync(mapper);
8701
8702 NotifyMotionArgs args;
8703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8704 ASSERT_EQ(DISPLAY_ID, args.displayId);
8705}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008706
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008707TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8708 addConfigurationProperty("touch.deviceType", "touchScreen");
8709 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008710 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008711
8712 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8713
Michael Wrighta9cf4192022-12-01 23:46:39 +00008714 prepareDisplay(ui::ROTATION_0);
8715 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008716
8717 // Send a touch event
8718 processPosition(mapper, 100, 100);
8719 processSync(mapper);
8720
8721 NotifyMotionArgs args;
8722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8723 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8724}
8725
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008726TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008727 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008728
Michael Wrighta9cf4192022-12-01 23:46:39 +00008729 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008730 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008731 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008732
Josep del Río2d8c79a2023-01-23 19:33:50 +00008733 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008734
8735 NotifyMotionArgs motionArgs;
8736 processPosition(mapper, 100, 100);
8737 processSync(mapper);
8738
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008741 ASSERT_EQ(ui::LogicalDisplayId::INVALID, motionArgs.displayId);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008742}
8743
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008744/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008745 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8746 */
8747TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8748 addConfigurationProperty("touch.deviceType", "touchScreen");
8749 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008750 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008751
Michael Wrighta9cf4192022-12-01 23:46:39 +00008752 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008753 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8754 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8755 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8756 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008757
8758 NotifyMotionArgs args;
8759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8760 ASSERT_EQ(26, args.readTime);
8761
Harry Cutts33476232023-01-30 19:57:29 +00008762 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8763 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8764 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008765
8766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8767 ASSERT_EQ(33, args.readTime);
8768}
8769
8770/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008771 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8772 * events should not be delivered to the listener.
8773 */
8774TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8775 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008776 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008777 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008778 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008779 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008780 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008781 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008782
8783 NotifyMotionArgs motionArgs;
8784 processPosition(mapper, 100, 100);
8785 processSync(mapper);
8786
8787 mFakeListener->assertNotifyMotionWasNotCalled();
8788}
8789
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008790/**
8791 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8792 * the touch mapper can process the events and the events can be delivered to the listener.
8793 */
8794TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8795 addConfigurationProperty("touch.deviceType", "touchScreen");
8796 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008797 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008798 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008799 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008800 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008801 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008802
8803 NotifyMotionArgs motionArgs;
8804 processPosition(mapper, 100, 100);
8805 processSync(mapper);
8806
8807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8808 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8809}
8810
Josh Thielene986aed2023-06-01 14:17:30 +00008811/**
8812 * When the viewport is deactivated (isActive transitions from true to false),
8813 * and touch.enableForInactiveViewport is false, touches prior to the transition
8814 * should be cancelled.
8815 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008816TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8817 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008818 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008819 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008820 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008821 std::optional<DisplayViewport> optionalDisplayViewport =
8822 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8823 ASSERT_TRUE(optionalDisplayViewport.has_value());
8824 DisplayViewport displayViewport = *optionalDisplayViewport;
8825
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008826 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008827 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008828 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008829
8830 // Finger down
8831 int32_t x = 100, y = 100;
8832 processPosition(mapper, x, y);
8833 processSync(mapper);
8834
8835 NotifyMotionArgs motionArgs;
8836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8837 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8838
8839 // Deactivate display viewport
8840 displayViewport.isActive = false;
8841 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008842 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008843
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008844 // The ongoing touch should be canceled immediately
8845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8846 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8847
8848 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008849 x += 10, y += 10;
8850 processPosition(mapper, x, y);
8851 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008853
8854 // Reactivate display viewport
8855 displayViewport.isActive = true;
8856 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008857 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008858
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008859 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008860 x += 10, y += 10;
8861 processPosition(mapper, x, y);
8862 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8864 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008865}
8866
Josh Thielene986aed2023-06-01 14:17:30 +00008867/**
8868 * When the viewport is deactivated (isActive transitions from true to false),
8869 * and touch.enableForInactiveViewport is true, touches prior to the transition
8870 * should not be cancelled.
8871 */
8872TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8873 addConfigurationProperty("touch.deviceType", "touchScreen");
8874 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8875 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8876 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8877 std::optional<DisplayViewport> optionalDisplayViewport =
8878 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8879 ASSERT_TRUE(optionalDisplayViewport.has_value());
8880 DisplayViewport displayViewport = *optionalDisplayViewport;
8881
8882 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8883 prepareAxes(POSITION);
8884 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8885
8886 // Finger down
8887 int32_t x = 100, y = 100;
8888 processPosition(mapper, x, y);
8889 processSync(mapper);
8890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8891 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8892
8893 // Deactivate display viewport
8894 displayViewport.isActive = false;
8895 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8896 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8897
8898 // The ongoing touch should not be canceled
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8900
8901 // Finger move is not ignored
8902 x += 10, y += 10;
8903 processPosition(mapper, x, y);
8904 processSync(mapper);
8905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8906 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8907
8908 // Reactivate display viewport
8909 displayViewport.isActive = true;
8910 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8911 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8912
8913 // Finger move continues and does not start new gesture
8914 x += 10, y += 10;
8915 processPosition(mapper, x, y);
8916 processSync(mapper);
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8918 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8919}
8920
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008921TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008922 prepareAxes(POSITION);
8923 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008924 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008925 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008926
8927 NotifyMotionArgs motionArgs;
8928 // Unrotated video frame
8929 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8930 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008931 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008932 processPosition(mapper, 100, 200);
8933 processSync(mapper);
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8935 ASSERT_EQ(frames, motionArgs.videoFrames);
8936
8937 // Subsequent touch events should not have any videoframes
8938 // This is implemented separately in FakeEventHub,
8939 // but that should match the behaviour of TouchVideoDevice.
8940 processPosition(mapper, 200, 200);
8941 processSync(mapper);
8942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8943 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8944}
8945
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008946TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008947 prepareAxes(POSITION);
8948 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008949 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008950 // Unrotated video frame
8951 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8952 NotifyMotionArgs motionArgs;
8953
8954 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008955 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008956 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008957 clearViewports();
8958 prepareDisplay(orientation);
8959 std::vector<TouchVideoFrame> frames{frame};
8960 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8961 processPosition(mapper, 100, 200);
8962 processSync(mapper);
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(frames, motionArgs.videoFrames);
8965 }
8966}
8967
8968TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8969 prepareAxes(POSITION);
8970 addConfigurationProperty("touch.deviceType", "touchScreen");
8971 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8972 // orientation-aware are affected by display rotation.
8973 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008974 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008975 // Unrotated video frame
8976 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8977 NotifyMotionArgs motionArgs;
8978
8979 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008980 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008981 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008982 clearViewports();
8983 prepareDisplay(orientation);
8984 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008985 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008986 processPosition(mapper, 100, 200);
8987 processSync(mapper);
8988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008989 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8990 // compared to the display. This is so that when the window transform (which contains the
8991 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8992 // window's coordinate space.
8993 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008994 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008995
8996 // Release finger.
8997 processSync(mapper);
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008999 }
9000}
9001
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009002TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009003 prepareAxes(POSITION);
9004 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009005 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009006 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9007 // so mix these.
9008 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9009 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9010 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9011 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9012 NotifyMotionArgs motionArgs;
9013
Michael Wrighta9cf4192022-12-01 23:46:39 +00009014 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009015 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009016 processPosition(mapper, 100, 200);
9017 processSync(mapper);
9018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009019 ASSERT_EQ(frames, motionArgs.videoFrames);
9020}
9021
9022TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9023 prepareAxes(POSITION);
9024 addConfigurationProperty("touch.deviceType", "touchScreen");
9025 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9026 // orientation-aware are affected by display rotation.
9027 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009028 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009029 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9030 // so mix these.
9031 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9032 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9033 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9034 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9035 NotifyMotionArgs motionArgs;
9036
Michael Wrighta9cf4192022-12-01 23:46:39 +00009037 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009038 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9039 processPosition(mapper, 100, 200);
9040 processSync(mapper);
9041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9042 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9043 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9044 // compared to the display. This is so that when the window transform (which contains the
9045 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9046 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009047 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009048 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009049 ASSERT_EQ(frames, motionArgs.videoFrames);
9050}
9051
Arthur Hung9da14732019-09-02 16:16:58 +08009052/**
9053 * If we had defined port associations, but the viewport is not ready, the touch device would be
9054 * expected to be disabled, and it should be enabled after the viewport has found.
9055 */
9056TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009057 constexpr uint8_t hdmi2 = 1;
9058 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009059 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009060
9061 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9062
9063 addConfigurationProperty("touch.deviceType", "touchScreen");
9064 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009065 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009066
9067 ASSERT_EQ(mDevice->isEnabled(), false);
9068
9069 // Add display on hdmi2, the device should be enabled and can receive touch event.
9070 prepareSecondaryDisplay(type, hdmi2);
9071 ASSERT_EQ(mDevice->isEnabled(), true);
9072
9073 // Send a touch event.
9074 processPosition(mapper, 100, 100);
9075 processSync(mapper);
9076
9077 NotifyMotionArgs args;
9078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9079 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9080}
9081
Arthur Hung421eb1c2020-01-16 00:09:42 +08009082TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009083 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009084 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009085 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009086 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009087
9088 NotifyMotionArgs motionArgs;
9089
9090 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9091 // finger down
9092 processId(mapper, 1);
9093 processPosition(mapper, x1, y1);
9094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9096 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009097 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009098
9099 // finger move
9100 processId(mapper, 1);
9101 processPosition(mapper, x2, y2);
9102 processSync(mapper);
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009105 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009106
9107 // finger up.
9108 processId(mapper, -1);
9109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9111 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009112 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009113
9114 // new finger down
9115 processId(mapper, 1);
9116 processPosition(mapper, x3, y3);
9117 processSync(mapper);
9118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9119 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009120 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009121}
9122
9123/**
arthurhungcc7f9802020-04-30 17:55:40 +08009124 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9125 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009126 */
arthurhungcc7f9802020-04-30 17:55:40 +08009127TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009128 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009129 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009130 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009131 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009132
9133 NotifyMotionArgs motionArgs;
9134
9135 // default tool type is finger
9136 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009137 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009138 processPosition(mapper, x1, y1);
9139 processSync(mapper);
9140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9141 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009142 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009143
9144 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9145 processToolType(mapper, MT_TOOL_PALM);
9146 processSync(mapper);
9147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9148 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9149
9150 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009151 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009152 processPosition(mapper, x2, y2);
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9155
9156 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009157 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009158 processSync(mapper);
9159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9160
9161 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009162 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009163 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009164 processPosition(mapper, x3, y3);
9165 processSync(mapper);
9166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009168 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009169}
9170
arthurhungbf89a482020-04-17 17:37:55 +08009171/**
arthurhungcc7f9802020-04-30 17:55:40 +08009172 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9173 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009174 */
arthurhungcc7f9802020-04-30 17:55:40 +08009175TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009176 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009177 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009178 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009179 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009180
9181 NotifyMotionArgs motionArgs;
9182
9183 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009184 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9185 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009186 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);
arthurhungbf89a482020-04-17 17:37:55 +08009191
9192 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009193 processSlot(mapper, SECOND_SLOT);
9194 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009195 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009196 processSync(mapper);
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009198 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009199 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009200
9201 // If the tool type of the first finger changes to MT_TOOL_PALM,
9202 // we expect to receive ACTION_POINTER_UP with cancel flag.
9203 processSlot(mapper, FIRST_SLOT);
9204 processId(mapper, FIRST_TRACKING_ID);
9205 processToolType(mapper, MT_TOOL_PALM);
9206 processSync(mapper);
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009208 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009209 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9210
9211 // The following MOVE events of second finger should be processed.
9212 processSlot(mapper, SECOND_SLOT);
9213 processId(mapper, SECOND_TRACKING_ID);
9214 processPosition(mapper, x2 + 1, y2 + 1);
9215 processSync(mapper);
9216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009218 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009219
9220 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9221 // it. Second finger receive move.
9222 processSlot(mapper, FIRST_SLOT);
9223 processId(mapper, INVALID_TRACKING_ID);
9224 processSync(mapper);
9225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009227 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009228
9229 // Second finger keeps moving.
9230 processSlot(mapper, SECOND_SLOT);
9231 processId(mapper, SECOND_TRACKING_ID);
9232 processPosition(mapper, x2 + 2, y2 + 2);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009236 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009237
9238 // Second finger up.
9239 processId(mapper, INVALID_TRACKING_ID);
9240 processSync(mapper);
9241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9242 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9243 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9244}
9245
9246/**
9247 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9248 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9249 */
9250TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9251 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009252 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009253 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009254 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009255
9256 NotifyMotionArgs motionArgs;
9257
9258 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9259 // First finger down.
9260 processId(mapper, FIRST_TRACKING_ID);
9261 processPosition(mapper, x1, y1);
9262 processSync(mapper);
9263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9264 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009265 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009266
9267 // Second finger down.
9268 processSlot(mapper, SECOND_SLOT);
9269 processId(mapper, SECOND_TRACKING_ID);
9270 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009271 processSync(mapper);
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009273 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009274 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009275
arthurhungcc7f9802020-04-30 17:55:40 +08009276 // If the tool type of the first finger changes to MT_TOOL_PALM,
9277 // we expect to receive ACTION_POINTER_UP with cancel flag.
9278 processSlot(mapper, FIRST_SLOT);
9279 processId(mapper, FIRST_TRACKING_ID);
9280 processToolType(mapper, MT_TOOL_PALM);
9281 processSync(mapper);
9282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009283 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009284 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9285
9286 // Second finger keeps moving.
9287 processSlot(mapper, SECOND_SLOT);
9288 processId(mapper, SECOND_TRACKING_ID);
9289 processPosition(mapper, x2 + 1, y2 + 1);
9290 processSync(mapper);
9291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9292 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9293
9294 // second finger becomes palm, receive cancel due to only 1 finger is active.
9295 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009296 processToolType(mapper, MT_TOOL_PALM);
9297 processSync(mapper);
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9299 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9300
arthurhungcc7f9802020-04-30 17:55:40 +08009301 // third finger down.
9302 processSlot(mapper, THIRD_SLOT);
9303 processId(mapper, THIRD_TRACKING_ID);
9304 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009305 processPosition(mapper, x3, y3);
9306 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9308 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009309 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009310 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009311
9312 // third finger move
9313 processId(mapper, THIRD_TRACKING_ID);
9314 processPosition(mapper, x3 + 1, y3 + 1);
9315 processSync(mapper);
9316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9318
9319 // first finger up, third finger receive move.
9320 processSlot(mapper, FIRST_SLOT);
9321 processId(mapper, INVALID_TRACKING_ID);
9322 processSync(mapper);
9323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009325 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009326
9327 // second finger up, third finger receive move.
9328 processSlot(mapper, SECOND_SLOT);
9329 processId(mapper, INVALID_TRACKING_ID);
9330 processSync(mapper);
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009333 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009334
9335 // third finger up.
9336 processSlot(mapper, THIRD_SLOT);
9337 processId(mapper, INVALID_TRACKING_ID);
9338 processSync(mapper);
9339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9340 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9341 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9342}
9343
9344/**
9345 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9346 * and the active finger could still be allowed to receive the events
9347 */
9348TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9349 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009350 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009351 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009352 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009353
9354 NotifyMotionArgs motionArgs;
9355
9356 // default tool type is finger
9357 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9358 processId(mapper, FIRST_TRACKING_ID);
9359 processPosition(mapper, x1, y1);
9360 processSync(mapper);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9362 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009363 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009364
9365 // Second finger down.
9366 processSlot(mapper, SECOND_SLOT);
9367 processId(mapper, SECOND_TRACKING_ID);
9368 processPosition(mapper, x2, y2);
9369 processSync(mapper);
9370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009371 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009372 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009373
9374 // If the tool type of the second finger changes to MT_TOOL_PALM,
9375 // we expect to receive ACTION_POINTER_UP with cancel flag.
9376 processId(mapper, SECOND_TRACKING_ID);
9377 processToolType(mapper, MT_TOOL_PALM);
9378 processSync(mapper);
9379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009380 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009381 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9382
9383 // The following MOVE event should be processed.
9384 processSlot(mapper, FIRST_SLOT);
9385 processId(mapper, FIRST_TRACKING_ID);
9386 processPosition(mapper, x1 + 1, y1 + 1);
9387 processSync(mapper);
9388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9389 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009390 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009391
9392 // second finger up.
9393 processSlot(mapper, SECOND_SLOT);
9394 processId(mapper, INVALID_TRACKING_ID);
9395 processSync(mapper);
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9398
9399 // first finger keep moving
9400 processSlot(mapper, FIRST_SLOT);
9401 processId(mapper, FIRST_TRACKING_ID);
9402 processPosition(mapper, x1 + 2, y1 + 2);
9403 processSync(mapper);
9404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9405 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9406
9407 // first finger up.
9408 processId(mapper, INVALID_TRACKING_ID);
9409 processSync(mapper);
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9411 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9412 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009413}
9414
Arthur Hung9ad18942021-06-19 02:04:46 +00009415/**
9416 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9417 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9418 * cause slot be valid again.
9419 */
9420TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9421 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009422 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009423 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009424 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009425
9426 NotifyMotionArgs motionArgs;
9427
9428 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9429 // First finger down.
9430 processId(mapper, FIRST_TRACKING_ID);
9431 processPosition(mapper, x1, y1);
9432 processPressure(mapper, RAW_PRESSURE_MAX);
9433 processSync(mapper);
9434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009436 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009437
9438 // First finger move.
9439 processId(mapper, FIRST_TRACKING_ID);
9440 processPosition(mapper, x1 + 1, y1 + 1);
9441 processPressure(mapper, RAW_PRESSURE_MAX);
9442 processSync(mapper);
9443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009445 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009446
9447 // Second finger down.
9448 processSlot(mapper, SECOND_SLOT);
9449 processId(mapper, SECOND_TRACKING_ID);
9450 processPosition(mapper, x2, y2);
9451 processPressure(mapper, RAW_PRESSURE_MAX);
9452 processSync(mapper);
9453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009454 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009455 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009456
9457 // second finger up with some unexpected data.
9458 processSlot(mapper, SECOND_SLOT);
9459 processId(mapper, INVALID_TRACKING_ID);
9460 processPosition(mapper, x2, y2);
9461 processSync(mapper);
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009463 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009464 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009465
9466 // first finger up with some unexpected data.
9467 processSlot(mapper, FIRST_SLOT);
9468 processId(mapper, INVALID_TRACKING_ID);
9469 processPosition(mapper, x2, y2);
9470 processPressure(mapper, RAW_PRESSURE_MAX);
9471 processSync(mapper);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9473 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009474 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009475}
9476
Arpit Singh4b4a4572023-11-24 18:19:56 +00009477TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009478 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009479 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009480 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009481 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009482
9483 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009484 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009485 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009486 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009487 processPressure(mapper, RAW_PRESSURE_MAX);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9490 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9491
9492 // Second finger down.
9493 processSlot(mapper, SECOND_SLOT);
9494 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009495 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009496 processPressure(mapper, RAW_PRESSURE_MAX);
9497 processSync(mapper);
9498 ASSERT_NO_FATAL_FAILURE(
9499 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9500
Arpit Singh4b4a4572023-11-24 18:19:56 +00009501 // Set MT Slot state to be repopulated for the required slots
9502 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9503 mtSlotValues[0] = FIRST_TRACKING_ID;
9504 mtSlotValues[1] = SECOND_TRACKING_ID;
9505 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9506
9507 mtSlotValues[0] = x1;
9508 mtSlotValues[1] = x2;
9509 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9510
9511 mtSlotValues[0] = y1;
9512 mtSlotValues[1] = y2;
9513 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9514
9515 mtSlotValues[0] = RAW_PRESSURE_MAX;
9516 mtSlotValues[1] = RAW_PRESSURE_MAX;
9517 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9518
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009519 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009520 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009521 resetMapper(mapper, ARBITRARY_TIME);
9522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9523 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009524
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009525 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9526 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009527 processPosition(mapper, 301, 302);
9528 processSync(mapper);
9529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9530 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9532 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009533
9534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9535}
9536
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009537TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009538 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009539 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009540 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009541 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009542
9543 // First finger touches down and releases.
9544 processId(mapper, FIRST_TRACKING_ID);
9545 processPosition(mapper, 100, 200);
9546 processPressure(mapper, RAW_PRESSURE_MAX);
9547 processSync(mapper);
9548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9549 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9550 processId(mapper, INVALID_TRACKING_ID);
9551 processSync(mapper);
9552 ASSERT_NO_FATAL_FAILURE(
9553 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9554
9555 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9556 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009557 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9559
9560 // Send an empty sync frame. Since there are no pointers, no events are generated.
9561 processSync(mapper);
9562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9563}
9564
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009565TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009566 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009567 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009568 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009569 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009571
9572 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9573 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9574 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9575 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9576 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9577
9578 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009579 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009580 processId(mapper, FIRST_TRACKING_ID);
9581 processToolType(mapper, MT_TOOL_PEN);
9582 processPosition(mapper, 100, 200);
9583 processPressure(mapper, RAW_PRESSURE_MAX);
9584 processSync(mapper);
9585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9586 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9587 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009588 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009589
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009590 // Now that we know the device supports styluses, ensure that the device is re-configured with
9591 // the stylus source.
9592 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9593 {
9594 const auto& devices = mReader->getInputDevices();
9595 auto deviceInfo =
9596 std::find_if(devices.begin(), devices.end(),
9597 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9598 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9599 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9600 }
9601
9602 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9604
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009605 processId(mapper, INVALID_TRACKING_ID);
9606 processSync(mapper);
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9608 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9609 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009610 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009611}
9612
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009613// --- MultiTouchInputMapperTest_ExternalDevice ---
9614
9615class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9616protected:
Chris Yea52ade12020-08-27 16:49:20 -07009617 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009618};
9619
9620/**
9621 * Expect fallback to internal viewport if device is external and external viewport is not present.
9622 */
9623TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9624 prepareAxes(POSITION);
9625 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009626 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009627 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009628
9629 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9630
9631 NotifyMotionArgs motionArgs;
9632
9633 // Expect the event to be sent to the internal viewport,
9634 // because an external viewport is not present.
9635 processPosition(mapper, 100, 100);
9636 processSync(mapper);
9637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009638 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, motionArgs.displayId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009639
9640 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009641 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009642 processPosition(mapper, 100, 100);
9643 processSync(mapper);
9644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9645 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9646}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009647
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00009648// TODO(b/281840344): Remove the test when the old touchpad stack is removed. It is currently
9649// unclear what the behavior of the touchpad logic in TouchInputMapper should do after the
9650// PointerChoreographer refactor.
9651TEST_F(MultiTouchInputMapperTest, DISABLED_Process_TouchpadPointer) {
Harry Cutts8722be92024-04-05 14:46:05 +00009652 // prepare device
Michael Wrighta9cf4192022-12-01 23:46:39 +00009653 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009654 prepareAxes(POSITION | ID | SLOT);
9655 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9656 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009657 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009658 // run uncaptured pointer tests - pushes out generic events
9659 // FINGER 0 DOWN
9660 processId(mapper, 3);
9661 processPosition(mapper, 100, 100);
9662 processKey(mapper, BTN_TOUCH, 1);
9663 processSync(mapper);
9664
9665 // start at (100,100), cursor should be at (0,0) * scale
9666 NotifyMotionArgs args;
9667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9668 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9669 ASSERT_NO_FATAL_FAILURE(
9670 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9671
9672 // FINGER 0 MOVE
9673 processPosition(mapper, 200, 200);
9674 processSync(mapper);
9675
9676 // compute scaling to help with touch position checking
9677 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9678 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9679 float scale =
9680 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9681
9682 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9684 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9686 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009687
9688 // BUTTON DOWN
9689 processKey(mapper, BTN_LEFT, 1);
9690 processSync(mapper);
9691
9692 // touchinputmapper design sends a move before button press
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9694 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9696 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9697
9698 // BUTTON UP
9699 processKey(mapper, BTN_LEFT, 0);
9700 processSync(mapper);
9701
9702 // touchinputmapper design sends a move after button release
9703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9704 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9706 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009707}
9708
Harry Cutts8722be92024-04-05 14:46:05 +00009709TEST_F(MultiTouchInputMapperTest, Touchpad_GetSources) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00009710 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009711 prepareAxes(POSITION | ID | SLOT);
9712 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Hiroki Sato25040232024-02-22 17:21:22 +09009713 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Arpit Singha8c236b2023-04-25 13:56:05 +00009714 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009715
Josep del Río2d8c79a2023-01-23 19:33:50 +00009716 // uncaptured touchpad should be a pointer device
9717 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009718}
9719
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009720// --- BluetoothMultiTouchInputMapperTest ---
9721
9722class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9723protected:
9724 void SetUp() override {
9725 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9726 }
9727};
9728
9729TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9730 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009731 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009732 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009733 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009734
9735 nsecs_t kernelEventTime = ARBITRARY_TIME;
9736 nsecs_t expectedEventTime = ARBITRARY_TIME;
9737 // Touch down.
9738 processId(mapper, FIRST_TRACKING_ID);
9739 processPosition(mapper, 100, 200);
9740 processPressure(mapper, RAW_PRESSURE_MAX);
9741 processSync(mapper, ARBITRARY_TIME);
9742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9743 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9744
9745 // Process several events that come in quick succession, according to their timestamps.
9746 for (int i = 0; i < 3; i++) {
9747 constexpr static nsecs_t delta = ms2ns(1);
9748 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9749 kernelEventTime += delta;
9750 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9751
9752 processPosition(mapper, 101 + i, 201 + i);
9753 processSync(mapper, kernelEventTime);
9754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9756 WithEventTime(expectedEventTime))));
9757 }
9758
9759 // Release the touch.
9760 processId(mapper, INVALID_TRACKING_ID);
9761 processPressure(mapper, RAW_PRESSURE_MIN);
9762 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9764 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9765 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9766}
9767
9768// --- MultiTouchPointerModeTest ---
9769
HQ Liue6983c72022-04-19 22:14:56 +00009770class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9771protected:
9772 float mPointerMovementScale;
9773 float mPointerXZoomScale;
9774 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9775 addConfigurationProperty("touch.deviceType", "pointer");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009776 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009777
9778 prepareAxes(POSITION);
9779 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9780 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9781 // needs to be disabled, and the pointer gesture needs to be enabled.
Hiroki Sato25040232024-02-22 17:21:22 +09009782 mFakePolicy->setPointerCapture(/*window=*/nullptr);
HQ Liue6983c72022-04-19 22:14:56 +00009783 mFakePolicy->setPointerGestureEnabled(true);
HQ Liue6983c72022-04-19 22:14:56 +00009784
9785 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9786 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9787 mPointerMovementScale =
9788 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9789 mPointerXZoomScale =
9790 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9791 }
9792
9793 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9794 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9795 /*flat*/ 0,
9796 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9797 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9798 /*flat*/ 0,
9799 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9800 }
9801};
9802
9803/**
9804 * Two fingers down on a pointer mode touch pad. The width
9805 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9806 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9807 * be greater than the both value to be freeform gesture, so that after two
9808 * fingers start to move downwards, the gesture should be swipe.
9809 */
9810TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9811 // The min freeform gesture width is 25units/mm x 30mm = 750
9812 // which is greater than fraction of the diagnal length of the touchpad (349).
9813 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +00009814 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009815 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009816 NotifyMotionArgs motionArgs;
9817
9818 // Two fingers down at once.
9819 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9820 // Pointer's initial position is used the [0,0] coordinate.
9821 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9822
9823 processId(mapper, FIRST_TRACKING_ID);
9824 processPosition(mapper, x1, y1);
9825 processMTSync(mapper);
9826 processId(mapper, SECOND_TRACKING_ID);
9827 processPosition(mapper, x2, y2);
9828 processMTSync(mapper);
9829 processSync(mapper);
9830
9831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009832 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009833 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009834 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009835 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009836 ASSERT_NO_FATAL_FAILURE(
9837 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9838
9839 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9840 // that there should be 1 pointer.
9841 int32_t movingDistance = 200;
9842 y1 += movingDistance;
9843 y2 += movingDistance;
9844
9845 processId(mapper, FIRST_TRACKING_ID);
9846 processPosition(mapper, x1, y1);
9847 processMTSync(mapper);
9848 processId(mapper, SECOND_TRACKING_ID);
9849 processPosition(mapper, x2, y2);
9850 processMTSync(mapper);
9851 processSync(mapper);
9852
9853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009854 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009855 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009856 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009857 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9859 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9860 0, 0, 0, 0));
9861}
9862
9863/**
9864 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9865 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9866 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9867 * value to be freeform gesture, so that after two fingers start to move downwards,
9868 * the gesture should be swipe.
9869 */
9870TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9871 // The min freeform gesture width is 5units/mm x 30mm = 150
9872 // which is greater than fraction of the diagnal length of the touchpad (349).
9873 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +00009874 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +00009875 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009876 NotifyMotionArgs motionArgs;
9877
9878 // Two fingers down at once.
9879 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9880 // Pointer's initial position is used the [0,0] coordinate.
9881 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9882
9883 processId(mapper, FIRST_TRACKING_ID);
9884 processPosition(mapper, x1, y1);
9885 processMTSync(mapper);
9886 processId(mapper, SECOND_TRACKING_ID);
9887 processPosition(mapper, x2, y2);
9888 processMTSync(mapper);
9889 processSync(mapper);
9890
9891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009892 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009893 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009894 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009895 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009896 ASSERT_NO_FATAL_FAILURE(
9897 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9898
9899 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9900 // and there should be 1 pointer.
9901 int32_t movingDistance = 200;
9902 y1 += movingDistance;
9903 y2 += movingDistance;
9904
9905 processId(mapper, FIRST_TRACKING_ID);
9906 processPosition(mapper, x1, y1);
9907 processMTSync(mapper);
9908 processId(mapper, SECOND_TRACKING_ID);
9909 processPosition(mapper, x2, y2);
9910 processMTSync(mapper);
9911 processSync(mapper);
9912
9913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009914 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009915 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009916 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009917 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009918 // New coordinate is the scaled relative coordinate from the initial coordinate.
9919 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9920 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9921 0, 0, 0, 0));
9922}
9923
9924/**
9925 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9926 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9927 * freeform gestures after two fingers start to move downwards.
9928 */
9929TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +00009930 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009931 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009932
9933 NotifyMotionArgs motionArgs;
9934
9935 // Two fingers down at once. Wider than the max swipe width.
9936 // The gesture is expected to be PRESS, then transformed to FREEFORM
9937 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9938
9939 processId(mapper, FIRST_TRACKING_ID);
9940 processPosition(mapper, x1, y1);
9941 processMTSync(mapper);
9942 processId(mapper, SECOND_TRACKING_ID);
9943 processPosition(mapper, x2, y2);
9944 processMTSync(mapper);
9945 processSync(mapper);
9946
9947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009948 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009949 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009950 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009951 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009952 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9953 ASSERT_NO_FATAL_FAILURE(
9954 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9955
9956 int32_t movingDistance = 200;
9957
9958 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9959 // then two down events for two pointers.
9960 y1 += movingDistance;
9961 y2 += movingDistance;
9962
9963 processId(mapper, FIRST_TRACKING_ID);
9964 processPosition(mapper, x1, y1);
9965 processMTSync(mapper);
9966 processId(mapper, SECOND_TRACKING_ID);
9967 processPosition(mapper, x2, y2);
9968 processMTSync(mapper);
9969 processSync(mapper);
9970
9971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9972 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009973 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009974 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009976 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009977 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009978 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009980 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009981 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009982 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009983 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009984 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009985 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009986 // Two pointers' scaled relative coordinates from their initial centroid.
9987 // Initial y coordinates are 0 as y1 and y2 have the same value.
9988 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9989 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9990 // When pointers move, the new coordinates equal to the initial coordinates plus
9991 // scaled moving distance.
9992 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9993 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9994 0, 0, 0, 0));
9995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9996 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9997 0, 0, 0, 0));
9998
9999 // Move two fingers down again, expect one MOVE motion event.
10000 y1 += movingDistance;
10001 y2 += movingDistance;
10002
10003 processId(mapper, FIRST_TRACKING_ID);
10004 processPosition(mapper, x1, y1);
10005 processMTSync(mapper);
10006 processId(mapper, SECOND_TRACKING_ID);
10007 processPosition(mapper, x2, y2);
10008 processMTSync(mapper);
10009 processSync(mapper);
10010
10011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010012 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010014 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010015 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10017 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10018 0, 0, 0, 0, 0));
10019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10020 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10021 0, 0, 0, 0, 0));
10022}
10023
Harry Cutts39b7ca22022-10-05 15:55:48 +000010024TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010025 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010026 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010027 NotifyMotionArgs motionArgs;
10028
10029 // Place two fingers down.
10030 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10031
10032 processId(mapper, FIRST_TRACKING_ID);
10033 processPosition(mapper, x1, y1);
10034 processMTSync(mapper);
10035 processId(mapper, SECOND_TRACKING_ID);
10036 processPosition(mapper, x2, y2);
10037 processMTSync(mapper);
10038 processSync(mapper);
10039
10040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010041 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010042 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10043 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10044 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10045 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10046
10047 // Move the two fingers down and to the left.
10048 int32_t movingDistance = 200;
10049 x1 -= movingDistance;
10050 y1 += movingDistance;
10051 x2 -= movingDistance;
10052 y2 += movingDistance;
10053
10054 processId(mapper, FIRST_TRACKING_ID);
10055 processPosition(mapper, x1, y1);
10056 processMTSync(mapper);
10057 processId(mapper, SECOND_TRACKING_ID);
10058 processPosition(mapper, x2, y2);
10059 processMTSync(mapper);
10060 processSync(mapper);
10061
10062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010063 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10065 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10066 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10067 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10068}
10069
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010070TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010071 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010072 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010073 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10075
10076 // Start a stylus gesture.
10077 processKey(mapper, BTN_TOOL_PEN, 1);
10078 processId(mapper, FIRST_TRACKING_ID);
10079 processPosition(mapper, 100, 200);
10080 processSync(mapper);
10081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10082 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10083 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010084 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010085 // TODO(b/257078296): Pointer mode generates extra event.
10086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10088 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010089 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10091
10092 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10093 // gesture should be disabled.
10094 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10095 viewport->isActive = false;
10096 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010097 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10099 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10100 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010101 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010102 // TODO(b/257078296): Pointer mode generates extra event.
10103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10104 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10105 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010106 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10108}
10109
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010110// --- JoystickInputMapperTest ---
10111
10112class JoystickInputMapperTest : public InputMapperTest {
10113protected:
10114 static const int32_t RAW_X_MIN;
10115 static const int32_t RAW_X_MAX;
10116 static const int32_t RAW_Y_MIN;
10117 static const int32_t RAW_Y_MAX;
10118
10119 void SetUp() override {
10120 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10121 }
10122 void prepareAxes() {
10123 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10124 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10125 }
10126
10127 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10129 }
10130
10131 void processSync(JoystickInputMapper& mapper) {
10132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10133 }
10134
Michael Wrighta9cf4192022-12-01 23:46:39 +000010135 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010136 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10137 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10138 NO_PORT, ViewportType::VIRTUAL);
10139 }
10140};
10141
10142const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10143const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10144const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10145const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10146
10147TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10148 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010149 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010150
10151 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10152
Michael Wrighta9cf4192022-12-01 23:46:39 +000010153 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010154
10155 // Send an axis event
10156 processAxis(mapper, ABS_X, 100);
10157 processSync(mapper);
10158
10159 NotifyMotionArgs args;
10160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10161 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10162
10163 // Send another axis event
10164 processAxis(mapper, ABS_Y, 100);
10165 processSync(mapper);
10166
10167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10168 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10169}
10170
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010171// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010172
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010173class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010174protected:
10175 static const char* DEVICE_NAME;
10176 static const char* DEVICE_LOCATION;
10177 static const int32_t DEVICE_ID;
10178 static const int32_t DEVICE_GENERATION;
10179 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010180 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010181 static const int32_t EVENTHUB_ID;
10182
10183 std::shared_ptr<FakeEventHub> mFakeEventHub;
10184 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010185 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010186 std::unique_ptr<InstrumentedInputReader> mReader;
10187 std::shared_ptr<InputDevice> mDevice;
10188
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010189 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010190 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010191 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010192 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010193 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010194 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010195 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10196 }
10197
10198 void SetUp() override { SetUp(DEVICE_CLASSES); }
10199
10200 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010201 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010202 mFakePolicy.clear();
10203 }
10204
Chris Yee2b1e5c2021-03-10 22:45:12 -080010205 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10206 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010207 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010208 InputDeviceIdentifier identifier;
10209 identifier.name = name;
10210 identifier.location = location;
10211 std::shared_ptr<InputDevice> device =
10212 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10213 identifier);
10214 mReader->pushNextDevice(device);
10215 mFakeEventHub->addDevice(eventHubId, name, classes);
10216 mReader->loopOnce();
10217 return device;
10218 }
10219
10220 template <class T, typename... Args>
10221 T& addControllerAndConfigure(Args... args) {
10222 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10223
10224 return controller;
10225 }
10226};
10227
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010228const char* PeripheralControllerTest::DEVICE_NAME = "device";
10229const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10230const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10231const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10232const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010233const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10234 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010235const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010236
10237// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010238class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010239protected:
10240 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010241 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010242 }
10243};
10244
10245TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010246 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010247
Harry Cuttsa5b71292022-11-28 12:56:17 +000010248 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10249 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10250 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010251}
10252
10253TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010254 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010255
Harry Cuttsa5b71292022-11-28 12:56:17 +000010256 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10257 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10258 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010259}
10260
10261// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010262class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010263protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010264 void SetUp() override {
10265 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10266 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010267};
10268
Chris Ye85758332021-05-16 23:05:17 -070010269TEST_F(LightControllerTest, MonoLight) {
10270 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010271 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010272 .maxBrightness = 255,
10273 .flags = InputLightClass::BRIGHTNESS,
10274 .path = ""};
10275 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010276
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010277 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010278 InputDeviceInfo info;
10279 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010280 std::vector<InputDeviceLightInfo> lights = info.getLights();
10281 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010282 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10283 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10284
10285 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10286 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10287}
10288
DingYong99f2c3c2023-12-20 15:46:06 +080010289TEST_F(LightControllerTest, MonoKeyboardMuteLight) {
10290 RawLightInfo infoMono = {.id = 1,
10291 .name = "mono_keyboard_mute",
10292 .maxBrightness = 255,
10293 .flags = InputLightClass::BRIGHTNESS |
10294 InputLightClass::KEYBOARD_MIC_MUTE,
10295 .path = ""};
10296 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10297
10298 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10299 std::list<NotifyArgs> unused =
10300 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10301 /*changes=*/{});
10302
10303 InputDeviceInfo info;
10304 controller.populateDeviceInfo(&info);
10305 std::vector<InputDeviceLightInfo> lights = info.getLights();
10306 ASSERT_EQ(1U, lights.size());
10307 ASSERT_EQ(InputDeviceLightType::KEYBOARD_MIC_MUTE, lights[0].type);
10308 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10309}
10310
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010311TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10312 RawLightInfo infoMono = {.id = 1,
10313 .name = "mono_keyboard_backlight",
10314 .maxBrightness = 255,
10315 .flags = InputLightClass::BRIGHTNESS |
10316 InputLightClass::KEYBOARD_BACKLIGHT,
10317 .path = ""};
10318 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10319
10320 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10321 InputDeviceInfo info;
10322 controller.populateDeviceInfo(&info);
10323 std::vector<InputDeviceLightInfo> lights = info.getLights();
10324 ASSERT_EQ(1U, lights.size());
10325 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10326 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010327
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010328 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10329 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010330}
10331
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010332TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10333 RawLightInfo infoMono = {.id = 1,
10334 .name = "mono_light",
10335 .maxBrightness = 255,
10336 .flags = InputLightClass::BRIGHTNESS,
10337 .path = ""};
10338 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10339 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10340 "0,100,200");
10341
10342 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10343 std::list<NotifyArgs> unused =
10344 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10345 /*changes=*/{});
10346
10347 InputDeviceInfo info;
10348 controller.populateDeviceInfo(&info);
10349 std::vector<InputDeviceLightInfo> lights = info.getLights();
10350 ASSERT_EQ(1U, lights.size());
10351 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10352}
10353
10354TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10355 RawLightInfo infoMono = {.id = 1,
10356 .name = "mono_keyboard_backlight",
10357 .maxBrightness = 255,
10358 .flags = InputLightClass::BRIGHTNESS |
10359 InputLightClass::KEYBOARD_BACKLIGHT,
10360 .path = ""};
10361 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10362
10363 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10364 std::list<NotifyArgs> unused =
10365 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10366 /*changes=*/{});
10367
10368 InputDeviceInfo info;
10369 controller.populateDeviceInfo(&info);
10370 std::vector<InputDeviceLightInfo> lights = info.getLights();
10371 ASSERT_EQ(1U, lights.size());
10372 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10373}
10374
10375TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10376 RawLightInfo infoMono = {.id = 1,
10377 .name = "mono_keyboard_backlight",
10378 .maxBrightness = 255,
10379 .flags = InputLightClass::BRIGHTNESS |
10380 InputLightClass::KEYBOARD_BACKLIGHT,
10381 .path = ""};
10382 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10383 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10384 "0,100,200");
10385
10386 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10387 std::list<NotifyArgs> unused =
10388 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10389 /*changes=*/{});
10390
10391 InputDeviceInfo info;
10392 controller.populateDeviceInfo(&info);
10393 std::vector<InputDeviceLightInfo> lights = info.getLights();
10394 ASSERT_EQ(1U, lights.size());
10395 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10396 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10397 ASSERT_EQ(BrightnessLevel(0), *it);
10398 std::advance(it, 1);
10399 ASSERT_EQ(BrightnessLevel(100), *it);
10400 std::advance(it, 1);
10401 ASSERT_EQ(BrightnessLevel(200), *it);
10402}
10403
10404TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10405 RawLightInfo infoMono = {.id = 1,
10406 .name = "mono_keyboard_backlight",
10407 .maxBrightness = 255,
10408 .flags = InputLightClass::BRIGHTNESS |
10409 InputLightClass::KEYBOARD_BACKLIGHT,
10410 .path = ""};
10411 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10412 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10413 "0,100,200,300,400,500");
10414
10415 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10416 std::list<NotifyArgs> unused =
10417 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10418 /*changes=*/{});
10419
10420 InputDeviceInfo info;
10421 controller.populateDeviceInfo(&info);
10422 std::vector<InputDeviceLightInfo> lights = info.getLights();
10423 ASSERT_EQ(1U, lights.size());
10424 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10425}
10426
Chris Yee2b1e5c2021-03-10 22:45:12 -080010427TEST_F(LightControllerTest, RGBLight) {
10428 RawLightInfo infoRed = {.id = 1,
10429 .name = "red",
10430 .maxBrightness = 255,
10431 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10432 .path = ""};
10433 RawLightInfo infoGreen = {.id = 2,
10434 .name = "green",
10435 .maxBrightness = 255,
10436 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10437 .path = ""};
10438 RawLightInfo infoBlue = {.id = 3,
10439 .name = "blue",
10440 .maxBrightness = 255,
10441 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10442 .path = ""};
10443 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10444 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10445 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10446
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010447 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010448 InputDeviceInfo info;
10449 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010450 std::vector<InputDeviceLightInfo> lights = info.getLights();
10451 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010452 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10453 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10454 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10455
10456 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10457 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10458}
10459
10460TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10461 RawLightInfo infoRed = {.id = 1,
10462 .name = "red_keyboard_backlight",
10463 .maxBrightness = 255,
10464 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10465 InputLightClass::KEYBOARD_BACKLIGHT,
10466 .path = ""};
10467 RawLightInfo infoGreen = {.id = 2,
10468 .name = "green_keyboard_backlight",
10469 .maxBrightness = 255,
10470 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10471 InputLightClass::KEYBOARD_BACKLIGHT,
10472 .path = ""};
10473 RawLightInfo infoBlue = {.id = 3,
10474 .name = "blue_keyboard_backlight",
10475 .maxBrightness = 255,
10476 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10477 InputLightClass::KEYBOARD_BACKLIGHT,
10478 .path = ""};
10479 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10480 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10481 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10482
10483 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10484 InputDeviceInfo info;
10485 controller.populateDeviceInfo(&info);
10486 std::vector<InputDeviceLightInfo> lights = info.getLights();
10487 ASSERT_EQ(1U, lights.size());
10488 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10489 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10490 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10491
10492 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10493 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10494}
10495
10496TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10497 RawLightInfo infoRed = {.id = 1,
10498 .name = "red",
10499 .maxBrightness = 255,
10500 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10501 .path = ""};
10502 RawLightInfo infoGreen = {.id = 2,
10503 .name = "green",
10504 .maxBrightness = 255,
10505 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10506 .path = ""};
10507 RawLightInfo infoBlue = {.id = 3,
10508 .name = "blue",
10509 .maxBrightness = 255,
10510 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10511 .path = ""};
10512 RawLightInfo infoGlobal = {.id = 3,
10513 .name = "global_keyboard_backlight",
10514 .maxBrightness = 255,
10515 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10516 InputLightClass::KEYBOARD_BACKLIGHT,
10517 .path = ""};
10518 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10519 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10520 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10521 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10522
10523 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10524 InputDeviceInfo info;
10525 controller.populateDeviceInfo(&info);
10526 std::vector<InputDeviceLightInfo> lights = info.getLights();
10527 ASSERT_EQ(1U, lights.size());
10528 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10529 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10530 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010531
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010532 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10533 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010534}
10535
10536TEST_F(LightControllerTest, MultiColorRGBLight) {
10537 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010538 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010539 .maxBrightness = 255,
10540 .flags = InputLightClass::BRIGHTNESS |
10541 InputLightClass::MULTI_INTENSITY |
10542 InputLightClass::MULTI_INDEX,
10543 .path = ""};
10544
10545 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10546
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010547 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010548 InputDeviceInfo info;
10549 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010550 std::vector<InputDeviceLightInfo> lights = info.getLights();
10551 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010552 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10553 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10554 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10555
10556 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10557 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10558}
10559
10560TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10561 RawLightInfo infoColor = {.id = 1,
10562 .name = "multi_color_keyboard_backlight",
10563 .maxBrightness = 255,
10564 .flags = InputLightClass::BRIGHTNESS |
10565 InputLightClass::MULTI_INTENSITY |
10566 InputLightClass::MULTI_INDEX |
10567 InputLightClass::KEYBOARD_BACKLIGHT,
10568 .path = ""};
10569
10570 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10571
10572 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10573 InputDeviceInfo info;
10574 controller.populateDeviceInfo(&info);
10575 std::vector<InputDeviceLightInfo> lights = info.getLights();
10576 ASSERT_EQ(1U, lights.size());
10577 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10578 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10579 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010580
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010581 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10582 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010583}
10584
10585TEST_F(LightControllerTest, PlayerIdLight) {
10586 RawLightInfo info1 = {.id = 1,
10587 .name = "player1",
10588 .maxBrightness = 255,
10589 .flags = InputLightClass::BRIGHTNESS,
10590 .path = ""};
10591 RawLightInfo info2 = {.id = 2,
10592 .name = "player2",
10593 .maxBrightness = 255,
10594 .flags = InputLightClass::BRIGHTNESS,
10595 .path = ""};
10596 RawLightInfo info3 = {.id = 3,
10597 .name = "player3",
10598 .maxBrightness = 255,
10599 .flags = InputLightClass::BRIGHTNESS,
10600 .path = ""};
10601 RawLightInfo info4 = {.id = 4,
10602 .name = "player4",
10603 .maxBrightness = 255,
10604 .flags = InputLightClass::BRIGHTNESS,
10605 .path = ""};
10606 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10607 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10608 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10609 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10610
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010611 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010612 InputDeviceInfo info;
10613 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010614 std::vector<InputDeviceLightInfo> lights = info.getLights();
10615 ASSERT_EQ(1U, lights.size());
10616 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010617 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10618 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010619
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010620 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10621 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10622 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010623}
10624
Michael Wrightd02c5b62014-02-10 15:10:22 -080010625} // namespace android