blob: 804b4f77e6afe185ce8ed9820d7e3ad628ca5ec4 [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
Harry Cuttsa32a1192024-06-04 15:10:31 +0000312 std::list<NotifyArgs> process(const RawEvent& rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700313 std::scoped_lock<std::mutex> lock(mLock);
Harry Cuttsa32a1192024-06-04 15:10:31 +0000314 mLastEvent = rawEvent;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800315 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:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003317 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003318 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003319 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003320
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003321 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003322 int32_t originalKeyCode, int32_t rotatedKeyCode,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003323 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003324};
3325
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003326/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3327 * orientation.
3328 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003329void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003330 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3331 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003332}
3333
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003334void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003335 int32_t originalScanCode, int32_t originalKeyCode,
Linnan Li13bf76a2024-05-05 19:18:02 +08003336 int32_t rotatedKeyCode,
3337 ui::LogicalDisplayId displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338 NotifyKeyArgs args;
3339
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3342 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3343 ASSERT_EQ(originalScanCode, args.scanCode);
3344 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003345 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3350 ASSERT_EQ(originalScanCode, args.scanCode);
3351 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003353}
3354
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003356 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003357 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003358 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003360 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361}
3362
3363TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3364 const int32_t USAGE_A = 0x070004;
3365 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003366 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3367 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003368 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3369 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3370 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003371
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003372 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003373 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003374 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003375 // Initial metastate is AMETA_NONE.
3376 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003377
3378 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003380 NotifyKeyArgs args;
3381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3382 ASSERT_EQ(DEVICE_ID, args.deviceId);
3383 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3384 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3385 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3386 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3387 ASSERT_EQ(KEY_HOME, args.scanCode);
3388 ASSERT_EQ(AMETA_NONE, args.metaState);
3389 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3390 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3391 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3392
3393 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003394 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3396 ASSERT_EQ(DEVICE_ID, args.deviceId);
3397 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3398 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3399 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3400 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3401 ASSERT_EQ(KEY_HOME, args.scanCode);
3402 ASSERT_EQ(AMETA_NONE, args.metaState);
3403 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3404 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3405 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3406
3407 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3411 ASSERT_EQ(DEVICE_ID, args.deviceId);
3412 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3413 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3414 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3415 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3416 ASSERT_EQ(0, args.scanCode);
3417 ASSERT_EQ(AMETA_NONE, args.metaState);
3418 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3419 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3420 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3421
3422 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003423 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3424 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3426 ASSERT_EQ(DEVICE_ID, args.deviceId);
3427 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3428 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3429 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3430 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3431 ASSERT_EQ(0, args.scanCode);
3432 ASSERT_EQ(AMETA_NONE, args.metaState);
3433 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3434 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3435 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3436
3437 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003438 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(DEVICE_ID, args.deviceId);
3442 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3443 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3444 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3445 ASSERT_EQ(0, args.keyCode);
3446 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3447 ASSERT_EQ(AMETA_NONE, args.metaState);
3448 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3449 ASSERT_EQ(0U, args.policyFlags);
3450 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3451
3452 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003453 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3454 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3456 ASSERT_EQ(DEVICE_ID, args.deviceId);
3457 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3458 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3459 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3460 ASSERT_EQ(0, args.keyCode);
3461 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3462 ASSERT_EQ(AMETA_NONE, args.metaState);
3463 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3464 ASSERT_EQ(0U, args.policyFlags);
3465 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3466}
3467
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003468TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3469 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3470 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3471 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3472
3473 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003474 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003475 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3476
3477 // Key down by scan code.
3478 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3479 NotifyKeyArgs args;
3480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3481 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3482
3483 // Key up by scan code.
3484 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3486 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3487}
3488
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003489/**
3490 * Ensure that the readTime is set to the time when the EV_KEY is received.
3491 */
3492TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3494
3495 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003496 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003497 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3498 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 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003519 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003520 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003521
Arthur Hung95f68612022-04-07 14:08:22 +08003522 // Initial metastate is AMETA_NONE.
3523 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
3525 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 NotifyKeyArgs args;
3528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3529 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003530 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003531 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003532
3533 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003534 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3536 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003538
3539 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003540 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3542 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003543 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003544
3545 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003546 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3548 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003549 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003550 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551}
3552
3553TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003554 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3555 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3557 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003559 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003560 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003561 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003562
Michael Wrighta9cf4192022-12-01 23:46:39 +00003563 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3565 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3566 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3567 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3568 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3569 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3570 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3571 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3572}
3573
3574TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3577 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3578 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003579
Michael Wrightd02c5b62014-02-10 15:10:22 -08003580 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003581 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003582 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003583 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003584
Michael Wrighta9cf4192022-12-01 23:46:39 +00003585 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003586 ASSERT_NO_FATAL_FAILURE(
3587 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3588 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3589 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3590 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3591 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3592 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3593 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003594
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003595 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003596 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003597 ASSERT_NO_FATAL_FAILURE(
3598 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3599 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3600 AKEYCODE_DPAD_UP, DISPLAY_ID));
3601 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3602 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3603 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3604 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003605
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003606 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003607 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003608 ASSERT_NO_FATAL_FAILURE(
3609 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3610 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3611 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3612 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3613 AKEYCODE_DPAD_UP, DISPLAY_ID));
3614 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3615 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003617 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003618 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003619 ASSERT_NO_FATAL_FAILURE(
3620 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3621 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3622 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3623 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3624 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3625 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3626 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003627
3628 // Special case: if orientation changes while key is down, we still emit the same keycode
3629 // in the key up as we did in the key down.
3630 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003631 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003632 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003633 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3635 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3636 ASSERT_EQ(KEY_UP, args.scanCode);
3637 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3638
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003639 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003640 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3643 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3644 ASSERT_EQ(KEY_UP, args.scanCode);
3645 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3646}
3647
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003648TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3649 // If the keyboard is not orientation aware,
3650 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003651 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003652
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003653 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003654 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003655 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003656 NotifyKeyArgs args;
3657
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003658 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003663 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003664
Michael Wrighta9cf4192022-12-01 23:46:39 +00003665 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003666 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003670 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003671}
3672
3673TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3674 // If the keyboard is orientation aware,
3675 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003677
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003678 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003679 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003680 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003681 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003682 NotifyKeyArgs args;
3683
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003684 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003685 // ^--- already checked by the previous test
3686
Michael Wrighta9cf4192022-12-01 23:46:39 +00003687 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003688 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003689 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3693 ASSERT_EQ(DISPLAY_ID, args.displayId);
3694
Linnan Li13bf76a2024-05-05 19:18:02 +08003695 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003696 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003697 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003698 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3703 ASSERT_EQ(newDisplayId, args.displayId);
3704}
3705
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003708 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003709 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003711 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003712 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003714 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003715 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003716}
3717
Philip Junker4af3b3d2021-12-14 10:36:55 +01003718TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3719 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003720 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003721 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3722
3723 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3724 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3725 << "If a mapping is available, the result is equal to the mapping";
3726
3727 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3728 << "If no mapping is available, the result is the key location";
3729}
3730
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003733 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003736 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003739 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003740 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741}
3742
3743TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003744 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003745 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003746 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003748 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003751 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003752 ASSERT_TRUE(flags[0]);
3753 ASSERT_FALSE(flags[1]);
3754}
3755
3756TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003757 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3758 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3759 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3760 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3761 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3762 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003763
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003764 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003765 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003766 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003767 // Initial metastate is AMETA_NONE.
3768 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003769
3770 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003771 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3772 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3773 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003774
3775 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003778 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3779 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3780 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003781 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003782
3783 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003786 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3787 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3788 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003789 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790
3791 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003792 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3793 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003794 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3795 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3796 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003797 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003798
3799 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003802 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3803 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3804 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003805 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806
3807 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003808 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3809 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003810 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3811 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3812 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003813 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003814
3815 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003818 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3819 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3820 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003821 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003822}
3823
Chris Yea52ade12020-08-27 16:49:20 -07003824TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3825 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3826 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3827 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3828 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3829
3830 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003831 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003832 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3833
Chris Yea52ade12020-08-27 16:49:20 -07003834 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003835 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003836 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3837 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3838 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3839 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3840
3841 NotifyKeyArgs args;
3842 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3845 ASSERT_EQ(AMETA_NONE, args.metaState);
3846 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3847 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3848 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3849
3850 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003851 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3853 ASSERT_EQ(AMETA_NONE, args.metaState);
3854 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3855 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3856 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3857}
3858
Arthur Hung2c9a3342019-07-23 14:18:59 +08003859TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3860 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003861 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3862 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3863 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3864 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003865
3866 // keyboard 2.
3867 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003868 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003869 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003870 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003871 std::shared_ptr<InputDevice> device2 =
3872 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003873 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003874
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003875 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3876 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3877 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3878 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003879
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003880 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003881 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003882 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003883
Arpit Singh67ca6842023-04-26 14:43:16 +00003884 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003885 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003886 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3887 mFakePolicy
3888 ->getReaderConfiguration(),
3889 AINPUT_SOURCE_KEYBOARD,
3890 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003891 std::list<NotifyArgs> unused =
3892 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003893 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003894 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003895
3896 // Prepared displays and associated info.
3897 constexpr uint8_t hdmi1 = 0;
3898 constexpr uint8_t hdmi2 = 1;
3899 const std::string SECONDARY_UNIQUE_ID = "local:1";
3900
3901 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3902 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3903
3904 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003905 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003906 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003907 ASSERT_FALSE(device2->isEnabled());
3908
3909 // Prepare second display.
Linnan Li13bf76a2024-05-05 19:18:02 +08003910 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Michael Wrighta9cf4192022-12-01 23:46:39 +00003911 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003912 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003913 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003914 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003915 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003916 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003917 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003918
3919 // Device should be enabled after the associated display is found.
3920 ASSERT_TRUE(mDevice->isEnabled());
3921 ASSERT_TRUE(device2->isEnabled());
3922
3923 // Test pad key events
3924 ASSERT_NO_FATAL_FAILURE(
3925 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3926 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3927 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3928 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3929 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3930 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3931 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3932
3933 ASSERT_NO_FATAL_FAILURE(
3934 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3935 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3936 AKEYCODE_DPAD_RIGHT, newDisplayId));
3937 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3938 AKEYCODE_DPAD_DOWN, newDisplayId));
3939 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3940 AKEYCODE_DPAD_LEFT, newDisplayId));
3941}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942
arthurhungc903df12020-08-11 15:08:42 +08003943TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3944 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3945 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3946 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3947 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3948 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3949 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3950
3951 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003952 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003953 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003954 // Initial metastate is AMETA_NONE.
3955 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003956
3957 // Initialization should have turned all of the lights off.
3958 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3959 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3960 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3961
3962 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3964 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003965 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3966 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3967
3968 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003971 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3972 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3973
3974 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003975 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3976 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003977 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3978 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3979
3980 mFakeEventHub->removeDevice(EVENTHUB_ID);
3981 mReader->loopOnce();
3982
3983 // keyboard 2 should default toggle keys.
3984 const std::string USB2 = "USB2";
3985 const std::string DEVICE_NAME2 = "KEYBOARD2";
3986 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3987 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3988 std::shared_ptr<InputDevice> device2 =
3989 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003990 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003991 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3992 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3993 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3994 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3995 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3996 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3997
Arpit Singh67ca6842023-04-26 14:43:16 +00003998 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003999 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00004000 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
4001 mFakePolicy
4002 ->getReaderConfiguration(),
4003 AINPUT_SOURCE_KEYBOARD,
4004 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004005 std::list<NotifyArgs> unused =
4006 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004007 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004008 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08004009
4010 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
4011 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
4012 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08004013 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
4014 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08004015}
4016
Arthur Hungcb40a002021-08-03 14:31:01 +00004017TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
4018 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4019 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4020 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4021
4022 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00004023 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00004024 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4025 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004026 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00004027 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08004028 // Initial metastate is AMETA_NONE.
4029 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00004030
4031 mReader->toggleCapsLockState(DEVICE_ID);
4032 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
4033}
4034
Arthur Hungfb3cc112022-04-13 07:39:50 +00004035TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
4036 // keyboard 1.
4037 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4038 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
4039 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4040 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4041 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4042 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4043
4044 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00004045 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00004046 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4047
4048 // keyboard 2.
4049 const std::string USB2 = "USB2";
4050 const std::string DEVICE_NAME2 = "KEYBOARD2";
4051 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
4052 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
4053 std::shared_ptr<InputDevice> device2 =
4054 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
4055 ftl::Flags<InputDeviceClass>(0));
4056 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
4057 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
4058 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
4059 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
4060 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
4061 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
4062
Arpit Singh67ca6842023-04-26 14:43:16 +00004063 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004064 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00004065 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
4066 mFakePolicy
4067 ->getReaderConfiguration(),
4068 AINPUT_SOURCE_KEYBOARD,
4069 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004070 std::list<NotifyArgs> unused =
4071 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004072 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07004073 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004074
Arthur Hung95f68612022-04-07 14:08:22 +08004075 // Initial metastate is AMETA_NONE.
4076 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4077 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4078
4079 // Toggle num lock on and off.
4080 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4081 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004082 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4083 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4084 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4085
4086 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4087 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4088 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4089 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4090 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4091
4092 // Toggle caps lock on and off.
4093 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4094 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4095 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4096 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4097 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4098
4099 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4100 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4101 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4102 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4103 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4104
4105 // Toggle scroll lock on and off.
4106 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4107 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4108 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4109 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4110 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4111
4112 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4113 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4114 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4115 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4116 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4117}
4118
Arthur Hung2141d542022-08-23 07:45:21 +00004119TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4120 const int32_t USAGE_A = 0x070004;
4121 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4122 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4123
4124 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004125 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004126 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4127 // Key down by scan code.
4128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4129 NotifyKeyArgs args;
4130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4131 ASSERT_EQ(DEVICE_ID, args.deviceId);
4132 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4133 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4134 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4135 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4136 ASSERT_EQ(KEY_HOME, args.scanCode);
4137 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4138
4139 // Disable device, it should synthesize cancellation events for down events.
4140 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004141 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004142
4143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4144 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4145 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4146 ASSERT_EQ(KEY_HOME, args.scanCode);
4147 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4148}
4149
Zixuan Qufecb6062022-11-12 04:44:31 +00004150TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004151 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4152 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004153 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004154 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4155 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004156
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004157 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004158 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4159
4160 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004161 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004162
4163 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4164 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4165 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4166 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4167 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004168 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4169
4170 // Call change layout association with the same values: Generation shouldn't change
4171 generation = mReader->getContext()->getGeneration();
4172 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4173 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4174 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4175 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004176}
4177
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004178TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4179 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4180 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4181
4182 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004183 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004184 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4185 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004186 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004187
4188 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4189 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4190}
4191
Justin Chung71ddb432023-03-27 04:29:07 +00004192TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4193 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4194 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004195 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004196 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4197 NotifyKeyArgs args;
4198
4199 // Key down
4200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4202 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4203}
4204
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004205// --- KeyboardInputMapperTest_ExternalDevice ---
4206
4207class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4208protected:
Chris Yea52ade12020-08-27 16:49:20 -07004209 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004210};
4211
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004212TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004213 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4214 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004215
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004216 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4217 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4218 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4219 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004220
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004221 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004222 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004223 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004224
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004226 NotifyKeyArgs args;
4227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4228 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4229
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004230 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4232 ASSERT_EQ(uint32_t(0), args.policyFlags);
4233
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004236 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004237
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004238 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4240 ASSERT_EQ(uint32_t(0), args.policyFlags);
4241
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004242 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4244 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4245
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004246 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4248 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4249}
4250
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004251TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_NoneAlphabeticKeyboard) {
4252 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4253 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4254
4255 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4256 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4257 POLICY_FLAG_WAKE);
4258
4259 KeyboardInputMapper& mapper =
4260 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4261 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4262
4263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4264 NotifyKeyArgs args;
4265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4266 ASSERT_EQ(uint32_t(0), args.policyFlags);
4267
4268 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4270 ASSERT_EQ(uint32_t(0), args.policyFlags);
4271
4272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4274 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4275
4276 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4278 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4279}
4280
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004281TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004282 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004283
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004284 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4285 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4286 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004287
Powei Fengd041c5d2019-05-03 17:11:33 -07004288 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004289 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004290 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004291 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004292
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004294 NotifyKeyArgs args;
4295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4296 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4297
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004298 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4300 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4301
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4304 ASSERT_EQ(uint32_t(0), args.policyFlags);
4305
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004306 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4308 ASSERT_EQ(uint32_t(0), args.policyFlags);
4309
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4312 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4313
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004314 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4316 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4317}
4318
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319// --- TouchInputMapperTest ---
4320
4321class TouchInputMapperTest : public InputMapperTest {
4322protected:
4323 static const int32_t RAW_X_MIN;
4324 static const int32_t RAW_X_MAX;
4325 static const int32_t RAW_Y_MIN;
4326 static const int32_t RAW_Y_MAX;
4327 static const int32_t RAW_TOUCH_MIN;
4328 static const int32_t RAW_TOUCH_MAX;
4329 static const int32_t RAW_TOOL_MIN;
4330 static const int32_t RAW_TOOL_MAX;
4331 static const int32_t RAW_PRESSURE_MIN;
4332 static const int32_t RAW_PRESSURE_MAX;
4333 static const int32_t RAW_ORIENTATION_MIN;
4334 static const int32_t RAW_ORIENTATION_MAX;
4335 static const int32_t RAW_DISTANCE_MIN;
4336 static const int32_t RAW_DISTANCE_MAX;
4337 static const int32_t RAW_TILT_MIN;
4338 static const int32_t RAW_TILT_MAX;
4339 static const int32_t RAW_ID_MIN;
4340 static const int32_t RAW_ID_MAX;
4341 static const int32_t RAW_SLOT_MIN;
4342 static const int32_t RAW_SLOT_MAX;
4343 static const float X_PRECISION;
4344 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004345 static const float X_PRECISION_VIRTUAL;
4346 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004347
4348 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004349 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
4351 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4352
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004353 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004354 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004355
Michael Wrightd02c5b62014-02-10 15:10:22 -08004356 enum Axes {
4357 POSITION = 1 << 0,
4358 TOUCH = 1 << 1,
4359 TOOL = 1 << 2,
4360 PRESSURE = 1 << 3,
4361 ORIENTATION = 1 << 4,
4362 MINOR = 1 << 5,
4363 ID = 1 << 6,
4364 DISTANCE = 1 << 7,
4365 TILT = 1 << 8,
4366 SLOT = 1 << 9,
4367 TOOL_TYPE = 1 << 10,
4368 };
4369
Michael Wrighta9cf4192022-12-01 23:46:39 +00004370 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004371 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004372 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004373 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004374 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375 int32_t toRawX(float displayX);
4376 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004377 int32_t toRotatedRawX(float displayX);
4378 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004379 float toCookedX(float rawX, float rawY);
4380 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004381 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004382 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004384 float toDisplayY(int32_t rawY, int32_t displayHeight);
4385
Michael Wrightd02c5b62014-02-10 15:10:22 -08004386};
4387
4388const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4389const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4390const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4391const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4392const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4393const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4394const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4395const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004396const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4397const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4399const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4400const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4401const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4402const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4403const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4404const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4405const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4406const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4407const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4408const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4409const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004410const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4411 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4412const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4413 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004414const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4415 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416
4417const float TouchInputMapperTest::GEOMETRIC_SCALE =
4418 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4419 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4420
4421const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4422 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4423 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4424};
4425
Michael Wrighta9cf4192022-12-01 23:46:39 +00004426void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004427 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4428 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004429}
4430
4431void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4432 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004433 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004434}
4435
Michael Wrighta9cf4192022-12-01 23:46:39 +00004436void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004437 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4438 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4439 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004440}
4441
Michael Wrightd02c5b62014-02-10 15:10:22 -08004442void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004443 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4444 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4445 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4446 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004447}
4448
Jason Gerecke489fda82012-09-07 17:19:40 -07004449void TouchInputMapperTest::prepareLocationCalibration() {
4450 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4451}
4452
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453int32_t TouchInputMapperTest::toRawX(float displayX) {
4454 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4455}
4456
4457int32_t TouchInputMapperTest::toRawY(float displayY) {
4458 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4459}
4460
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004461int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4462 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4463}
4464
4465int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4466 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4467}
4468
Jason Gerecke489fda82012-09-07 17:19:40 -07004469float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4470 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4471 return rawX;
4472}
4473
4474float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4475 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4476 return rawY;
4477}
4478
Michael Wrightd02c5b62014-02-10 15:10:22 -08004479float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004480 return toDisplayX(rawX, DISPLAY_WIDTH);
4481}
4482
4483float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4484 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485}
4486
4487float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004488 return toDisplayY(rawY, DISPLAY_HEIGHT);
4489}
4490
4491float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4492 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493}
4494
4495
4496// --- SingleTouchInputMapperTest ---
4497
4498class SingleTouchInputMapperTest : public TouchInputMapperTest {
4499protected:
4500 void prepareButtons();
4501 void prepareAxes(int axes);
4502
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004503 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4504 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4505 void processUp(SingleTouchInputMapper& mappery);
4506 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4507 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4508 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4509 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4510 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4511 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512};
4513
4514void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004515 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516}
4517
4518void SingleTouchInputMapperTest::prepareAxes(int axes) {
4519 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004520 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4521 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 }
4523 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004524 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4525 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 }
4527 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004528 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4529 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530 }
4531 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004532 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4533 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534 }
4535 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004536 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4537 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004538 }
4539}
4540
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004541void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545}
4546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004547void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4549 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550}
4551
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004552void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554}
4555
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004556void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558}
4559
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004560void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4561 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563}
4564
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004565void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567}
4568
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004569void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4570 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004573}
4574
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004575void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4576 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578}
4579
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004580void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004581 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582}
4583
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004585 prepareButtons();
4586 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004587 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588
Josep del Río2d8c79a2023-01-23 19:33:50 +00004589 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004590}
4591
Michael Wrightd02c5b62014-02-10 15:10:22 -08004592TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593 prepareButtons();
4594 prepareAxes(POSITION);
4595 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004596 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004598 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599}
4600
4601TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004603 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604 prepareButtons();
4605 prepareAxes(POSITION);
4606 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004607 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004608
4609 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004610 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004611
4612 // Virtual key is down.
4613 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4614 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4615 processDown(mapper, x, y);
4616 processSync(mapper);
4617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004619 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620
4621 // Virtual key is up.
4622 processUp(mapper);
4623 processSync(mapper);
4624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4625
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004626 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004627}
4628
4629TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004630 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004631 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 prepareButtons();
4633 prepareAxes(POSITION);
4634 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004635 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636
4637 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004638 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639
4640 // Virtual key is down.
4641 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4642 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4643 processDown(mapper, x, y);
4644 processSync(mapper);
4645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4646
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004647 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648
4649 // Virtual key is up.
4650 processUp(mapper);
4651 processSync(mapper);
4652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4653
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004654 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655}
4656
4657TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004658 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004659 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 prepareButtons();
4661 prepareAxes(POSITION);
4662 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004663 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004664
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004666 ASSERT_TRUE(
4667 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004668 ASSERT_TRUE(flags[0]);
4669 ASSERT_FALSE(flags[1]);
4670}
4671
4672TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004674 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 prepareButtons();
4676 prepareAxes(POSITION);
4677 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004678 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004679
arthurhungdcef2dc2020-08-11 14:47:50 +08004680 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004681
4682 NotifyKeyArgs args;
4683
4684 // Press virtual key.
4685 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4686 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4687 processDown(mapper, x, y);
4688 processSync(mapper);
4689
4690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4691 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4692 ASSERT_EQ(DEVICE_ID, args.deviceId);
4693 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4694 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4695 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4696 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4697 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4698 ASSERT_EQ(KEY_HOME, args.scanCode);
4699 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4700 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4701
4702 // Release virtual key.
4703 processUp(mapper);
4704 processSync(mapper);
4705
4706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4707 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4708 ASSERT_EQ(DEVICE_ID, args.deviceId);
4709 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4710 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4711 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4712 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4713 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4714 ASSERT_EQ(KEY_HOME, args.scanCode);
4715 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4716 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4717
4718 // Should not have sent any motions.
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4720}
4721
4722TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004723 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004724 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004725 prepareButtons();
4726 prepareAxes(POSITION);
4727 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004728 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729
arthurhungdcef2dc2020-08-11 14:47:50 +08004730 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731
4732 NotifyKeyArgs keyArgs;
4733
4734 // Press virtual key.
4735 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4736 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4737 processDown(mapper, x, y);
4738 processSync(mapper);
4739
4740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4741 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4742 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4743 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4744 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4745 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4746 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4747 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4748 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4749 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4750 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4751
4752 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4753 // into the display area.
4754 y -= 100;
4755 processMove(mapper, x, y);
4756 processSync(mapper);
4757
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4759 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4760 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4761 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4762 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4763 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4764 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4765 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4766 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4767 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4768 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4769 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4770
4771 NotifyMotionArgs motionArgs;
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4773 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4774 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4775 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4776 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4777 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4778 ASSERT_EQ(0, motionArgs.flags);
4779 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4780 ASSERT_EQ(0, motionArgs.buttonState);
4781 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004782 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004784 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4786 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4787 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4788 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4789 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4790
4791 // Keep moving out of bounds. Should generate a pointer move.
4792 y -= 50;
4793 processMove(mapper, x, y);
4794 processSync(mapper);
4795
4796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4797 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4798 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4799 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4800 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4802 ASSERT_EQ(0, motionArgs.flags);
4803 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4804 ASSERT_EQ(0, motionArgs.buttonState);
4805 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004806 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004807 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004808 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004809 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4810 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4811 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4812 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4813 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4814
4815 // Release out of bounds. Should generate a pointer up.
4816 processUp(mapper);
4817 processSync(mapper);
4818
4819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4820 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4821 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4822 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4823 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4824 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4825 ASSERT_EQ(0, motionArgs.flags);
4826 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4827 ASSERT_EQ(0, motionArgs.buttonState);
4828 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004829 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004830 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004831 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4833 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4834 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4835 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4837
4838 // Should not have sent any more keys or motions.
4839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4841}
4842
4843TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004844 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004845 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846 prepareButtons();
4847 prepareAxes(POSITION);
4848 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004849 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004850
arthurhungdcef2dc2020-08-11 14:47:50 +08004851 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004852
4853 NotifyMotionArgs motionArgs;
4854
4855 // Initially go down out of bounds.
4856 int32_t x = -10;
4857 int32_t y = -10;
4858 processDown(mapper, x, y);
4859 processSync(mapper);
4860
4861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4862
4863 // Move into the display area. Should generate a pointer down.
4864 x = 50;
4865 y = 75;
4866 processMove(mapper, x, y);
4867 processSync(mapper);
4868
4869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4870 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4871 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4872 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4873 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4874 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4875 ASSERT_EQ(0, motionArgs.flags);
4876 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4877 ASSERT_EQ(0, motionArgs.buttonState);
4878 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004879 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004880 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004881 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4883 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4884 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4885 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4886 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4887
4888 // Release. Should generate a pointer up.
4889 processUp(mapper);
4890 processSync(mapper);
4891
4892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4893 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4894 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4895 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4896 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4897 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4898 ASSERT_EQ(0, motionArgs.flags);
4899 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4900 ASSERT_EQ(0, motionArgs.buttonState);
4901 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004902 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004904 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4906 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4907 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4908 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4909 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4910
4911 // Should not have sent any more keys or motions.
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4914}
4915
Santos Cordonfa5cf462017-04-05 10:37:00 -07004916TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004917 addConfigurationProperty("touch.deviceType", "touchScreen");
4918 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4919
Michael Wrighta9cf4192022-12-01 23:46:39 +00004920 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004921 prepareButtons();
4922 prepareAxes(POSITION);
4923 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004924 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004925
arthurhungdcef2dc2020-08-11 14:47:50 +08004926 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004927
4928 NotifyMotionArgs motionArgs;
4929
4930 // Down.
4931 int32_t x = 100;
4932 int32_t y = 125;
4933 processDown(mapper, x, y);
4934 processSync(mapper);
4935
4936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4937 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4938 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4939 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4940 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4941 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4942 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4943 ASSERT_EQ(0, motionArgs.flags);
4944 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4945 ASSERT_EQ(0, motionArgs.buttonState);
4946 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004947 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004948 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004949 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4951 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4952 1, 0, 0, 0, 0, 0, 0, 0));
4953 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4954 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4955 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4956
4957 // Move.
4958 x += 50;
4959 y += 75;
4960 processMove(mapper, x, y);
4961 processSync(mapper);
4962
4963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4964 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4965 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4966 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4967 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4968 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4970 ASSERT_EQ(0, motionArgs.flags);
4971 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4972 ASSERT_EQ(0, motionArgs.buttonState);
4973 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004974 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004975 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004976 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4978 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4979 1, 0, 0, 0, 0, 0, 0, 0));
4980 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4981 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4982 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4983
4984 // Up.
4985 processUp(mapper);
4986 processSync(mapper);
4987
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4989 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4990 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4991 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4992 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4993 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4994 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4995 ASSERT_EQ(0, motionArgs.flags);
4996 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4997 ASSERT_EQ(0, motionArgs.buttonState);
4998 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004999 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07005000 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005001 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5003 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5004 1, 0, 0, 0, 0, 0, 0, 0));
5005 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5006 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5007 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5008
5009 // Should not have sent any more keys or motions.
5010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5012}
5013
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005016 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017 prepareButtons();
5018 prepareAxes(POSITION);
5019 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005020 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021
arthurhungdcef2dc2020-08-11 14:47:50 +08005022 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023
5024 NotifyMotionArgs motionArgs;
5025
5026 // Down.
5027 int32_t x = 100;
5028 int32_t y = 125;
5029 processDown(mapper, x, y);
5030 processSync(mapper);
5031
5032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5033 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5034 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5035 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5036 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5037 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5038 ASSERT_EQ(0, motionArgs.flags);
5039 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5040 ASSERT_EQ(0, motionArgs.buttonState);
5041 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005042 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005044 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5046 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5047 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5048 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5049 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5050
5051 // Move.
5052 x += 50;
5053 y += 75;
5054 processMove(mapper, x, y);
5055 processSync(mapper);
5056
5057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5058 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5059 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5060 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5061 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5063 ASSERT_EQ(0, motionArgs.flags);
5064 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5065 ASSERT_EQ(0, motionArgs.buttonState);
5066 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005067 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005069 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5071 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5072 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5073 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5074 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5075
5076 // Up.
5077 processUp(mapper);
5078 processSync(mapper);
5079
5080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5082 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5083 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5084 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5085 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5086 ASSERT_EQ(0, motionArgs.flags);
5087 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5088 ASSERT_EQ(0, motionArgs.buttonState);
5089 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005090 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005091 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005092 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5094 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5095 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5096 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5097 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5098
5099 // Should not have sent any more keys or motions.
5100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5102}
5103
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005104TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 addConfigurationProperty("touch.deviceType", "touchScreen");
5106 prepareButtons();
5107 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005108 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5109 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005110 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111
5112 NotifyMotionArgs args;
5113
5114 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005115 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005116 processDown(mapper, toRawX(50), toRawY(75));
5117 processSync(mapper);
5118
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5120 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5121 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5122
5123 processUp(mapper);
5124 processSync(mapper);
5125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5126}
5127
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005128TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129 addConfigurationProperty("touch.deviceType", "touchScreen");
5130 prepareButtons();
5131 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005132 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5133 // orientation-aware are affected by display rotation.
5134 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005135 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005136
5137 NotifyMotionArgs args;
5138
5139 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005140 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005141 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005142 processDown(mapper, toRawX(50), toRawY(75));
5143 processSync(mapper);
5144
5145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5146 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5147 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5148
5149 processUp(mapper);
5150 processSync(mapper);
5151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5152
5153 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005154 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005155 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005156 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005157 processSync(mapper);
5158
5159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5160 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5161 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5162
5163 processUp(mapper);
5164 processSync(mapper);
5165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5166
5167 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005168 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005169 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005170 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5171 processSync(mapper);
5172
5173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5174 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5175 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5176
5177 processUp(mapper);
5178 processSync(mapper);
5179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5180
5181 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005182 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005183 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005184 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005185 processSync(mapper);
5186
5187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5188 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5189 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5190
5191 processUp(mapper);
5192 processSync(mapper);
5193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5194}
5195
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005196TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5197 addConfigurationProperty("touch.deviceType", "touchScreen");
5198 prepareButtons();
5199 prepareAxes(POSITION);
5200 addConfigurationProperty("touch.orientationAware", "1");
5201 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5202 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005203 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005204 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005205 NotifyMotionArgs args;
5206
5207 // Orientation 0.
5208 processDown(mapper, toRawX(50), toRawY(75));
5209 processSync(mapper);
5210
5211 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5212 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5213 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5214
5215 processUp(mapper);
5216 processSync(mapper);
5217 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5218}
5219
5220TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5221 addConfigurationProperty("touch.deviceType", "touchScreen");
5222 prepareButtons();
5223 prepareAxes(POSITION);
5224 addConfigurationProperty("touch.orientationAware", "1");
5225 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5226 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005227 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005228 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005229 NotifyMotionArgs args;
5230
5231 // Orientation 90.
5232 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5233 processSync(mapper);
5234
5235 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5236 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5237 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5238
5239 processUp(mapper);
5240 processSync(mapper);
5241 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5242}
5243
5244TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5245 addConfigurationProperty("touch.deviceType", "touchScreen");
5246 prepareButtons();
5247 prepareAxes(POSITION);
5248 addConfigurationProperty("touch.orientationAware", "1");
5249 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5250 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005251 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005252 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005253 NotifyMotionArgs args;
5254
5255 // Orientation 180.
5256 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5257 processSync(mapper);
5258
5259 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5260 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5261 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5262
5263 processUp(mapper);
5264 processSync(mapper);
5265 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5266}
5267
5268TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5269 addConfigurationProperty("touch.deviceType", "touchScreen");
5270 prepareButtons();
5271 prepareAxes(POSITION);
5272 addConfigurationProperty("touch.orientationAware", "1");
5273 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5274 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005275 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005276 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005277 NotifyMotionArgs args;
5278
5279 // Orientation 270.
5280 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5281 processSync(mapper);
5282
5283 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5284 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5285 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5286
5287 processUp(mapper);
5288 processSync(mapper);
5289 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5290}
5291
5292TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5293 addConfigurationProperty("touch.deviceType", "touchScreen");
5294 prepareButtons();
5295 prepareAxes(POSITION);
5296 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5297 // orientation-aware are affected by display rotation.
5298 addConfigurationProperty("touch.orientationAware", "0");
5299 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005300 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005301
5302 NotifyMotionArgs args;
5303
5304 // Orientation 90, Rotation 0.
5305 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005306 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005307 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5308 processSync(mapper);
5309
5310 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5311 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5312 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5313
5314 processUp(mapper);
5315 processSync(mapper);
5316 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5317
5318 // Orientation 90, Rotation 90.
5319 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005320 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005321 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005322 processSync(mapper);
5323
5324 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5325 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5326 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5327
5328 processUp(mapper);
5329 processSync(mapper);
5330 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5331
5332 // Orientation 90, Rotation 180.
5333 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005334 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005335 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5336 processSync(mapper);
5337
5338 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5339 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5340 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5341
5342 processUp(mapper);
5343 processSync(mapper);
5344 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5345
5346 // Orientation 90, Rotation 270.
5347 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005348 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005349 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 -07005350 processSync(mapper);
5351
5352 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5353 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5354 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5355
5356 processUp(mapper);
5357 processSync(mapper);
5358 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5359}
5360
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005361TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5362 addConfigurationProperty("touch.deviceType", "touchScreen");
5363 prepareButtons();
5364 prepareAxes(POSITION);
5365 addConfigurationProperty("touch.orientationAware", "1");
5366 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005367 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005368
5369 // Set a physical frame in the display viewport.
5370 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5371 viewport->physicalLeft = 20;
5372 viewport->physicalTop = 600;
5373 viewport->physicalRight = 30;
5374 viewport->physicalBottom = 610;
5375 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005376 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005377
5378 // Start the touch.
5379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5380 processSync(mapper);
5381
5382 // Expect all input starting outside the physical frame to be ignored.
5383 const std::array<Point, 6> outsidePoints = {
5384 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5385 for (const auto& p : outsidePoints) {
5386 processMove(mapper, toRawX(p.x), toRawY(p.y));
5387 processSync(mapper);
5388 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5389 }
5390
5391 // Move the touch into the physical frame.
5392 processMove(mapper, toRawX(25), toRawY(605));
5393 processSync(mapper);
5394 NotifyMotionArgs args;
5395 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5396 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5397 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5398 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5399
5400 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5401 for (const auto& p : outsidePoints) {
5402 processMove(mapper, toRawX(p.x), toRawY(p.y));
5403 processSync(mapper);
5404 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5405 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5406 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5407 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5408 }
5409
5410 processUp(mapper);
5411 processSync(mapper);
5412 EXPECT_NO_FATAL_FAILURE(
5413 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5414}
5415
Harry Cutts1db43992023-06-19 17:05:07 +00005416TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
Harry Cutts1db43992023-06-19 17:05:07 +00005417 addConfigurationProperty("touch.deviceType", "pointer");
5418 prepareAxes(POSITION);
5419 prepareDisplay(ui::ROTATION_0);
5420 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5421
5422 // Set a physical frame in the display viewport.
5423 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5424 viewport->physicalLeft = 20;
5425 viewport->physicalTop = 600;
5426 viewport->physicalRight = 30;
5427 viewport->physicalBottom = 610;
5428 mFakePolicy->updateViewport(*viewport);
5429 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5430
5431 // Start the touch.
5432 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5433 processSync(mapper);
5434
5435 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5436 // produced.
5437 const std::array<Point, 6> outsidePoints = {
5438 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5439 for (const auto& p : outsidePoints) {
5440 processMove(mapper, toRawX(p.x), toRawY(p.y));
5441 processSync(mapper);
5442 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5443 }
5444}
5445
Michael Wrightd02c5b62014-02-10 15:10:22 -08005446TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005447 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005448 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005449 prepareButtons();
5450 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005451 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005452
5453 // These calculations are based on the input device calibration documentation.
5454 int32_t rawX = 100;
5455 int32_t rawY = 200;
5456 int32_t rawPressure = 10;
5457 int32_t rawToolMajor = 12;
5458 int32_t rawDistance = 2;
5459 int32_t rawTiltX = 30;
5460 int32_t rawTiltY = 110;
5461
5462 float x = toDisplayX(rawX);
5463 float y = toDisplayY(rawY);
5464 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5465 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5466 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5467 float distance = float(rawDistance);
5468
5469 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5470 float tiltScale = M_PI / 180;
5471 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5472 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5473 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5474 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5475
5476 processDown(mapper, rawX, rawY);
5477 processPressure(mapper, rawPressure);
5478 processToolMajor(mapper, rawToolMajor);
5479 processDistance(mapper, rawDistance);
5480 processTilt(mapper, rawTiltX, rawTiltY);
5481 processSync(mapper);
5482
5483 NotifyMotionArgs args;
5484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5485 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5486 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5487 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
Prabir Pradhan9a53b552024-06-04 02:59:40 +00005488 ASSERT_EQ(args.flags,
5489 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION |
5490 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491}
5492
Jason Gerecke489fda82012-09-07 17:19:40 -07005493TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005494 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005495 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005496 prepareLocationCalibration();
5497 prepareButtons();
5498 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005499 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005500
5501 int32_t rawX = 100;
5502 int32_t rawY = 200;
5503
5504 float x = toDisplayX(toCookedX(rawX, rawY));
5505 float y = toDisplayY(toCookedY(rawX, rawY));
5506
5507 processDown(mapper, rawX, rawY);
5508 processSync(mapper);
5509
5510 NotifyMotionArgs args;
5511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5512 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5513 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5514}
5515
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005517 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005518 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 prepareButtons();
5520 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005521 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522
5523 NotifyMotionArgs motionArgs;
5524 NotifyKeyArgs keyArgs;
5525
5526 processDown(mapper, 100, 200);
5527 processSync(mapper);
5528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5530 ASSERT_EQ(0, motionArgs.buttonState);
5531
5532 // press BTN_LEFT, release BTN_LEFT
5533 processKey(mapper, BTN_LEFT, 1);
5534 processSync(mapper);
5535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5537 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5538
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5540 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5541 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5542
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543 processKey(mapper, BTN_LEFT, 0);
5544 processSync(mapper);
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005546 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005548
5549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005550 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005551 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552
5553 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5554 processKey(mapper, BTN_RIGHT, 1);
5555 processKey(mapper, BTN_MIDDLE, 1);
5556 processSync(mapper);
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5559 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5560 motionArgs.buttonState);
5561
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5563 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5564 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5565
5566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5567 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5568 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5569 motionArgs.buttonState);
5570
Michael Wrightd02c5b62014-02-10 15:10:22 -08005571 processKey(mapper, BTN_RIGHT, 0);
5572 processSync(mapper);
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005574 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005575 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005576
5577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005579 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005580
5581 processKey(mapper, BTN_MIDDLE, 0);
5582 processSync(mapper);
5583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005589 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005590
5591 // press BTN_BACK, release BTN_BACK
5592 processKey(mapper, BTN_BACK, 1);
5593 processSync(mapper);
5594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5595 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5596 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005597
Michael Wrightd02c5b62014-02-10 15:10:22 -08005598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005599 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005600 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5601
5602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5604 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605
5606 processKey(mapper, BTN_BACK, 0);
5607 processSync(mapper);
5608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005609 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005611
5612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005614 ASSERT_EQ(0, motionArgs.buttonState);
5615
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5617 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5618 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5619
5620 // press BTN_SIDE, release BTN_SIDE
5621 processKey(mapper, BTN_SIDE, 1);
5622 processSync(mapper);
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5624 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5625 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005626
Michael Wrightd02c5b62014-02-10 15:10:22 -08005627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005629 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5630
5631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5632 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5633 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005634
5635 processKey(mapper, BTN_SIDE, 0);
5636 processSync(mapper);
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005639 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005640
5641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005642 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005643 ASSERT_EQ(0, motionArgs.buttonState);
5644
Michael Wrightd02c5b62014-02-10 15:10:22 -08005645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5646 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5647 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5648
5649 // press BTN_FORWARD, release BTN_FORWARD
5650 processKey(mapper, BTN_FORWARD, 1);
5651 processSync(mapper);
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5653 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5654 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005655
Michael Wrightd02c5b62014-02-10 15:10:22 -08005656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005658 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5659
5660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5661 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5662 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005663
5664 processKey(mapper, BTN_FORWARD, 0);
5665 processSync(mapper);
5666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005669
5670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005672 ASSERT_EQ(0, motionArgs.buttonState);
5673
Michael Wrightd02c5b62014-02-10 15:10:22 -08005674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5675 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5676 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5677
5678 // press BTN_EXTRA, release BTN_EXTRA
5679 processKey(mapper, BTN_EXTRA, 1);
5680 processSync(mapper);
5681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5682 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5683 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005684
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005687 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5688
5689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5690 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5691 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005692
5693 processKey(mapper, BTN_EXTRA, 0);
5694 processSync(mapper);
5695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005696 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005697 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005698
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005701 ASSERT_EQ(0, motionArgs.buttonState);
5702
Michael Wrightd02c5b62014-02-10 15:10:22 -08005703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5704 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5705 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5706
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5708
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709 // press BTN_STYLUS, release BTN_STYLUS
5710 processKey(mapper, BTN_STYLUS, 1);
5711 processSync(mapper);
5712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005714 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5715
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5718 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005719
5720 processKey(mapper, BTN_STYLUS, 0);
5721 processSync(mapper);
5722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005723 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005725
5726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005728 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005729
5730 // press BTN_STYLUS2, release BTN_STYLUS2
5731 processKey(mapper, BTN_STYLUS2, 1);
5732 processSync(mapper);
5733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005735 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5736
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5738 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5739 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005740
5741 processKey(mapper, BTN_STYLUS2, 0);
5742 processSync(mapper);
5743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005744 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005746
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005749 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750
5751 // release touch
5752 processUp(mapper);
5753 processSync(mapper);
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5755 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5756 ASSERT_EQ(0, motionArgs.buttonState);
5757}
5758
5759TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005760 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005761 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762 prepareButtons();
5763 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005764 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765
5766 NotifyMotionArgs motionArgs;
5767
5768 // default tool type is finger
5769 processDown(mapper, 100, 200);
5770 processSync(mapper);
5771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5772 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005773 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005774
5775 // eraser
5776 processKey(mapper, BTN_TOOL_RUBBER, 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::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781
5782 // stylus
5783 processKey(mapper, BTN_TOOL_RUBBER, 0);
5784 processKey(mapper, BTN_TOOL_PEN, 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
5790 // brush
5791 processKey(mapper, BTN_TOOL_PEN, 0);
5792 processKey(mapper, BTN_TOOL_BRUSH, 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 // pencil
5799 processKey(mapper, BTN_TOOL_BRUSH, 0);
5800 processKey(mapper, BTN_TOOL_PENCIL, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005805
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005806 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005807 processKey(mapper, BTN_TOOL_PENCIL, 0);
5808 processKey(mapper, BTN_TOOL_AIRBRUSH, 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::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813
5814 // mouse
5815 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5816 processKey(mapper, BTN_TOOL_MOUSE, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005821
5822 // lens
5823 processKey(mapper, BTN_TOOL_MOUSE, 0);
5824 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829
5830 // double-tap
5831 processKey(mapper, BTN_TOOL_LENS, 0);
5832 processKey(mapper, BTN_TOOL_DOUBLETAP, 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 // triple-tap
5839 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5840 processKey(mapper, BTN_TOOL_TRIPLETAP, 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 // quad-tap
5847 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5848 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5849 processSync(mapper);
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005852 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853
5854 // finger
5855 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5856 processKey(mapper, BTN_TOOL_FINGER, 1);
5857 processSync(mapper);
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005860 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861
5862 // stylus trumps finger
5863 processKey(mapper, BTN_TOOL_PEN, 1);
5864 processSync(mapper);
5865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5866 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005867 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868
5869 // eraser trumps stylus
5870 processKey(mapper, BTN_TOOL_RUBBER, 1);
5871 processSync(mapper);
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005874 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875
5876 // mouse trumps eraser
5877 processKey(mapper, BTN_TOOL_MOUSE, 1);
5878 processSync(mapper);
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005881 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005882
5883 // back to default tool type
5884 processKey(mapper, BTN_TOOL_MOUSE, 0);
5885 processKey(mapper, BTN_TOOL_RUBBER, 0);
5886 processKey(mapper, BTN_TOOL_PEN, 0);
5887 processKey(mapper, BTN_TOOL_FINGER, 0);
5888 processSync(mapper);
5889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005891 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005892}
5893
5894TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005896 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897 prepareButtons();
5898 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005899 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005900 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901
5902 NotifyMotionArgs motionArgs;
5903
5904 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5905 processKey(mapper, BTN_TOOL_FINGER, 1);
5906 processMove(mapper, 100, 200);
5907 processSync(mapper);
5908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5909 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5910 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5911 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5912
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5914 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5916 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5917
5918 // move a little
5919 processMove(mapper, 150, 250);
5920 processSync(mapper);
5921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5922 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5924 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5925
5926 // down when BTN_TOUCH is pressed, pressure defaults to 1
5927 processKey(mapper, BTN_TOUCH, 1);
5928 processSync(mapper);
5929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5932 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5933
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5935 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5937 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5938
5939 // up when BTN_TOUCH is released, hover restored
5940 processKey(mapper, BTN_TOUCH, 0);
5941 processSync(mapper);
5942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5943 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5944 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5945 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5946
5947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5948 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5949 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5950 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5951
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5953 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5955 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5956
5957 // exit hover when pointer goes away
5958 processKey(mapper, BTN_TOOL_FINGER, 0);
5959 processSync(mapper);
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5961 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5963 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5964}
5965
5966TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005968 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969 prepareButtons();
5970 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005971 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972
5973 NotifyMotionArgs motionArgs;
5974
5975 // initially hovering because pressure is 0
5976 processDown(mapper, 100, 200);
5977 processPressure(mapper, 0);
5978 processSync(mapper);
5979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5980 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5982 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5983
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5985 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5987 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5988
5989 // move a little
5990 processMove(mapper, 150, 250);
5991 processSync(mapper);
5992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5993 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5995 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5996
5997 // down when pressure is non-zero
5998 processPressure(mapper, RAW_PRESSURE_MAX);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6001 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6002 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6003 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6004
6005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6006 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6008 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6009
6010 // up when pressure becomes 0, hover restored
6011 processPressure(mapper, 0);
6012 processSync(mapper);
6013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6014 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6016 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6017
6018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6019 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6021 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6022
6023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6024 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6025 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6026 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6027
6028 // exit hover when pointer goes away
6029 processUp(mapper);
6030 processSync(mapper);
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6032 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6034 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6035}
6036
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006037TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6038 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006039 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006040 prepareButtons();
6041 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006042 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006043
6044 // Touch down.
6045 processDown(mapper, 100, 200);
6046 processPressure(mapper, 1);
6047 processSync(mapper);
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6049 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6050
6051 // Reset the mapper. This should cancel the ongoing gesture.
6052 resetMapper(mapper, ARBITRARY_TIME);
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6054 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6055
6056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6057}
6058
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006059TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6060 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006061 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006062 prepareButtons();
6063 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00006064 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006065
6066 // Set the initial state for the touch pointer.
6067 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6068 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6069 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6070 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6071
6072 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006073 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6074 // does not generate any events.
6075 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006076
6077 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6078 // the recreated touch state to generate a down event.
6079 processSync(mapper);
6080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6081 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6082
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6084}
6085
lilinnan687e58f2022-07-19 16:00:50 +08006086TEST_F(SingleTouchInputMapperTest,
6087 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6088 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006089 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006090 prepareButtons();
6091 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006092 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006093 NotifyMotionArgs motionArgs;
6094
6095 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006096 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006097 processSync(mapper);
6098
6099 // We should receive a down event
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6102
6103 // Change display id
6104 clearViewports();
6105 prepareSecondaryDisplay(ViewportType::INTERNAL);
6106
6107 // We should receive a cancel event
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6109 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6110 // Then receive reset called
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6112}
6113
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006114TEST_F(SingleTouchInputMapperTest,
6115 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6116 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006117 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006118 prepareButtons();
6119 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006120 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6122 NotifyMotionArgs motionArgs;
6123
6124 // Start a new gesture.
6125 processDown(mapper, 100, 200);
6126 processSync(mapper);
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6128 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6129
6130 // Make the viewport inactive. This will put the device in disabled mode.
6131 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6132 viewport->isActive = false;
6133 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006134 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006135
6136 // We should receive a cancel event for the ongoing gesture.
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6138 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6139 // Then we should be notified that the device was reset.
6140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6141
6142 // No events are generated while the viewport is inactive.
6143 processMove(mapper, 101, 201);
6144 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006145 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006146 processSync(mapper);
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6148
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006149 // Start a new gesture while the viewport is still inactive.
6150 processDown(mapper, 300, 400);
6151 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6152 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6153 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6154 processSync(mapper);
6155
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006156 // Make the viewport active again. The device should resume processing events.
6157 viewport->isActive = true;
6158 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006159 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006160
6161 // The device is reset because it changes back to direct mode, without generating any events.
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6164
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006165 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006166 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6168 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006169
6170 // No more events.
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6173}
6174
Prabir Pradhan211ba622022-10-31 21:09:21 +00006175TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6176 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006177 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006178 prepareButtons();
6179 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006180 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6182
6183 // Press a stylus button.
6184 processKey(mapper, BTN_STYLUS, 1);
6185 processSync(mapper);
6186
6187 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6188 processDown(mapper, 100, 200);
6189 processSync(mapper);
6190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6191 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6192 WithCoords(toDisplayX(100), toDisplayY(200)),
6193 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6195 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6196 WithCoords(toDisplayX(100), toDisplayY(200)),
6197 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6198
6199 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6200 // the button has not actually been released, since there will be no pointers through which the
6201 // button state can be reported. The event is generated at the location of the pointer before
6202 // it went up.
6203 processUp(mapper);
6204 processSync(mapper);
6205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6206 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6207 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6209 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6210 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6211}
6212
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006213TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6214 addConfigurationProperty("touch.deviceType", "touchScreen");
6215 prepareDisplay(ui::ROTATION_0);
6216 prepareButtons();
6217 prepareAxes(POSITION);
6218
6219 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6220
Arpit Singha8c236b2023-04-25 13:56:05 +00006221 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6223
6224 // Press a stylus button.
6225 processKey(mapper, BTN_STYLUS, 1);
6226 processSync(mapper);
6227
6228 // Start a touch gesture and ensure that the stylus button is not reported.
6229 processDown(mapper, 100, 200);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6232 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6233
6234 // Release and press the stylus button again.
6235 processKey(mapper, BTN_STYLUS, 0);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6238 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6239 processKey(mapper, BTN_STYLUS, 1);
6240 processSync(mapper);
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6242 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6243
6244 // Release the touch gesture.
6245 processUp(mapper);
6246 processSync(mapper);
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6248 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6249
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6251}
6252
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006253TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6254 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6255 prepareDisplay(ui::ROTATION_0);
6256 prepareButtons();
6257 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006258 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6260
6261 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6262}
6263
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006264TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6265 // Initialize the device without setting device source to touch navigation.
6266 addConfigurationProperty("touch.deviceType", "touchScreen");
6267 prepareDisplay(ui::ROTATION_0);
6268 prepareButtons();
6269 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006270 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006271
6272 // Ensure that the device is created as a touchscreen, not touch navigation.
6273 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6274
6275 // Add device type association after the device was created.
6276 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6277
6278 // Send update to the mapper.
6279 std::list<NotifyArgs> unused2 =
6280 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006281 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006282
6283 // Check whether device type update was successful.
6284 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6285}
6286
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006287TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6288 // Initialize the device without setting device source to touch navigation.
6289 addConfigurationProperty("touch.deviceType", "touchScreen");
6290 prepareDisplay(ui::ROTATION_0);
6291 prepareButtons();
6292 prepareAxes(POSITION);
6293 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6294
6295 // Set a physical frame in the display viewport.
6296 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6297 viewport->physicalLeft = 0;
6298 viewport->physicalTop = 0;
6299 viewport->physicalRight = DISPLAY_WIDTH / 2;
6300 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6301 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006302 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006303
Arpit Singha8c236b2023-04-25 13:56:05 +00006304 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006305
6306 // Hovering inside the physical frame produces events.
6307 processKey(mapper, BTN_TOOL_PEN, 1);
6308 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6309 processSync(mapper);
6310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6311 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6313 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6314
6315 // Leaving the physical frame ends the hovering gesture.
6316 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6317 processSync(mapper);
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6319 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6320
6321 // Moving outside the physical frame does not produce events.
6322 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6323 processSync(mapper);
6324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6325
6326 // Re-entering the physical frame produces events.
6327 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6328 processSync(mapper);
6329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6330 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6332 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6333}
6334
Prabir Pradhan5632d622021-09-06 07:57:20 -07006335// --- TouchDisplayProjectionTest ---
6336
6337class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6338public:
6339 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6340 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6341 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006342 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6343 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6344 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006345 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006346 auto rotatedWidth = naturalDisplayWidth;
6347 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006348 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006349 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006350 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006351 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006352 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006353 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006354 inverseRotationFlags = ui::Transform::ROT_180;
6355 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006356 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006357 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006358 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006359 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006360 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006361 inverseRotationFlags = ui::Transform::ROT_0;
6362 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006363 }
6364
Prabir Pradhana9df3162022-12-05 23:57:27 +00006365 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006366 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6367
6368 std::optional<DisplayViewport> internalViewport =
6369 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6370 DisplayViewport& v = *internalViewport;
6371 v.displayId = DISPLAY_ID;
6372 v.orientation = orientation;
6373
6374 v.logicalLeft = 0;
6375 v.logicalTop = 0;
6376 v.logicalRight = 100;
6377 v.logicalBottom = 100;
6378
6379 v.physicalLeft = rotatedPhysicalDisplay.left;
6380 v.physicalTop = rotatedPhysicalDisplay.top;
6381 v.physicalRight = rotatedPhysicalDisplay.right;
6382 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6383
Prabir Pradhana9df3162022-12-05 23:57:27 +00006384 v.deviceWidth = rotatedWidth;
6385 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006386
6387 v.isActive = true;
6388 v.uniqueId = UNIQUE_ID;
6389 v.type = ViewportType::INTERNAL;
6390 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006391 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006392 }
6393
6394 void assertReceivedMove(const Point& point) {
6395 NotifyMotionArgs motionArgs;
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006398 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006399 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6400 1, 0, 0, 0, 0, 0, 0, 0));
6401 }
6402};
6403
6404TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6405 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006406 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006407
6408 prepareButtons();
6409 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006410 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006411
6412 NotifyMotionArgs motionArgs;
6413
6414 // Configure the DisplayViewport such that the logical display maps to a subsection of
6415 // the display panel called the physical display. Here, the physical display is bounded by the
6416 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6417 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6418 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6419 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6420
Michael Wrighta9cf4192022-12-01 23:46:39 +00006421 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006422 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6423
6424 // Touches outside the physical display should be ignored, and should not generate any
6425 // events. Ensure touches at the following points that lie outside of the physical display
6426 // area do not generate any events.
6427 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6428 processDown(mapper, toRawX(point.x), toRawY(point.y));
6429 processSync(mapper);
6430 processUp(mapper);
6431 processSync(mapper);
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6433 << "Unexpected event generated for touch outside physical display at point: "
6434 << point.x << ", " << point.y;
6435 }
6436 }
6437}
6438
6439TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6440 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006441 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006442
6443 prepareButtons();
6444 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006445 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006446
6447 NotifyMotionArgs motionArgs;
6448
6449 // Configure the DisplayViewport such that the logical display maps to a subsection of
6450 // the display panel called the physical display. Here, the physical display is bounded by the
6451 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6452 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6453
Michael Wrighta9cf4192022-12-01 23:46:39 +00006454 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006455 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6456
6457 // Touches that start outside the physical display should be ignored until it enters the
6458 // physical display bounds, at which point it should generate a down event. Start a touch at
6459 // the point (5, 100), which is outside the physical display bounds.
6460 static const Point kOutsidePoint{5, 100};
6461 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6464
6465 // Move the touch into the physical display area. This should generate a pointer down.
6466 processMove(mapper, toRawX(11), toRawY(21));
6467 processSync(mapper);
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6469 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006470 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006471 ASSERT_NO_FATAL_FAILURE(
6472 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6473
6474 // Move the touch inside the physical display area. This should generate a pointer move.
6475 processMove(mapper, toRawX(69), toRawY(159));
6476 processSync(mapper);
6477 assertReceivedMove({69, 159});
6478
6479 // Move outside the physical display area. Since the pointer is already down, this should
6480 // now continue generating events.
6481 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6482 processSync(mapper);
6483 assertReceivedMove(kOutsidePoint);
6484
6485 // Release. This should generate a pointer up.
6486 processUp(mapper);
6487 processSync(mapper);
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6489 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6490 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6491 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6492
6493 // Ensure no more events were generated.
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6496 }
6497}
6498
Prabir Pradhana9df3162022-12-05 23:57:27 +00006499// --- TouchscreenPrecisionTests ---
6500
6501// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6502// in various orientations and with different display rotations. We configure the touchscreen to
6503// have a higher resolution than that of the display by an integer scale factor in each axis so that
6504// we can enforce that coordinates match precisely as expected.
6505class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6506 public ::testing::WithParamInterface<ui::Rotation> {
6507public:
6508 void SetUp() override {
6509 SingleTouchInputMapperTest::SetUp();
6510
6511 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6512 // four times the resolution of the display in the Y axis.
6513 prepareButtons();
6514 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006515 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6516 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006517 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006518 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6519 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006520 }
6521
6522 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6523 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6524 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6525 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6526
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006527 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6528 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6529
6530 static const int32_t PRECISION_RAW_X_FLAT = 16;
6531 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6532
6533 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6534 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6535
Prabir Pradhana9df3162022-12-05 23:57:27 +00006536 static const std::array<Point, 4> kRawCorners;
6537};
6538
6539const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6540 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6541 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6542 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6543 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6544}};
6545
6546// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6547// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6548// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6549TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6550 enum class Orientation {
6551 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6552 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6553 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6554 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6555 ftl_last = ORIENTATION_270,
6556 };
6557 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6558 Orientation::ORIENTATION_270;
6559 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6560 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6561 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6562 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6563 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6564 };
6565
6566 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6567
6568 // Configure the touchscreen as being installed in the one of the four different orientations
6569 // relative to the display.
6570 addConfigurationProperty("touch.deviceType", "touchScreen");
6571 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6572 prepareDisplay(ui::ROTATION_0);
6573
Arpit Singha8c236b2023-04-25 13:56:05 +00006574 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006575
6576 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6577 // orientations of either 90 or 270) this means the display's natural resolution will be
6578 // flipped.
6579 const bool displayRotated =
6580 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6581 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6582 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6583 const Rect physicalFrame{0, 0, width, height};
6584 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6585
6586 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6587 const float expectedPrecisionX = displayRotated ? 4 : 2;
6588 const float expectedPrecisionY = displayRotated ? 2 : 4;
6589
6590 // Test all four corners.
6591 for (int i = 0; i < 4; i++) {
6592 const auto& raw = kRawCorners[i];
6593 processDown(mapper, raw.x, raw.y);
6594 processSync(mapper);
6595 const auto& expected = expectedPoints[i];
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6597 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6598 WithCoords(expected.x, expected.y),
6599 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6600 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6601 << "with touchscreen orientation "
6602 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6603 << expected.x << ", " << expected.y << ").";
6604 processUp(mapper);
6605 processSync(mapper);
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6607 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6608 WithCoords(expected.x, expected.y))));
6609 }
6610}
6611
Prabir Pradhan82687402022-12-06 01:32:53 +00006612TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6613 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6614 kMappedCorners = {
6615 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6616 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6617 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6618 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6619 };
6620
6621 const ui::Rotation displayRotation = GetParam();
6622
6623 addConfigurationProperty("touch.deviceType", "touchScreen");
6624 prepareDisplay(displayRotation);
6625
Arpit Singha8c236b2023-04-25 13:56:05 +00006626 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006627
6628 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6629
6630 // Test all four corners.
6631 for (int i = 0; i < 4; i++) {
6632 const auto& expected = expectedPoints[i];
6633 const auto& raw = kRawCorners[i];
6634 processDown(mapper, raw.x, raw.y);
6635 processSync(mapper);
6636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6637 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6638 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6639 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6640 << "with display rotation " << ui::toCString(displayRotation)
6641 << ", expected point (" << expected.x << ", " << expected.y << ").";
6642 processUp(mapper);
6643 processSync(mapper);
6644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6645 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6646 WithCoords(expected.x, expected.y))));
6647 }
6648}
6649
Prabir Pradhan3e798762022-12-02 21:02:11 +00006650TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6651 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6652 kMappedCorners = {
6653 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6654 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6655 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6656 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6657 };
6658
6659 const ui::Rotation displayRotation = GetParam();
6660
6661 addConfigurationProperty("touch.deviceType", "touchScreen");
6662 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6663
Arpit Singha8c236b2023-04-25 13:56:05 +00006664 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006665
6666 // Ori 270, so width and height swapped
6667 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6668 prepareDisplay(displayRotation);
6669 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6670
6671 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6672
6673 // Test all four corners.
6674 for (int i = 0; i < 4; i++) {
6675 const auto& expected = expectedPoints[i];
6676 const auto& raw = kRawCorners[i];
6677 processDown(mapper, raw.x, raw.y);
6678 processSync(mapper);
6679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6680 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6681 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6682 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6683 << "with display rotation " << ui::toCString(displayRotation)
6684 << ", expected point (" << expected.x << ", " << expected.y << ").";
6685 processUp(mapper);
6686 processSync(mapper);
6687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6688 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6689 WithCoords(expected.x, expected.y))));
6690 }
6691}
6692
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006693TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6694 const ui::Rotation displayRotation = GetParam();
6695
6696 addConfigurationProperty("touch.deviceType", "touchScreen");
6697 prepareDisplay(displayRotation);
6698
6699 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006700 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006701
6702 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6703 // MotionRanges use display pixels as their units
6704 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6705 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6706
6707 // The MotionRanges should be oriented in the rotated display's coordinate space
6708 const bool displayRotated =
6709 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6710
6711 constexpr float MAX_X = 479.5;
6712 constexpr float MAX_Y = 799.75;
6713 EXPECT_EQ(xRange->min, 0.f);
6714 EXPECT_EQ(yRange->min, 0.f);
6715 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6716 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6717
6718 EXPECT_EQ(xRange->flat, 8.f);
6719 EXPECT_EQ(yRange->flat, 8.f);
6720
6721 EXPECT_EQ(xRange->fuzz, 2.f);
6722 EXPECT_EQ(yRange->fuzz, 2.f);
6723
6724 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6725 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6726}
6727
Prabir Pradhana9df3162022-12-05 23:57:27 +00006728// Run the precision tests for all rotations.
6729INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6730 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6731 ui::ROTATION_270),
6732 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6733 return ftl::enum_string(testParamInfo.param);
6734 });
6735
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006736// --- ExternalStylusFusionTest ---
6737
6738class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6739public:
6740 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6741 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006742 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006743 prepareButtons();
6744 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006745 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006746
6747 mStylusState.when = ARBITRARY_TIME;
6748 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006749 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006750 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006751 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006752 processExternalStylusState(mapper);
6753 return mapper;
6754 }
6755
6756 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6757 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6758 for (const NotifyArgs& args : generatedArgs) {
6759 mFakeListener->notify(args);
6760 }
6761 // Loop the reader to flush the input listener queue.
6762 mReader->loopOnce();
6763 return generatedArgs;
6764 }
6765
6766protected:
6767 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006768
6769 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6770 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006771 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006772
6773 // The first pointer is withheld.
6774 processDown(mapper, 100, 200);
6775 processSync(mapper);
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6777 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6778 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6779
6780 // The external stylus reports pressure. The withheld finger pointer is released as a
6781 // stylus.
6782 mStylusState.pressure = 1.f;
6783 processExternalStylusState(mapper);
6784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6785 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6786 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6787
6788 // Subsequent pointer events are not withheld.
6789 processMove(mapper, 101, 201);
6790 processSync(mapper);
6791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6792 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6793
6794 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6796 }
6797
6798 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6799 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6800
6801 // Releasing the touch pointer ends the gesture.
6802 processUp(mapper);
6803 processSync(mapper);
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006805 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006806 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006807
6808 mStylusState.pressure = 0.f;
6809 processExternalStylusState(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6812 }
6813
6814 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006815 // When stylus fusion is not successful, events should be reported with the original source.
6816 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006817 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006818 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006819
6820 // The first pointer is withheld when an external stylus is connected,
6821 // and a timeout is requested.
6822 processDown(mapper, 100, 200);
6823 processSync(mapper);
6824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6825 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6826 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6827
6828 // If the timeout expires early, it is requested again.
6829 handleTimeout(mapper, ARBITRARY_TIME + 1);
6830 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6831 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6832
6833 // When the timeout expires, the withheld touch is released as a finger pointer.
6834 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6836 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6837
6838 // Subsequent pointer events are not withheld.
6839 processMove(mapper, 101, 201);
6840 processSync(mapper);
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6842 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6843 processUp(mapper);
6844 processSync(mapper);
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6846 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6847
6848 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6850 }
6851
6852private:
6853 InputDeviceInfo mExternalStylusDeviceInfo{};
6854};
6855
6856TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6857 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006858 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006859}
6860
6861TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6862 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6863 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6864}
6865
6866TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6867 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6868 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6869}
6870
6871// Test a successful stylus fusion gesture where the pressure is reported by the external
6872// before the touch is reported by the touchscreen.
6873TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6874 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006875 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006876
6877 // The external stylus reports pressure first. It is ignored for now.
6878 mStylusState.pressure = 1.f;
6879 processExternalStylusState(mapper);
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6881 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6882
6883 // When the touch goes down afterwards, it is reported as a stylus pointer.
6884 processDown(mapper, 100, 200);
6885 processSync(mapper);
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6887 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6888 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6889
6890 processMove(mapper, 101, 201);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6893 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6894 processUp(mapper);
6895 processSync(mapper);
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6897 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6898
6899 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6901}
6902
6903TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6904 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6905
6906 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6907 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6908
6909 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6910 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6911 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6912 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6913}
6914
6915TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6916 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006917 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006918
6919 mStylusState.pressure = 0.8f;
6920 processExternalStylusState(mapper);
6921 processDown(mapper, 100, 200);
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6924 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6925 WithPressure(0.8f))));
6926 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6927
6928 // The external stylus reports a pressure change. We wait for some time for a touch event.
6929 mStylusState.pressure = 0.6f;
6930 processExternalStylusState(mapper);
6931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6932 ASSERT_NO_FATAL_FAILURE(
6933 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6934
6935 // If a touch is reported within the timeout, it reports the updated pressure.
6936 processMove(mapper, 101, 201);
6937 processSync(mapper);
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6939 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6940 WithPressure(0.6f))));
6941 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6942
6943 // There is another pressure change.
6944 mStylusState.pressure = 0.5f;
6945 processExternalStylusState(mapper);
6946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6947 ASSERT_NO_FATAL_FAILURE(
6948 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6949
6950 // If a touch is not reported within the timeout, a move event is generated to report
6951 // the new pressure.
6952 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6954 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6955 WithPressure(0.5f))));
6956
6957 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6958 // repeated indefinitely.
6959 mStylusState.pressure = 0.0f;
6960 processExternalStylusState(mapper);
6961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6962 ASSERT_NO_FATAL_FAILURE(
6963 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6964 processMove(mapper, 102, 202);
6965 processSync(mapper);
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6967 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6968 WithPressure(0.5f))));
6969 processMove(mapper, 103, 203);
6970 processSync(mapper);
6971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6972 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6973 WithPressure(0.5f))));
6974
6975 processUp(mapper);
6976 processSync(mapper);
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006978 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006979 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006980
6981 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6983}
6984
6985TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6986 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006987 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006988
6989 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006990 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006991 processExternalStylusState(mapper);
6992 processDown(mapper, 100, 200);
6993 processSync(mapper);
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6995 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006996 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006997 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6998
6999 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007000 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007001 processExternalStylusState(mapper);
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7003 ASSERT_NO_FATAL_FAILURE(
7004 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7005
7006 // If a touch is reported within the timeout, it reports the updated pressure.
7007 processMove(mapper, 101, 201);
7008 processSync(mapper);
7009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7010 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007011 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007012 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7013
7014 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007015 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007016 processExternalStylusState(mapper);
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7018 ASSERT_NO_FATAL_FAILURE(
7019 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7020
7021 // If a touch is not reported within the timeout, a move event is generated to report
7022 // the new tool type.
7023 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7025 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007026 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007027
7028 processUp(mapper);
7029 processSync(mapper);
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7031 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007032 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007033
7034 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7036}
7037
7038TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7039 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007040 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007041
7042 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7043
7044 // The external stylus reports a button change. We wait for some time for a touch event.
7045 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7046 processExternalStylusState(mapper);
7047 ASSERT_NO_FATAL_FAILURE(
7048 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7049
7050 // If a touch is reported within the timeout, it reports the updated button state.
7051 processMove(mapper, 101, 201);
7052 processSync(mapper);
7053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7054 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7055 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7057 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7058 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7059 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7060
7061 // The button is now released.
7062 mStylusState.buttons = 0;
7063 processExternalStylusState(mapper);
7064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7065 ASSERT_NO_FATAL_FAILURE(
7066 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7067
7068 // If a touch is not reported within the timeout, a move event is generated to report
7069 // the new button state.
7070 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7072 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7073 WithButtonState(0))));
7074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007075 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7076 WithButtonState(0))));
7077
7078 processUp(mapper);
7079 processSync(mapper);
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007081 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7082
7083 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7085}
7086
Michael Wrightd02c5b62014-02-10 15:10:22 -08007087// --- MultiTouchInputMapperTest ---
7088
7089class MultiTouchInputMapperTest : public TouchInputMapperTest {
7090protected:
7091 void prepareAxes(int axes);
7092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007093 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7094 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7095 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7096 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7097 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7098 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7099 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7100 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7101 void processId(MultiTouchInputMapper& mapper, int32_t id);
7102 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7103 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7104 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007105 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007106 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007107 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7108 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109};
7110
7111void MultiTouchInputMapperTest::prepareAxes(int axes) {
7112 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007113 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7114 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007115 }
7116 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007117 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7118 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007119 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007120 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7121 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007122 }
7123 }
7124 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007125 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7126 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007128 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007129 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007130 }
7131 }
7132 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7134 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135 }
7136 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7138 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139 }
7140 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007141 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7142 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007143 }
7144 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007145 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7146 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147 }
7148 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007149 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7150 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151 }
7152 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007153 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154 }
7155}
7156
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007157void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7158 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161}
7162
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007163void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7164 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007166}
7167
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007168void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7169 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007170 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171}
7172
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007173void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175}
7176
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007177void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007178 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179}
7180
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007181void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7182 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007184}
7185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007186void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007188}
7189
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007190void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007192}
7193
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007194void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196}
7197
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007198void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007199 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200}
7201
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007202void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204}
7205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007206void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7207 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007209}
7210
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007211void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7212 int32_t value) {
7213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7215}
7216
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007217void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007219}
7220
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007221void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7222 nsecs_t readTime) {
7223 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007224}
7225
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007227 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007228 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229 prepareAxes(POSITION);
7230 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007231 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007232
arthurhungdcef2dc2020-08-11 14:47:50 +08007233 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007234
7235 NotifyMotionArgs motionArgs;
7236
7237 // Two fingers down at once.
7238 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7239 processPosition(mapper, x1, y1);
7240 processMTSync(mapper);
7241 processPosition(mapper, x2, y2);
7242 processMTSync(mapper);
7243 processSync(mapper);
7244
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7247 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7248 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7249 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7250 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7251 ASSERT_EQ(0, motionArgs.flags);
7252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7253 ASSERT_EQ(0, motionArgs.buttonState);
7254 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007255 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007257 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7259 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7260 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7261 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7263
7264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7266 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7267 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7268 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007269 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007270 ASSERT_EQ(0, motionArgs.flags);
7271 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7272 ASSERT_EQ(0, motionArgs.buttonState);
7273 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007274 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007275 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007276 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007277 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007278 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7280 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7282 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7283 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7284 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7286
7287 // Move.
7288 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7289 processPosition(mapper, x1, y1);
7290 processMTSync(mapper);
7291 processPosition(mapper, x2, y2);
7292 processMTSync(mapper);
7293 processSync(mapper);
7294
7295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7297 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7298 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7299 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7301 ASSERT_EQ(0, motionArgs.flags);
7302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7303 ASSERT_EQ(0, motionArgs.buttonState);
7304 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007305 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007307 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007308 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007309 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7311 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7313 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7314 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7315 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7317
7318 // First finger up.
7319 x2 += 15; y2 -= 20;
7320 processPosition(mapper, x2, y2);
7321 processMTSync(mapper);
7322 processSync(mapper);
7323
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7325 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7326 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7327 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7328 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007329 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007330 ASSERT_EQ(0, motionArgs.flags);
7331 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7332 ASSERT_EQ(0, motionArgs.buttonState);
7333 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007334 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007335 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007336 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007337 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007338 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7340 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7342 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7343 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7344 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7345 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7346
7347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7349 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7350 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7351 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7352 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7353 ASSERT_EQ(0, motionArgs.flags);
7354 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7355 ASSERT_EQ(0, motionArgs.buttonState);
7356 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007357 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007358 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007359 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7361 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7362 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7363 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7365
7366 // Move.
7367 x2 += 20; y2 -= 25;
7368 processPosition(mapper, x2, y2);
7369 processMTSync(mapper);
7370 processSync(mapper);
7371
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7374 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7375 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7376 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7378 ASSERT_EQ(0, motionArgs.flags);
7379 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7380 ASSERT_EQ(0, motionArgs.buttonState);
7381 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007382 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007383 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007384 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7386 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7387 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7388 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7390
7391 // New finger down.
7392 int32_t x3 = 700, y3 = 300;
7393 processPosition(mapper, x2, y2);
7394 processMTSync(mapper);
7395 processPosition(mapper, x3, y3);
7396 processMTSync(mapper);
7397 processSync(mapper);
7398
7399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7400 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7401 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7402 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7403 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007404 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007405 ASSERT_EQ(0, motionArgs.flags);
7406 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7407 ASSERT_EQ(0, motionArgs.buttonState);
7408 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007409 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007410 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007411 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007412 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007413 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007414 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7415 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7417 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7418 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7419 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7421
7422 // Second finger up.
7423 x3 += 30; y3 -= 20;
7424 processPosition(mapper, x3, y3);
7425 processMTSync(mapper);
7426 processSync(mapper);
7427
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7429 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7430 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7431 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7432 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007433 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007434 ASSERT_EQ(0, motionArgs.flags);
7435 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7436 ASSERT_EQ(0, motionArgs.buttonState);
7437 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007438 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007439 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007440 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007441 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007442 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007443 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7444 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7446 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7447 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7448 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7450
7451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7452 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7453 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7454 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7455 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7457 ASSERT_EQ(0, motionArgs.flags);
7458 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7459 ASSERT_EQ(0, motionArgs.buttonState);
7460 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007461 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007462 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007463 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007464 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7465 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7466 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7467 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7468 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7469
7470 // Last finger up.
7471 processMTSync(mapper);
7472 processSync(mapper);
7473
7474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7475 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7476 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7477 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7478 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7479 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7480 ASSERT_EQ(0, motionArgs.flags);
7481 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7482 ASSERT_EQ(0, motionArgs.buttonState);
7483 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007484 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007485 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007486 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7488 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7489 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7490 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7491 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7492
7493 // Should not have sent any more keys or motions.
7494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7496}
7497
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007498TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7499 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007500 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007501
7502 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7503 /*fuzz*/ 0, /*resolution*/ 10);
7504 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7505 /*fuzz*/ 0, /*resolution*/ 11);
7506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7507 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7508 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7509 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7511 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7512 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7513 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7514
Arpit Singha8c236b2023-04-25 13:56:05 +00007515 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007516
7517 // X and Y axes
7518 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7519 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7520 // Touch major and minor
7521 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7522 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7523 // Tool major and minor
7524 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7525 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7526}
7527
7528TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7529 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007530 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007531
7532 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7533 /*fuzz*/ 0, /*resolution*/ 10);
7534 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7535 /*fuzz*/ 0, /*resolution*/ 11);
7536
7537 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7538
Arpit Singha8c236b2023-04-25 13:56:05 +00007539 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007540
7541 // Touch major and minor
7542 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7543 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7544 // Tool major and minor
7545 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7546 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7547}
7548
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007550 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007551 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007552 prepareAxes(POSITION | ID);
7553 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007554 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007555
arthurhungdcef2dc2020-08-11 14:47:50 +08007556 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557
7558 NotifyMotionArgs motionArgs;
7559
7560 // Two fingers down at once.
7561 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7562 processPosition(mapper, x1, y1);
7563 processId(mapper, 1);
7564 processMTSync(mapper);
7565 processPosition(mapper, x2, y2);
7566 processId(mapper, 2);
7567 processMTSync(mapper);
7568 processSync(mapper);
7569
7570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007572 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007573 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007574 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7576 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7577
7578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007579 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007580 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007582 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007583 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007584 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7586 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7588 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7589
7590 // Move.
7591 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7592 processPosition(mapper, x1, y1);
7593 processId(mapper, 1);
7594 processMTSync(mapper);
7595 processPosition(mapper, x2, y2);
7596 processId(mapper, 2);
7597 processMTSync(mapper);
7598 processSync(mapper);
7599
7600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7601 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007602 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007603 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007604 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007605 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007606 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7608 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7610 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7611
7612 // First finger up.
7613 x2 += 15; y2 -= 20;
7614 processPosition(mapper, x2, y2);
7615 processId(mapper, 2);
7616 processMTSync(mapper);
7617 processSync(mapper);
7618
7619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007620 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007621 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007622 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007623 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007624 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007625 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7627 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7629 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7630
7631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007633 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007634 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007635 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7637 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7638
7639 // Move.
7640 x2 += 20; y2 -= 25;
7641 processPosition(mapper, x2, y2);
7642 processId(mapper, 2);
7643 processMTSync(mapper);
7644 processSync(mapper);
7645
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007648 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007649 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007650 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7652 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7653
7654 // New finger down.
7655 int32_t x3 = 700, y3 = 300;
7656 processPosition(mapper, x2, y2);
7657 processId(mapper, 2);
7658 processMTSync(mapper);
7659 processPosition(mapper, x3, y3);
7660 processId(mapper, 3);
7661 processMTSync(mapper);
7662 processSync(mapper);
7663
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007665 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007666 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007668 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007669 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007670 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7672 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7674 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7675
7676 // Second finger up.
7677 x3 += 30; y3 -= 20;
7678 processPosition(mapper, x3, y3);
7679 processId(mapper, 3);
7680 processMTSync(mapper);
7681 processSync(mapper);
7682
7683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007684 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007685 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007686 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007687 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007688 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007689 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7691 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7693 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7694
7695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007697 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007698 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007699 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7701 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7702
7703 // Last finger up.
7704 processMTSync(mapper);
7705 processSync(mapper);
7706
7707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7708 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007709 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007710 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007711 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7713 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7714
7715 // Should not have sent any more keys or motions.
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7718}
7719
7720TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007721 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007722 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007723 prepareAxes(POSITION | ID | SLOT);
7724 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007725 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007726
arthurhungdcef2dc2020-08-11 14:47:50 +08007727 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728
7729 NotifyMotionArgs motionArgs;
7730
7731 // Two fingers down at once.
7732 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7733 processPosition(mapper, x1, y1);
7734 processId(mapper, 1);
7735 processSlot(mapper, 1);
7736 processPosition(mapper, x2, y2);
7737 processId(mapper, 2);
7738 processSync(mapper);
7739
7740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7741 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007742 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007743 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007744 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7746 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7747
7748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007749 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007750 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007751 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007752 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007753 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007754 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7756 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7758 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7759
7760 // Move.
7761 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7762 processSlot(mapper, 0);
7763 processPosition(mapper, x1, y1);
7764 processSlot(mapper, 1);
7765 processPosition(mapper, x2, y2);
7766 processSync(mapper);
7767
7768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7769 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007770 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007771 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007772 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007773 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007774 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7776 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7778 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7779
7780 // First finger up.
7781 x2 += 15; y2 -= 20;
7782 processSlot(mapper, 0);
7783 processId(mapper, -1);
7784 processSlot(mapper, 1);
7785 processPosition(mapper, x2, y2);
7786 processSync(mapper);
7787
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007789 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007790 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007791 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007792 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007793 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007794 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7796 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7798 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7799
7800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007802 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007803 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007804 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7806 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7807
7808 // Move.
7809 x2 += 20; y2 -= 25;
7810 processPosition(mapper, x2, y2);
7811 processSync(mapper);
7812
7813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007815 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007816 ASSERT_EQ(1, 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_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7819 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7820
7821 // New finger down.
7822 int32_t x3 = 700, y3 = 300;
7823 processPosition(mapper, x2, y2);
7824 processSlot(mapper, 0);
7825 processId(mapper, 3);
7826 processPosition(mapper, x3, y3);
7827 processSync(mapper);
7828
7829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007830 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007831 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007832 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007833 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007834 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007835 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7837 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7839 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7840
7841 // Second finger up.
7842 x3 += 30; y3 -= 20;
7843 processSlot(mapper, 1);
7844 processId(mapper, -1);
7845 processSlot(mapper, 0);
7846 processPosition(mapper, x3, y3);
7847 processSync(mapper);
7848
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007850 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007851 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007853 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007854 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007855 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7857 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7859 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7860
7861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007863 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007864 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007865 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7867 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7868
7869 // Last finger up.
7870 processId(mapper, -1);
7871 processSync(mapper);
7872
7873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7874 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007875 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007876 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007877 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7879 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7880
7881 // Should not have sent any more keys or motions.
7882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7884}
7885
7886TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007887 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007888 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007889 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007890 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891
7892 // These calculations are based on the input device calibration documentation.
7893 int32_t rawX = 100;
7894 int32_t rawY = 200;
7895 int32_t rawTouchMajor = 7;
7896 int32_t rawTouchMinor = 6;
7897 int32_t rawToolMajor = 9;
7898 int32_t rawToolMinor = 8;
7899 int32_t rawPressure = 11;
7900 int32_t rawDistance = 0;
7901 int32_t rawOrientation = 3;
7902 int32_t id = 5;
7903
7904 float x = toDisplayX(rawX);
7905 float y = toDisplayY(rawY);
7906 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7907 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7908 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7909 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7910 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7911 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7912 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7913 float distance = float(rawDistance);
7914
7915 processPosition(mapper, rawX, rawY);
7916 processTouchMajor(mapper, rawTouchMajor);
7917 processTouchMinor(mapper, rawTouchMinor);
7918 processToolMajor(mapper, rawToolMajor);
7919 processToolMinor(mapper, rawToolMinor);
7920 processPressure(mapper, rawPressure);
7921 processOrientation(mapper, rawOrientation);
7922 processDistance(mapper, rawDistance);
7923 processId(mapper, id);
7924 processMTSync(mapper);
7925 processSync(mapper);
7926
7927 NotifyMotionArgs args;
7928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7929 ASSERT_EQ(0, args.pointerProperties[0].id);
7930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7931 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7932 orientation, distance));
Prabir Pradhan9a53b552024-06-04 02:59:40 +00007933 ASSERT_EQ(args.flags, AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007934}
7935
7936TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007938 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7940 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007941 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007942
7943 // These calculations are based on the input device calibration documentation.
7944 int32_t rawX = 100;
7945 int32_t rawY = 200;
7946 int32_t rawTouchMajor = 140;
7947 int32_t rawTouchMinor = 120;
7948 int32_t rawToolMajor = 180;
7949 int32_t rawToolMinor = 160;
7950
7951 float x = toDisplayX(rawX);
7952 float y = toDisplayY(rawY);
7953 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7954 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7955 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7956 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7957 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7958
7959 processPosition(mapper, rawX, rawY);
7960 processTouchMajor(mapper, rawTouchMajor);
7961 processTouchMinor(mapper, rawTouchMinor);
7962 processToolMajor(mapper, rawToolMajor);
7963 processToolMinor(mapper, rawToolMinor);
7964 processMTSync(mapper);
7965 processSync(mapper);
7966
7967 NotifyMotionArgs args;
7968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7970 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7971}
7972
7973TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007974 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007975 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007976 prepareAxes(POSITION | TOUCH | TOOL);
7977 addConfigurationProperty("touch.size.calibration", "diameter");
7978 addConfigurationProperty("touch.size.scale", "10");
7979 addConfigurationProperty("touch.size.bias", "160");
7980 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007981 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982
7983 // These calculations are based on the input device calibration documentation.
7984 // Note: We only provide a single common touch/tool value because the device is assumed
7985 // not to emit separate values for each pointer (isSummed = 1).
7986 int32_t rawX = 100;
7987 int32_t rawY = 200;
7988 int32_t rawX2 = 150;
7989 int32_t rawY2 = 250;
7990 int32_t rawTouchMajor = 5;
7991 int32_t rawToolMajor = 8;
7992
7993 float x = toDisplayX(rawX);
7994 float y = toDisplayY(rawY);
7995 float x2 = toDisplayX(rawX2);
7996 float y2 = toDisplayY(rawY2);
7997 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7998 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7999 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8000
8001 processPosition(mapper, rawX, rawY);
8002 processTouchMajor(mapper, rawTouchMajor);
8003 processToolMajor(mapper, rawToolMajor);
8004 processMTSync(mapper);
8005 processPosition(mapper, rawX2, rawY2);
8006 processTouchMajor(mapper, rawTouchMajor);
8007 processToolMajor(mapper, rawToolMajor);
8008 processMTSync(mapper);
8009 processSync(mapper);
8010
8011 NotifyMotionArgs args;
8012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8013 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8014
8015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008016 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008017 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8019 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8021 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8022}
8023
8024TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008026 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008027 prepareAxes(POSITION | TOUCH | TOOL);
8028 addConfigurationProperty("touch.size.calibration", "area");
8029 addConfigurationProperty("touch.size.scale", "43");
8030 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00008031 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008032
8033 // These calculations are based on the input device calibration documentation.
8034 int32_t rawX = 100;
8035 int32_t rawY = 200;
8036 int32_t rawTouchMajor = 5;
8037 int32_t rawToolMajor = 8;
8038
8039 float x = toDisplayX(rawX);
8040 float y = toDisplayY(rawY);
8041 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8042 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8043 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8044
8045 processPosition(mapper, rawX, rawY);
8046 processTouchMajor(mapper, rawTouchMajor);
8047 processToolMajor(mapper, rawToolMajor);
8048 processMTSync(mapper);
8049 processSync(mapper);
8050
8051 NotifyMotionArgs args;
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8054 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8055}
8056
8057TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008058 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008059 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008060 prepareAxes(POSITION | PRESSURE);
8061 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8062 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008063 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064
Michael Wrightaa449c92017-12-13 21:21:43 +00008065 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008066 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008067 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8068 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8069 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8070
Michael Wrightd02c5b62014-02-10 15:10:22 -08008071 // These calculations are based on the input device calibration documentation.
8072 int32_t rawX = 100;
8073 int32_t rawY = 200;
8074 int32_t rawPressure = 60;
8075
8076 float x = toDisplayX(rawX);
8077 float y = toDisplayY(rawY);
8078 float pressure = float(rawPressure) * 0.01f;
8079
8080 processPosition(mapper, rawX, rawY);
8081 processPressure(mapper, rawPressure);
8082 processMTSync(mapper);
8083 processSync(mapper);
8084
8085 NotifyMotionArgs args;
8086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8087 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8088 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8089}
8090
8091TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008093 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008095 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096
8097 NotifyMotionArgs motionArgs;
8098 NotifyKeyArgs keyArgs;
8099
8100 processId(mapper, 1);
8101 processPosition(mapper, 100, 200);
8102 processSync(mapper);
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8104 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8105 ASSERT_EQ(0, motionArgs.buttonState);
8106
8107 // press BTN_LEFT, release BTN_LEFT
8108 processKey(mapper, BTN_LEFT, 1);
8109 processSync(mapper);
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8111 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8112 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8113
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8116 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8117
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 processKey(mapper, BTN_LEFT, 0);
8119 processSync(mapper);
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008121 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008123
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008126 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008127
8128 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8129 processKey(mapper, BTN_RIGHT, 1);
8130 processKey(mapper, BTN_MIDDLE, 1);
8131 processSync(mapper);
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8134 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8135 motionArgs.buttonState);
8136
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8138 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8139 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8140
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8143 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8144 motionArgs.buttonState);
8145
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146 processKey(mapper, BTN_RIGHT, 0);
8147 processSync(mapper);
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008151
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008154 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008155
8156 processKey(mapper, BTN_MIDDLE, 0);
8157 processSync(mapper);
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008159 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008160 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008161
8162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008164 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165
8166 // press BTN_BACK, release BTN_BACK
8167 processKey(mapper, BTN_BACK, 1);
8168 processSync(mapper);
8169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8170 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8171 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008172
Michael Wrightd02c5b62014-02-10 15:10:22 -08008173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008175 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8176
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8179 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180
8181 processKey(mapper, BTN_BACK, 0);
8182 processSync(mapper);
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008186
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008189 ASSERT_EQ(0, motionArgs.buttonState);
8190
Michael Wrightd02c5b62014-02-10 15:10:22 -08008191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8192 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8193 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8194
8195 // press BTN_SIDE, release BTN_SIDE
8196 processKey(mapper, BTN_SIDE, 1);
8197 processSync(mapper);
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8199 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8200 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008201
Michael Wrightd02c5b62014-02-10 15:10:22 -08008202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008204 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8205
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8208 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008209
8210 processKey(mapper, BTN_SIDE, 0);
8211 processSync(mapper);
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008213 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008215
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218 ASSERT_EQ(0, motionArgs.buttonState);
8219
Michael Wrightd02c5b62014-02-10 15:10:22 -08008220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8221 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8222 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8223
8224 // press BTN_FORWARD, release BTN_FORWARD
8225 processKey(mapper, BTN_FORWARD, 1);
8226 processSync(mapper);
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8228 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8229 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008230
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008233 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8234
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8237 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238
8239 processKey(mapper, BTN_FORWARD, 0);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008242 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008244
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247 ASSERT_EQ(0, motionArgs.buttonState);
8248
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8250 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8251 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8252
8253 // press BTN_EXTRA, release BTN_EXTRA
8254 processKey(mapper, BTN_EXTRA, 1);
8255 processSync(mapper);
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8257 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8258 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008259
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008262 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8263
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8266 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267
8268 processKey(mapper, BTN_EXTRA, 0);
8269 processSync(mapper);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008271 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008273
8274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008276 ASSERT_EQ(0, motionArgs.buttonState);
8277
Michael Wrightd02c5b62014-02-10 15:10:22 -08008278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8279 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8280 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8281
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8283
Michael Wrightd02c5b62014-02-10 15:10:22 -08008284 // press BTN_STYLUS, release BTN_STYLUS
8285 processKey(mapper, BTN_STYLUS, 1);
8286 processSync(mapper);
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008289 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8290
8291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8292 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8293 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294
8295 processKey(mapper, BTN_STYLUS, 0);
8296 processSync(mapper);
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008299 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008300
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008303 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008304
8305 // press BTN_STYLUS2, release BTN_STYLUS2
8306 processKey(mapper, BTN_STYLUS2, 1);
8307 processSync(mapper);
8308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008310 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8311
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8314 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315
8316 processKey(mapper, BTN_STYLUS2, 0);
8317 processSync(mapper);
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008319 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008320 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008321
8322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008324 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008325
8326 // release touch
8327 processId(mapper, -1);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8331 ASSERT_EQ(0, motionArgs.buttonState);
8332}
8333
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008334TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8335 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008336 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008337 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008338 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008339
8340 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8341 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8342
8343 // Touch down.
8344 processId(mapper, 1);
8345 processPosition(mapper, 100, 200);
8346 processSync(mapper);
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8348 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8349
8350 // Press and release button mapped to the primary stylus button.
8351 processKey(mapper, BTN_A, 1);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8354 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8355 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8357 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8358 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8359
8360 processKey(mapper, BTN_A, 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 // Press and release the HID usage mapped to the secondary stylus button.
8368 processHidUsage(mapper, 0xabcd, 1);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8371 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8372 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8374 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8375 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8376
8377 processHidUsage(mapper, 0xabcd, 0);
8378 processSync(mapper);
8379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8380 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8382 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8383
8384 // Release touch.
8385 processId(mapper, -1);
8386 processSync(mapper);
8387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8388 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8389}
8390
Michael Wrightd02c5b62014-02-10 15:10:22 -08008391TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008392 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008393 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008394 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008395 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396
8397 NotifyMotionArgs motionArgs;
8398
8399 // default tool type is finger
8400 processId(mapper, 1);
8401 processPosition(mapper, 100, 200);
8402 processSync(mapper);
8403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8404 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008405 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406
8407 // eraser
8408 processKey(mapper, BTN_TOOL_RUBBER, 1);
8409 processSync(mapper);
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008412 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413
8414 // stylus
8415 processKey(mapper, BTN_TOOL_RUBBER, 0);
8416 processKey(mapper, BTN_TOOL_PEN, 1);
8417 processSync(mapper);
8418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008420 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008421
8422 // brush
8423 processKey(mapper, BTN_TOOL_PEN, 0);
8424 processKey(mapper, BTN_TOOL_BRUSH, 1);
8425 processSync(mapper);
8426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008428 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429
8430 // pencil
8431 processKey(mapper, BTN_TOOL_BRUSH, 0);
8432 processKey(mapper, BTN_TOOL_PENCIL, 1);
8433 processSync(mapper);
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8435 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008436 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008437
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008438 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008439 processKey(mapper, BTN_TOOL_PENCIL, 0);
8440 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8441 processSync(mapper);
8442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008444 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445
8446 // mouse
8447 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8448 processKey(mapper, BTN_TOOL_MOUSE, 1);
8449 processSync(mapper);
8450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8451 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008452 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008453
8454 // lens
8455 processKey(mapper, BTN_TOOL_MOUSE, 0);
8456 processKey(mapper, BTN_TOOL_LENS, 1);
8457 processSync(mapper);
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008460 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008461
8462 // double-tap
8463 processKey(mapper, BTN_TOOL_LENS, 0);
8464 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8465 processSync(mapper);
8466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008468 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008469
8470 // triple-tap
8471 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8472 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8473 processSync(mapper);
8474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8475 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008476 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008477
8478 // quad-tap
8479 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8480 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8481 processSync(mapper);
8482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008484 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485
8486 // finger
8487 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8488 processKey(mapper, BTN_TOOL_FINGER, 1);
8489 processSync(mapper);
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008492 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008493
8494 // stylus trumps finger
8495 processKey(mapper, BTN_TOOL_PEN, 1);
8496 processSync(mapper);
8497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008499 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008500
8501 // eraser trumps stylus
8502 processKey(mapper, BTN_TOOL_RUBBER, 1);
8503 processSync(mapper);
8504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8505 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008506 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507
8508 // mouse trumps eraser
8509 processKey(mapper, BTN_TOOL_MOUSE, 1);
8510 processSync(mapper);
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008513 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514
8515 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8516 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8517 processSync(mapper);
8518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008520 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008521
8522 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8523 processToolType(mapper, MT_TOOL_PEN);
8524 processSync(mapper);
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008527 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008528
8529 // back to default tool type
8530 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8531 processKey(mapper, BTN_TOOL_MOUSE, 0);
8532 processKey(mapper, BTN_TOOL_RUBBER, 0);
8533 processKey(mapper, BTN_TOOL_PEN, 0);
8534 processKey(mapper, BTN_TOOL_FINGER, 0);
8535 processSync(mapper);
8536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008538 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539}
8540
8541TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008542 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008543 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008544 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008545 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008546 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008547
8548 NotifyMotionArgs motionArgs;
8549
8550 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8551 processId(mapper, 1);
8552 processPosition(mapper, 100, 200);
8553 processSync(mapper);
8554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8555 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8557 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8558
8559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8560 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8561 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8562 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8563
8564 // move a little
8565 processPosition(mapper, 150, 250);
8566 processSync(mapper);
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8568 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8569 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8570 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8571
8572 // down when BTN_TOUCH is pressed, pressure defaults to 1
8573 processKey(mapper, BTN_TOUCH, 1);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8576 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8578 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8579
8580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8581 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8582 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8583 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8584
8585 // up when BTN_TOUCH is released, hover restored
8586 processKey(mapper, BTN_TOUCH, 0);
8587 processSync(mapper);
8588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8589 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8591 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8592
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8594 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8596 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8597
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8599 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8601 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8602
8603 // exit hover when pointer goes away
8604 processId(mapper, -1);
8605 processSync(mapper);
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8607 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8609 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8610}
8611
8612TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008613 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008614 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008616 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008617
8618 NotifyMotionArgs motionArgs;
8619
8620 // initially hovering because pressure is 0
8621 processId(mapper, 1);
8622 processPosition(mapper, 100, 200);
8623 processPressure(mapper, 0);
8624 processSync(mapper);
8625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8626 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8628 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8629
8630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8631 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8633 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8634
8635 // move a little
8636 processPosition(mapper, 150, 250);
8637 processSync(mapper);
8638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8639 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8641 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8642
8643 // down when pressure becomes non-zero
8644 processPressure(mapper, RAW_PRESSURE_MAX);
8645 processSync(mapper);
8646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8647 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8649 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8650
8651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8652 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8654 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8655
8656 // up when pressure becomes 0, hover restored
8657 processPressure(mapper, 0);
8658 processSync(mapper);
8659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8660 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8662 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8663
8664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8665 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8667 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8668
8669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8670 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8672 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8673
8674 // exit hover when pointer goes away
8675 processId(mapper, -1);
8676 processSync(mapper);
8677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8678 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8680 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8681}
8682
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008683/**
8684 * Set the input device port <--> display port associations, and check that the
8685 * events are routed to the display that matches the display port.
8686 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8687 */
8688TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008689 const std::string usb2 = "USB2";
8690 const uint8_t hdmi1 = 0;
8691 const uint8_t hdmi2 = 1;
8692 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008693 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008694
8695 addConfigurationProperty("touch.deviceType", "touchScreen");
8696 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008697 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008698
8699 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8700 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8701
8702 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8703 // for this input device is specified, and the matching viewport is not present,
8704 // the input device should be disabled (at the mapper level).
8705
8706 // Add viewport for display 2 on hdmi2
8707 prepareSecondaryDisplay(type, hdmi2);
8708 // Send a touch event
8709 processPosition(mapper, 100, 100);
8710 processSync(mapper);
8711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8712
8713 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008714 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008715 // Send a touch event again
8716 processPosition(mapper, 100, 100);
8717 processSync(mapper);
8718
8719 NotifyMotionArgs args;
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8721 ASSERT_EQ(DISPLAY_ID, args.displayId);
8722}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008723
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008724TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8725 addConfigurationProperty("touch.deviceType", "touchScreen");
8726 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008727 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008728
8729 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8730
Michael Wrighta9cf4192022-12-01 23:46:39 +00008731 prepareDisplay(ui::ROTATION_0);
8732 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008733
8734 // Send a touch event
8735 processPosition(mapper, 100, 100);
8736 processSync(mapper);
8737
8738 NotifyMotionArgs args;
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8740 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8741}
8742
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008743TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008744 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008745
Michael Wrighta9cf4192022-12-01 23:46:39 +00008746 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008747 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008748 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008749
Josep del Río2d8c79a2023-01-23 19:33:50 +00008750 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008751
8752 NotifyMotionArgs motionArgs;
8753 processPosition(mapper, 100, 100);
8754 processSync(mapper);
8755
8756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8757 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008758 ASSERT_EQ(ui::LogicalDisplayId::INVALID, motionArgs.displayId);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008759}
8760
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008761/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008762 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8763 */
8764TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8765 addConfigurationProperty("touch.deviceType", "touchScreen");
8766 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008767 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008768
Michael Wrighta9cf4192022-12-01 23:46:39 +00008769 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008770 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8771 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8772 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8773 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008774
8775 NotifyMotionArgs args;
8776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8777 ASSERT_EQ(26, args.readTime);
8778
Harry Cutts33476232023-01-30 19:57:29 +00008779 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8780 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8781 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008782
8783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8784 ASSERT_EQ(33, args.readTime);
8785}
8786
8787/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008788 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8789 * events should not be delivered to the listener.
8790 */
8791TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8792 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008793 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008794 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008795 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008796 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008797 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008798 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008799
8800 NotifyMotionArgs motionArgs;
8801 processPosition(mapper, 100, 100);
8802 processSync(mapper);
8803
8804 mFakeListener->assertNotifyMotionWasNotCalled();
8805}
8806
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008807/**
8808 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8809 * the touch mapper can process the events and the events can be delivered to the listener.
8810 */
8811TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8812 addConfigurationProperty("touch.deviceType", "touchScreen");
8813 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008814 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008815 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008816 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008817 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008818 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008819
8820 NotifyMotionArgs motionArgs;
8821 processPosition(mapper, 100, 100);
8822 processSync(mapper);
8823
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8825 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8826}
8827
Josh Thielene986aed2023-06-01 14:17:30 +00008828/**
8829 * When the viewport is deactivated (isActive transitions from true to false),
8830 * and touch.enableForInactiveViewport is false, touches prior to the transition
8831 * should be cancelled.
8832 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008833TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8834 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008835 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008836 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008837 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008838 std::optional<DisplayViewport> optionalDisplayViewport =
8839 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8840 ASSERT_TRUE(optionalDisplayViewport.has_value());
8841 DisplayViewport displayViewport = *optionalDisplayViewport;
8842
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008843 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008844 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008845 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008846
8847 // Finger down
8848 int32_t x = 100, y = 100;
8849 processPosition(mapper, x, y);
8850 processSync(mapper);
8851
8852 NotifyMotionArgs motionArgs;
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8854 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8855
8856 // Deactivate display viewport
8857 displayViewport.isActive = false;
8858 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008859 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008860
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008861 // The ongoing touch should be canceled immediately
8862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8863 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8864
8865 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008866 x += 10, y += 10;
8867 processPosition(mapper, x, y);
8868 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008870
8871 // Reactivate display viewport
8872 displayViewport.isActive = true;
8873 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008874 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008875
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008876 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008877 x += 10, y += 10;
8878 processPosition(mapper, x, y);
8879 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8881 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008882}
8883
Josh Thielene986aed2023-06-01 14:17:30 +00008884/**
8885 * When the viewport is deactivated (isActive transitions from true to false),
8886 * and touch.enableForInactiveViewport is true, touches prior to the transition
8887 * should not be cancelled.
8888 */
8889TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8890 addConfigurationProperty("touch.deviceType", "touchScreen");
8891 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8892 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8893 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8894 std::optional<DisplayViewport> optionalDisplayViewport =
8895 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8896 ASSERT_TRUE(optionalDisplayViewport.has_value());
8897 DisplayViewport displayViewport = *optionalDisplayViewport;
8898
8899 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8900 prepareAxes(POSITION);
8901 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8902
8903 // Finger down
8904 int32_t x = 100, y = 100;
8905 processPosition(mapper, x, y);
8906 processSync(mapper);
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8908 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8909
8910 // Deactivate display viewport
8911 displayViewport.isActive = false;
8912 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8913 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8914
8915 // The ongoing touch should not be canceled
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8917
8918 // Finger move is not ignored
8919 x += 10, y += 10;
8920 processPosition(mapper, x, y);
8921 processSync(mapper);
8922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8923 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8924
8925 // Reactivate display viewport
8926 displayViewport.isActive = true;
8927 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8928 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8929
8930 // Finger move continues and does not start new gesture
8931 x += 10, y += 10;
8932 processPosition(mapper, x, y);
8933 processSync(mapper);
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8935 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8936}
8937
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008938TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008939 prepareAxes(POSITION);
8940 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008941 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008942 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008943
8944 NotifyMotionArgs motionArgs;
8945 // Unrotated video frame
8946 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8947 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008948 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008949 processPosition(mapper, 100, 200);
8950 processSync(mapper);
8951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8952 ASSERT_EQ(frames, motionArgs.videoFrames);
8953
8954 // Subsequent touch events should not have any videoframes
8955 // This is implemented separately in FakeEventHub,
8956 // but that should match the behaviour of TouchVideoDevice.
8957 processPosition(mapper, 200, 200);
8958 processSync(mapper);
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8960 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8961}
8962
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008963TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008964 prepareAxes(POSITION);
8965 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008966 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008967 // Unrotated video frame
8968 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8969 NotifyMotionArgs motionArgs;
8970
8971 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008972 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008973 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008974 clearViewports();
8975 prepareDisplay(orientation);
8976 std::vector<TouchVideoFrame> frames{frame};
8977 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8978 processPosition(mapper, 100, 200);
8979 processSync(mapper);
8980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8981 ASSERT_EQ(frames, motionArgs.videoFrames);
8982 }
8983}
8984
8985TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8986 prepareAxes(POSITION);
8987 addConfigurationProperty("touch.deviceType", "touchScreen");
8988 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8989 // orientation-aware are affected by display rotation.
8990 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008991 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008992 // Unrotated video frame
8993 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8994 NotifyMotionArgs motionArgs;
8995
8996 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008997 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008998 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008999 clearViewports();
9000 prepareDisplay(orientation);
9001 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009002 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009003 processPosition(mapper, 100, 200);
9004 processSync(mapper);
9005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009006 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9007 // compared to the display. This is so that when the window transform (which contains the
9008 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9009 // window's coordinate space.
9010 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009011 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009012
9013 // Release finger.
9014 processSync(mapper);
9015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009016 }
9017}
9018
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009019TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009020 prepareAxes(POSITION);
9021 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009022 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009023 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9024 // so mix these.
9025 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9026 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9027 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9028 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9029 NotifyMotionArgs motionArgs;
9030
Michael Wrighta9cf4192022-12-01 23:46:39 +00009031 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009032 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009033 processPosition(mapper, 100, 200);
9034 processSync(mapper);
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009036 ASSERT_EQ(frames, motionArgs.videoFrames);
9037}
9038
9039TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9040 prepareAxes(POSITION);
9041 addConfigurationProperty("touch.deviceType", "touchScreen");
9042 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9043 // orientation-aware are affected by display rotation.
9044 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009045 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009046 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9047 // so mix these.
9048 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9049 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9050 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9051 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9052 NotifyMotionArgs motionArgs;
9053
Michael Wrighta9cf4192022-12-01 23:46:39 +00009054 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009055 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9056 processPosition(mapper, 100, 200);
9057 processSync(mapper);
9058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9059 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9060 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9061 // compared to the display. This is so that when the window transform (which contains the
9062 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9063 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009064 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009065 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009066 ASSERT_EQ(frames, motionArgs.videoFrames);
9067}
9068
Arthur Hung9da14732019-09-02 16:16:58 +08009069/**
9070 * If we had defined port associations, but the viewport is not ready, the touch device would be
9071 * expected to be disabled, and it should be enabled after the viewport has found.
9072 */
9073TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009074 constexpr uint8_t hdmi2 = 1;
9075 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009076 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009077
9078 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9079
9080 addConfigurationProperty("touch.deviceType", "touchScreen");
9081 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009082 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009083
9084 ASSERT_EQ(mDevice->isEnabled(), false);
9085
9086 // Add display on hdmi2, the device should be enabled and can receive touch event.
9087 prepareSecondaryDisplay(type, hdmi2);
9088 ASSERT_EQ(mDevice->isEnabled(), true);
9089
9090 // Send a touch event.
9091 processPosition(mapper, 100, 100);
9092 processSync(mapper);
9093
9094 NotifyMotionArgs args;
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9096 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9097}
9098
Arthur Hung421eb1c2020-01-16 00:09:42 +08009099TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009100 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009101 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009102 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009103 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009104
9105 NotifyMotionArgs motionArgs;
9106
9107 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9108 // finger down
9109 processId(mapper, 1);
9110 processPosition(mapper, x1, y1);
9111 processSync(mapper);
9112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9113 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009114 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009115
9116 // finger move
9117 processId(mapper, 1);
9118 processPosition(mapper, x2, y2);
9119 processSync(mapper);
9120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009122 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009123
9124 // finger up.
9125 processId(mapper, -1);
9126 processSync(mapper);
9127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9128 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009129 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009130
9131 // new finger down
9132 processId(mapper, 1);
9133 processPosition(mapper, x3, y3);
9134 processSync(mapper);
9135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9136 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009137 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009138}
9139
9140/**
arthurhungcc7f9802020-04-30 17:55:40 +08009141 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9142 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009143 */
arthurhungcc7f9802020-04-30 17:55:40 +08009144TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009145 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009146 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009147 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009148 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009149
9150 NotifyMotionArgs motionArgs;
9151
9152 // default tool type is finger
9153 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009154 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009155 processPosition(mapper, x1, y1);
9156 processSync(mapper);
9157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9158 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009159 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009160
9161 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9162 processToolType(mapper, MT_TOOL_PALM);
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9165 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9166
9167 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009168 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009169 processPosition(mapper, x2, y2);
9170 processSync(mapper);
9171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9172
9173 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009174 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009175 processSync(mapper);
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9177
9178 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009179 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009180 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009181 processPosition(mapper, x3, y3);
9182 processSync(mapper);
9183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9184 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009185 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009186}
9187
arthurhungbf89a482020-04-17 17:37:55 +08009188/**
arthurhungcc7f9802020-04-30 17:55:40 +08009189 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9190 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009191 */
arthurhungcc7f9802020-04-30 17:55:40 +08009192TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009193 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009194 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009195 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009196 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009197
9198 NotifyMotionArgs motionArgs;
9199
9200 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009201 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9202 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009203 processPosition(mapper, x1, y1);
9204 processSync(mapper);
9205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9206 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009207 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009208
9209 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009210 processSlot(mapper, SECOND_SLOT);
9211 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009212 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009213 processSync(mapper);
9214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009215 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009216 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009217
9218 // If the tool type of the first finger changes to MT_TOOL_PALM,
9219 // we expect to receive ACTION_POINTER_UP with cancel flag.
9220 processSlot(mapper, FIRST_SLOT);
9221 processId(mapper, FIRST_TRACKING_ID);
9222 processToolType(mapper, MT_TOOL_PALM);
9223 processSync(mapper);
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009225 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009226 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9227
9228 // The following MOVE events of second finger should be processed.
9229 processSlot(mapper, SECOND_SLOT);
9230 processId(mapper, SECOND_TRACKING_ID);
9231 processPosition(mapper, x2 + 1, y2 + 1);
9232 processSync(mapper);
9233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009235 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009236
9237 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9238 // it. Second finger receive move.
9239 processSlot(mapper, FIRST_SLOT);
9240 processId(mapper, INVALID_TRACKING_ID);
9241 processSync(mapper);
9242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009244 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009245
9246 // Second finger keeps moving.
9247 processSlot(mapper, SECOND_SLOT);
9248 processId(mapper, SECOND_TRACKING_ID);
9249 processPosition(mapper, x2 + 2, y2 + 2);
9250 processSync(mapper);
9251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009253 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009254
9255 // Second finger up.
9256 processId(mapper, INVALID_TRACKING_ID);
9257 processSync(mapper);
9258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9259 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9260 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9261}
9262
9263/**
9264 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9265 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9266 */
9267TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9268 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009269 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009270 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009271 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009272
9273 NotifyMotionArgs motionArgs;
9274
9275 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9276 // First finger down.
9277 processId(mapper, FIRST_TRACKING_ID);
9278 processPosition(mapper, x1, y1);
9279 processSync(mapper);
9280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9281 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009282 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009283
9284 // Second finger down.
9285 processSlot(mapper, SECOND_SLOT);
9286 processId(mapper, SECOND_TRACKING_ID);
9287 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009288 processSync(mapper);
9289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009290 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009291 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009292
arthurhungcc7f9802020-04-30 17:55:40 +08009293 // If the tool type of the first finger changes to MT_TOOL_PALM,
9294 // we expect to receive ACTION_POINTER_UP with cancel flag.
9295 processSlot(mapper, FIRST_SLOT);
9296 processId(mapper, FIRST_TRACKING_ID);
9297 processToolType(mapper, MT_TOOL_PALM);
9298 processSync(mapper);
9299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009300 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009301 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9302
9303 // Second finger keeps moving.
9304 processSlot(mapper, SECOND_SLOT);
9305 processId(mapper, SECOND_TRACKING_ID);
9306 processPosition(mapper, x2 + 1, y2 + 1);
9307 processSync(mapper);
9308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9309 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9310
9311 // second finger becomes palm, receive cancel due to only 1 finger is active.
9312 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009313 processToolType(mapper, MT_TOOL_PALM);
9314 processSync(mapper);
9315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9316 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9317
arthurhungcc7f9802020-04-30 17:55:40 +08009318 // third finger down.
9319 processSlot(mapper, THIRD_SLOT);
9320 processId(mapper, THIRD_TRACKING_ID);
9321 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009322 processPosition(mapper, x3, y3);
9323 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9325 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009326 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009327 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009328
9329 // third finger move
9330 processId(mapper, THIRD_TRACKING_ID);
9331 processPosition(mapper, x3 + 1, y3 + 1);
9332 processSync(mapper);
9333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9335
9336 // first finger up, third finger receive move.
9337 processSlot(mapper, FIRST_SLOT);
9338 processId(mapper, INVALID_TRACKING_ID);
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009342 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009343
9344 // second finger up, third finger receive move.
9345 processSlot(mapper, SECOND_SLOT);
9346 processId(mapper, INVALID_TRACKING_ID);
9347 processSync(mapper);
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9349 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009350 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009351
9352 // third finger up.
9353 processSlot(mapper, THIRD_SLOT);
9354 processId(mapper, INVALID_TRACKING_ID);
9355 processSync(mapper);
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9358 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9359}
9360
9361/**
9362 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9363 * and the active finger could still be allowed to receive the events
9364 */
9365TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9366 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009367 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009368 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009369 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009370
9371 NotifyMotionArgs motionArgs;
9372
9373 // default tool type is finger
9374 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9375 processId(mapper, FIRST_TRACKING_ID);
9376 processPosition(mapper, x1, y1);
9377 processSync(mapper);
9378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9379 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009380 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009381
9382 // Second finger down.
9383 processSlot(mapper, SECOND_SLOT);
9384 processId(mapper, SECOND_TRACKING_ID);
9385 processPosition(mapper, x2, y2);
9386 processSync(mapper);
9387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009388 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009389 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009390
9391 // If the tool type of the second finger changes to MT_TOOL_PALM,
9392 // we expect to receive ACTION_POINTER_UP with cancel flag.
9393 processId(mapper, SECOND_TRACKING_ID);
9394 processToolType(mapper, MT_TOOL_PALM);
9395 processSync(mapper);
9396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009397 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009398 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9399
9400 // The following MOVE event should be processed.
9401 processSlot(mapper, FIRST_SLOT);
9402 processId(mapper, FIRST_TRACKING_ID);
9403 processPosition(mapper, x1 + 1, y1 + 1);
9404 processSync(mapper);
9405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009407 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009408
9409 // second finger up.
9410 processSlot(mapper, SECOND_SLOT);
9411 processId(mapper, INVALID_TRACKING_ID);
9412 processSync(mapper);
9413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9415
9416 // first finger keep moving
9417 processSlot(mapper, FIRST_SLOT);
9418 processId(mapper, FIRST_TRACKING_ID);
9419 processPosition(mapper, x1 + 2, y1 + 2);
9420 processSync(mapper);
9421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9423
9424 // first finger up.
9425 processId(mapper, INVALID_TRACKING_ID);
9426 processSync(mapper);
9427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9428 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9429 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009430}
9431
Arthur Hung9ad18942021-06-19 02:04:46 +00009432/**
9433 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9434 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9435 * cause slot be valid again.
9436 */
9437TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9438 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009439 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009440 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009441 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009442
9443 NotifyMotionArgs motionArgs;
9444
9445 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9446 // First finger down.
9447 processId(mapper, FIRST_TRACKING_ID);
9448 processPosition(mapper, x1, y1);
9449 processPressure(mapper, RAW_PRESSURE_MAX);
9450 processSync(mapper);
9451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9452 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009453 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009454
9455 // First finger move.
9456 processId(mapper, FIRST_TRACKING_ID);
9457 processPosition(mapper, x1 + 1, y1 + 1);
9458 processPressure(mapper, RAW_PRESSURE_MAX);
9459 processSync(mapper);
9460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9461 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009462 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009463
9464 // Second finger down.
9465 processSlot(mapper, SECOND_SLOT);
9466 processId(mapper, SECOND_TRACKING_ID);
9467 processPosition(mapper, x2, y2);
9468 processPressure(mapper, RAW_PRESSURE_MAX);
9469 processSync(mapper);
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009471 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009472 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009473
9474 // second finger up with some unexpected data.
9475 processSlot(mapper, SECOND_SLOT);
9476 processId(mapper, INVALID_TRACKING_ID);
9477 processPosition(mapper, x2, y2);
9478 processSync(mapper);
9479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009480 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009481 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009482
9483 // first finger up with some unexpected data.
9484 processSlot(mapper, FIRST_SLOT);
9485 processId(mapper, INVALID_TRACKING_ID);
9486 processPosition(mapper, x2, y2);
9487 processPressure(mapper, RAW_PRESSURE_MAX);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9490 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009491 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009492}
9493
Arpit Singh4b4a4572023-11-24 18:19:56 +00009494TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009495 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009496 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009497 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009498 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009499
9500 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009501 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009502 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009503 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009504 processPressure(mapper, RAW_PRESSURE_MAX);
9505 processSync(mapper);
9506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9507 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9508
9509 // Second finger down.
9510 processSlot(mapper, SECOND_SLOT);
9511 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009512 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009513 processPressure(mapper, RAW_PRESSURE_MAX);
9514 processSync(mapper);
9515 ASSERT_NO_FATAL_FAILURE(
9516 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9517
Arpit Singh4b4a4572023-11-24 18:19:56 +00009518 // Set MT Slot state to be repopulated for the required slots
9519 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9520 mtSlotValues[0] = FIRST_TRACKING_ID;
9521 mtSlotValues[1] = SECOND_TRACKING_ID;
9522 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9523
9524 mtSlotValues[0] = x1;
9525 mtSlotValues[1] = x2;
9526 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9527
9528 mtSlotValues[0] = y1;
9529 mtSlotValues[1] = y2;
9530 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9531
9532 mtSlotValues[0] = RAW_PRESSURE_MAX;
9533 mtSlotValues[1] = RAW_PRESSURE_MAX;
9534 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9535
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009536 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009537 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009538 resetMapper(mapper, ARBITRARY_TIME);
9539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9540 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009541
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009542 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9543 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009544 processPosition(mapper, 301, 302);
9545 processSync(mapper);
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9547 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9549 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009550
9551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9552}
9553
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009554TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009555 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009556 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009557 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009558 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009559
9560 // First finger touches down and releases.
9561 processId(mapper, FIRST_TRACKING_ID);
9562 processPosition(mapper, 100, 200);
9563 processPressure(mapper, RAW_PRESSURE_MAX);
9564 processSync(mapper);
9565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9566 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9567 processId(mapper, INVALID_TRACKING_ID);
9568 processSync(mapper);
9569 ASSERT_NO_FATAL_FAILURE(
9570 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9571
9572 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9573 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009574 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9576
9577 // Send an empty sync frame. Since there are no pointers, no events are generated.
9578 processSync(mapper);
9579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9580}
9581
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009582TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009583 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009584 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009585 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009586 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009588
9589 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9590 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9591 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9592 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9593 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9594
9595 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009596 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009597 processId(mapper, FIRST_TRACKING_ID);
9598 processToolType(mapper, MT_TOOL_PEN);
9599 processPosition(mapper, 100, 200);
9600 processPressure(mapper, RAW_PRESSURE_MAX);
9601 processSync(mapper);
9602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9603 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9604 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009605 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009606
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009607 // Now that we know the device supports styluses, ensure that the device is re-configured with
9608 // the stylus source.
9609 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9610 {
9611 const auto& devices = mReader->getInputDevices();
9612 auto deviceInfo =
9613 std::find_if(devices.begin(), devices.end(),
9614 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9615 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9616 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9617 }
9618
9619 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9621
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009622 processId(mapper, INVALID_TRACKING_ID);
9623 processSync(mapper);
9624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9625 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9626 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009627 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009628}
9629
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009630// --- MultiTouchInputMapperTest_ExternalDevice ---
9631
9632class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9633protected:
Chris Yea52ade12020-08-27 16:49:20 -07009634 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009635};
9636
9637/**
9638 * Expect fallback to internal viewport if device is external and external viewport is not present.
9639 */
9640TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9641 prepareAxes(POSITION);
9642 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009643 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009644 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009645
9646 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9647
9648 NotifyMotionArgs motionArgs;
9649
9650 // Expect the event to be sent to the internal viewport,
9651 // because an external viewport is not present.
9652 processPosition(mapper, 100, 100);
9653 processSync(mapper);
9654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009655 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, motionArgs.displayId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009656
9657 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009658 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009659 processPosition(mapper, 100, 100);
9660 processSync(mapper);
9661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9662 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9663}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009664
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00009665// TODO(b/281840344): Remove the test when the old touchpad stack is removed. It is currently
9666// unclear what the behavior of the touchpad logic in TouchInputMapper should do after the
9667// PointerChoreographer refactor.
9668TEST_F(MultiTouchInputMapperTest, DISABLED_Process_TouchpadPointer) {
Harry Cutts8722be92024-04-05 14:46:05 +00009669 // prepare device
Michael Wrighta9cf4192022-12-01 23:46:39 +00009670 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009671 prepareAxes(POSITION | ID | SLOT);
9672 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9673 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009674 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009675 // run uncaptured pointer tests - pushes out generic events
9676 // FINGER 0 DOWN
9677 processId(mapper, 3);
9678 processPosition(mapper, 100, 100);
9679 processKey(mapper, BTN_TOUCH, 1);
9680 processSync(mapper);
9681
9682 // start at (100,100), cursor should be at (0,0) * scale
9683 NotifyMotionArgs args;
9684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9685 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9686 ASSERT_NO_FATAL_FAILURE(
9687 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9688
9689 // FINGER 0 MOVE
9690 processPosition(mapper, 200, 200);
9691 processSync(mapper);
9692
9693 // compute scaling to help with touch position checking
9694 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9695 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9696 float scale =
9697 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9698
9699 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9701 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9703 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009704
9705 // BUTTON DOWN
9706 processKey(mapper, BTN_LEFT, 1);
9707 processSync(mapper);
9708
9709 // touchinputmapper design sends a move before button press
9710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9711 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9713 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9714
9715 // BUTTON UP
9716 processKey(mapper, BTN_LEFT, 0);
9717 processSync(mapper);
9718
9719 // touchinputmapper design sends a move after button release
9720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9721 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9723 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009724}
9725
Harry Cutts8722be92024-04-05 14:46:05 +00009726TEST_F(MultiTouchInputMapperTest, Touchpad_GetSources) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00009727 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009728 prepareAxes(POSITION | ID | SLOT);
9729 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Hiroki Sato25040232024-02-22 17:21:22 +09009730 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Arpit Singha8c236b2023-04-25 13:56:05 +00009731 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009732
Josep del Río2d8c79a2023-01-23 19:33:50 +00009733 // uncaptured touchpad should be a pointer device
9734 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009735}
9736
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009737// --- BluetoothMultiTouchInputMapperTest ---
9738
9739class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9740protected:
9741 void SetUp() override {
9742 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9743 }
9744};
9745
9746TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9747 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009748 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009749 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009750 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009751
9752 nsecs_t kernelEventTime = ARBITRARY_TIME;
9753 nsecs_t expectedEventTime = ARBITRARY_TIME;
9754 // Touch down.
9755 processId(mapper, FIRST_TRACKING_ID);
9756 processPosition(mapper, 100, 200);
9757 processPressure(mapper, RAW_PRESSURE_MAX);
9758 processSync(mapper, ARBITRARY_TIME);
9759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9760 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9761
9762 // Process several events that come in quick succession, according to their timestamps.
9763 for (int i = 0; i < 3; i++) {
9764 constexpr static nsecs_t delta = ms2ns(1);
9765 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9766 kernelEventTime += delta;
9767 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9768
9769 processPosition(mapper, 101 + i, 201 + i);
9770 processSync(mapper, kernelEventTime);
9771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9773 WithEventTime(expectedEventTime))));
9774 }
9775
9776 // Release the touch.
9777 processId(mapper, INVALID_TRACKING_ID);
9778 processPressure(mapper, RAW_PRESSURE_MIN);
9779 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9781 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9782 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9783}
9784
9785// --- MultiTouchPointerModeTest ---
9786
HQ Liue6983c72022-04-19 22:14:56 +00009787class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9788protected:
9789 float mPointerMovementScale;
9790 float mPointerXZoomScale;
9791 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9792 addConfigurationProperty("touch.deviceType", "pointer");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009793 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009794
9795 prepareAxes(POSITION);
9796 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9797 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9798 // needs to be disabled, and the pointer gesture needs to be enabled.
Hiroki Sato25040232024-02-22 17:21:22 +09009799 mFakePolicy->setPointerCapture(/*window=*/nullptr);
HQ Liue6983c72022-04-19 22:14:56 +00009800 mFakePolicy->setPointerGestureEnabled(true);
HQ Liue6983c72022-04-19 22:14:56 +00009801
9802 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9803 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9804 mPointerMovementScale =
9805 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9806 mPointerXZoomScale =
9807 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9808 }
9809
9810 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9811 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9812 /*flat*/ 0,
9813 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9814 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9815 /*flat*/ 0,
9816 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9817 }
9818};
9819
9820/**
9821 * Two fingers down on a pointer mode touch pad. The width
9822 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9823 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9824 * be greater than the both value to be freeform gesture, so that after two
9825 * fingers start to move downwards, the gesture should be swipe.
9826 */
9827TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9828 // The min freeform gesture width is 25units/mm x 30mm = 750
9829 // which is greater than fraction of the diagnal length of the touchpad (349).
9830 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +00009831 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009832 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009833 NotifyMotionArgs motionArgs;
9834
9835 // Two fingers down at once.
9836 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9837 // Pointer's initial position is used the [0,0] coordinate.
9838 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9839
9840 processId(mapper, FIRST_TRACKING_ID);
9841 processPosition(mapper, x1, y1);
9842 processMTSync(mapper);
9843 processId(mapper, SECOND_TRACKING_ID);
9844 processPosition(mapper, x2, y2);
9845 processMTSync(mapper);
9846 processSync(mapper);
9847
9848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009849 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009850 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009851 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009852 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009853 ASSERT_NO_FATAL_FAILURE(
9854 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9855
9856 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9857 // that there should be 1 pointer.
9858 int32_t movingDistance = 200;
9859 y1 += movingDistance;
9860 y2 += movingDistance;
9861
9862 processId(mapper, FIRST_TRACKING_ID);
9863 processPosition(mapper, x1, y1);
9864 processMTSync(mapper);
9865 processId(mapper, SECOND_TRACKING_ID);
9866 processPosition(mapper, x2, y2);
9867 processMTSync(mapper);
9868 processSync(mapper);
9869
9870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009871 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009873 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009874 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9876 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9877 0, 0, 0, 0));
9878}
9879
9880/**
9881 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9882 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9883 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9884 * value to be freeform gesture, so that after two fingers start to move downwards,
9885 * the gesture should be swipe.
9886 */
9887TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9888 // The min freeform gesture width is 5units/mm x 30mm = 150
9889 // which is greater than fraction of the diagnal length of the touchpad (349).
9890 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +00009891 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +00009892 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009893 NotifyMotionArgs motionArgs;
9894
9895 // Two fingers down at once.
9896 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9897 // Pointer's initial position is used the [0,0] coordinate.
9898 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9899
9900 processId(mapper, FIRST_TRACKING_ID);
9901 processPosition(mapper, x1, y1);
9902 processMTSync(mapper);
9903 processId(mapper, SECOND_TRACKING_ID);
9904 processPosition(mapper, x2, y2);
9905 processMTSync(mapper);
9906 processSync(mapper);
9907
9908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009909 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009910 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009911 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009912 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009913 ASSERT_NO_FATAL_FAILURE(
9914 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9915
9916 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9917 // and there should be 1 pointer.
9918 int32_t movingDistance = 200;
9919 y1 += movingDistance;
9920 y2 += movingDistance;
9921
9922 processId(mapper, FIRST_TRACKING_ID);
9923 processPosition(mapper, x1, y1);
9924 processMTSync(mapper);
9925 processId(mapper, SECOND_TRACKING_ID);
9926 processPosition(mapper, x2, y2);
9927 processMTSync(mapper);
9928 processSync(mapper);
9929
9930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009931 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009933 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009934 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009935 // New coordinate is the scaled relative coordinate from the initial coordinate.
9936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9937 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9938 0, 0, 0, 0));
9939}
9940
9941/**
9942 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9943 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9944 * freeform gestures after two fingers start to move downwards.
9945 */
9946TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +00009947 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009948 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009949
9950 NotifyMotionArgs motionArgs;
9951
9952 // Two fingers down at once. Wider than the max swipe width.
9953 // The gesture is expected to be PRESS, then transformed to FREEFORM
9954 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9955
9956 processId(mapper, FIRST_TRACKING_ID);
9957 processPosition(mapper, x1, y1);
9958 processMTSync(mapper);
9959 processId(mapper, SECOND_TRACKING_ID);
9960 processPosition(mapper, x2, y2);
9961 processMTSync(mapper);
9962 processSync(mapper);
9963
9964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009965 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009966 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009967 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009968 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009969 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9970 ASSERT_NO_FATAL_FAILURE(
9971 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9972
9973 int32_t movingDistance = 200;
9974
9975 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9976 // then two down events for two pointers.
9977 y1 += movingDistance;
9978 y2 += movingDistance;
9979
9980 processId(mapper, FIRST_TRACKING_ID);
9981 processPosition(mapper, x1, y1);
9982 processMTSync(mapper);
9983 processId(mapper, SECOND_TRACKING_ID);
9984 processPosition(mapper, x2, y2);
9985 processMTSync(mapper);
9986 processSync(mapper);
9987
9988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9989 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009990 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009991 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009993 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009994 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009995 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009997 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009998 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009999 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010000 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010001 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010002 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010003 // Two pointers' scaled relative coordinates from their initial centroid.
10004 // Initial y coordinates are 0 as y1 and y2 have the same value.
10005 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10006 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10007 // When pointers move, the new coordinates equal to the initial coordinates plus
10008 // scaled moving distance.
10009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10010 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10011 0, 0, 0, 0));
10012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10013 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10014 0, 0, 0, 0));
10015
10016 // Move two fingers down again, expect one MOVE motion event.
10017 y1 += movingDistance;
10018 y2 += movingDistance;
10019
10020 processId(mapper, FIRST_TRACKING_ID);
10021 processPosition(mapper, x1, y1);
10022 processMTSync(mapper);
10023 processId(mapper, SECOND_TRACKING_ID);
10024 processPosition(mapper, x2, y2);
10025 processMTSync(mapper);
10026 processSync(mapper);
10027
10028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010029 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010031 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010032 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010033 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10034 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10035 0, 0, 0, 0, 0));
10036 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10037 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10038 0, 0, 0, 0, 0));
10039}
10040
Harry Cutts39b7ca22022-10-05 15:55:48 +000010041TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010042 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010043 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010044 NotifyMotionArgs motionArgs;
10045
10046 // Place two fingers down.
10047 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10048
10049 processId(mapper, FIRST_TRACKING_ID);
10050 processPosition(mapper, x1, y1);
10051 processMTSync(mapper);
10052 processId(mapper, SECOND_TRACKING_ID);
10053 processPosition(mapper, x2, y2);
10054 processMTSync(mapper);
10055 processSync(mapper);
10056
10057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010058 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010059 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10060 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10061 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10062 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10063
10064 // Move the two fingers down and to the left.
10065 int32_t movingDistance = 200;
10066 x1 -= movingDistance;
10067 y1 += movingDistance;
10068 x2 -= movingDistance;
10069 y2 += movingDistance;
10070
10071 processId(mapper, FIRST_TRACKING_ID);
10072 processPosition(mapper, x1, y1);
10073 processMTSync(mapper);
10074 processId(mapper, SECOND_TRACKING_ID);
10075 processPosition(mapper, x2, y2);
10076 processMTSync(mapper);
10077 processSync(mapper);
10078
10079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010080 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010081 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10082 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10083 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10084 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10085}
10086
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010087TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010088 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010089 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010090 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10092
10093 // Start a stylus gesture.
10094 processKey(mapper, BTN_TOOL_PEN, 1);
10095 processId(mapper, FIRST_TRACKING_ID);
10096 processPosition(mapper, 100, 200);
10097 processSync(mapper);
10098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10099 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
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_MOVE),
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 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10110 // gesture should be disabled.
10111 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10112 viewport->isActive = false;
10113 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010114 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10117 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010118 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010119 // TODO(b/257078296): Pointer mode generates extra event.
10120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10121 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10122 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010123 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10125}
10126
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010127// --- JoystickInputMapperTest ---
10128
10129class JoystickInputMapperTest : public InputMapperTest {
10130protected:
10131 static const int32_t RAW_X_MIN;
10132 static const int32_t RAW_X_MAX;
10133 static const int32_t RAW_Y_MIN;
10134 static const int32_t RAW_Y_MAX;
10135
10136 void SetUp() override {
10137 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10138 }
10139 void prepareAxes() {
10140 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10141 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10142 }
10143
10144 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10145 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10146 }
10147
10148 void processSync(JoystickInputMapper& mapper) {
10149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10150 }
10151
Michael Wrighta9cf4192022-12-01 23:46:39 +000010152 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010153 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10154 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10155 NO_PORT, ViewportType::VIRTUAL);
10156 }
10157};
10158
10159const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10160const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10161const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10162const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10163
10164TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10165 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010166 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010167
10168 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10169
Michael Wrighta9cf4192022-12-01 23:46:39 +000010170 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010171
10172 // Send an axis event
10173 processAxis(mapper, ABS_X, 100);
10174 processSync(mapper);
10175
10176 NotifyMotionArgs args;
10177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10178 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10179
10180 // Send another axis event
10181 processAxis(mapper, ABS_Y, 100);
10182 processSync(mapper);
10183
10184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10185 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10186}
10187
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010188// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010189
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010190class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010191protected:
10192 static const char* DEVICE_NAME;
10193 static const char* DEVICE_LOCATION;
10194 static const int32_t DEVICE_ID;
10195 static const int32_t DEVICE_GENERATION;
10196 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010197 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010198 static const int32_t EVENTHUB_ID;
10199
10200 std::shared_ptr<FakeEventHub> mFakeEventHub;
10201 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010202 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010203 std::unique_ptr<InstrumentedInputReader> mReader;
10204 std::shared_ptr<InputDevice> mDevice;
10205
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010206 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010207 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010208 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010209 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010210 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010211 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010212 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10213 }
10214
10215 void SetUp() override { SetUp(DEVICE_CLASSES); }
10216
10217 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010218 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010219 mFakePolicy.clear();
10220 }
10221
Chris Yee2b1e5c2021-03-10 22:45:12 -080010222 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10223 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010224 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010225 InputDeviceIdentifier identifier;
10226 identifier.name = name;
10227 identifier.location = location;
10228 std::shared_ptr<InputDevice> device =
10229 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10230 identifier);
10231 mReader->pushNextDevice(device);
10232 mFakeEventHub->addDevice(eventHubId, name, classes);
10233 mReader->loopOnce();
10234 return device;
10235 }
10236
10237 template <class T, typename... Args>
10238 T& addControllerAndConfigure(Args... args) {
10239 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10240
10241 return controller;
10242 }
10243};
10244
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010245const char* PeripheralControllerTest::DEVICE_NAME = "device";
10246const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10247const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10248const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10249const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010250const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10251 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010252const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010253
10254// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010255class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010256protected:
10257 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010258 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010259 }
10260};
10261
10262TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010263 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010264
Harry Cuttsa5b71292022-11-28 12:56:17 +000010265 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10266 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10267 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010268}
10269
10270TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010271 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010272
Harry Cuttsa5b71292022-11-28 12:56:17 +000010273 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10274 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10275 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010276}
10277
10278// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010279class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010280protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010281 void SetUp() override {
10282 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10283 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010284};
10285
Chris Ye85758332021-05-16 23:05:17 -070010286TEST_F(LightControllerTest, MonoLight) {
10287 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010288 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010289 .maxBrightness = 255,
10290 .flags = InputLightClass::BRIGHTNESS,
10291 .path = ""};
10292 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010293
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010294 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010295 InputDeviceInfo info;
10296 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010297 std::vector<InputDeviceLightInfo> lights = info.getLights();
10298 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010299 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10300 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10301
10302 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10303 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10304}
10305
DingYong99f2c3c2023-12-20 15:46:06 +080010306TEST_F(LightControllerTest, MonoKeyboardMuteLight) {
10307 RawLightInfo infoMono = {.id = 1,
10308 .name = "mono_keyboard_mute",
10309 .maxBrightness = 255,
10310 .flags = InputLightClass::BRIGHTNESS |
10311 InputLightClass::KEYBOARD_MIC_MUTE,
10312 .path = ""};
10313 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10314
10315 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10316 std::list<NotifyArgs> unused =
10317 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10318 /*changes=*/{});
10319
10320 InputDeviceInfo info;
10321 controller.populateDeviceInfo(&info);
10322 std::vector<InputDeviceLightInfo> lights = info.getLights();
10323 ASSERT_EQ(1U, lights.size());
10324 ASSERT_EQ(InputDeviceLightType::KEYBOARD_MIC_MUTE, lights[0].type);
10325 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10326}
10327
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010328TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10329 RawLightInfo infoMono = {.id = 1,
10330 .name = "mono_keyboard_backlight",
10331 .maxBrightness = 255,
10332 .flags = InputLightClass::BRIGHTNESS |
10333 InputLightClass::KEYBOARD_BACKLIGHT,
10334 .path = ""};
10335 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10336
10337 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10338 InputDeviceInfo info;
10339 controller.populateDeviceInfo(&info);
10340 std::vector<InputDeviceLightInfo> lights = info.getLights();
10341 ASSERT_EQ(1U, lights.size());
10342 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10343 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010344
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010345 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10346 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010347}
10348
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010349TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10350 RawLightInfo infoMono = {.id = 1,
10351 .name = "mono_light",
10352 .maxBrightness = 255,
10353 .flags = InputLightClass::BRIGHTNESS,
10354 .path = ""};
10355 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10356 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10357 "0,100,200");
10358
10359 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10360 std::list<NotifyArgs> unused =
10361 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10362 /*changes=*/{});
10363
10364 InputDeviceInfo info;
10365 controller.populateDeviceInfo(&info);
10366 std::vector<InputDeviceLightInfo> lights = info.getLights();
10367 ASSERT_EQ(1U, lights.size());
10368 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10369}
10370
10371TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10372 RawLightInfo infoMono = {.id = 1,
10373 .name = "mono_keyboard_backlight",
10374 .maxBrightness = 255,
10375 .flags = InputLightClass::BRIGHTNESS |
10376 InputLightClass::KEYBOARD_BACKLIGHT,
10377 .path = ""};
10378 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10379
10380 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10381 std::list<NotifyArgs> unused =
10382 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10383 /*changes=*/{});
10384
10385 InputDeviceInfo info;
10386 controller.populateDeviceInfo(&info);
10387 std::vector<InputDeviceLightInfo> lights = info.getLights();
10388 ASSERT_EQ(1U, lights.size());
10389 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10390}
10391
10392TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10393 RawLightInfo infoMono = {.id = 1,
10394 .name = "mono_keyboard_backlight",
10395 .maxBrightness = 255,
10396 .flags = InputLightClass::BRIGHTNESS |
10397 InputLightClass::KEYBOARD_BACKLIGHT,
10398 .path = ""};
10399 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10400 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10401 "0,100,200");
10402
10403 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10404 std::list<NotifyArgs> unused =
10405 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10406 /*changes=*/{});
10407
10408 InputDeviceInfo info;
10409 controller.populateDeviceInfo(&info);
10410 std::vector<InputDeviceLightInfo> lights = info.getLights();
10411 ASSERT_EQ(1U, lights.size());
10412 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10413 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10414 ASSERT_EQ(BrightnessLevel(0), *it);
10415 std::advance(it, 1);
10416 ASSERT_EQ(BrightnessLevel(100), *it);
10417 std::advance(it, 1);
10418 ASSERT_EQ(BrightnessLevel(200), *it);
10419}
10420
10421TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10422 RawLightInfo infoMono = {.id = 1,
10423 .name = "mono_keyboard_backlight",
10424 .maxBrightness = 255,
10425 .flags = InputLightClass::BRIGHTNESS |
10426 InputLightClass::KEYBOARD_BACKLIGHT,
10427 .path = ""};
10428 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10429 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10430 "0,100,200,300,400,500");
10431
10432 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10433 std::list<NotifyArgs> unused =
10434 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10435 /*changes=*/{});
10436
10437 InputDeviceInfo info;
10438 controller.populateDeviceInfo(&info);
10439 std::vector<InputDeviceLightInfo> lights = info.getLights();
10440 ASSERT_EQ(1U, lights.size());
10441 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10442}
10443
Chris Yee2b1e5c2021-03-10 22:45:12 -080010444TEST_F(LightControllerTest, RGBLight) {
10445 RawLightInfo infoRed = {.id = 1,
10446 .name = "red",
10447 .maxBrightness = 255,
10448 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10449 .path = ""};
10450 RawLightInfo infoGreen = {.id = 2,
10451 .name = "green",
10452 .maxBrightness = 255,
10453 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10454 .path = ""};
10455 RawLightInfo infoBlue = {.id = 3,
10456 .name = "blue",
10457 .maxBrightness = 255,
10458 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10459 .path = ""};
10460 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10461 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10462 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10463
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010464 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010465 InputDeviceInfo info;
10466 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010467 std::vector<InputDeviceLightInfo> lights = info.getLights();
10468 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010469 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10470 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10471 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10472
10473 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10474 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10475}
10476
10477TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10478 RawLightInfo infoRed = {.id = 1,
10479 .name = "red_keyboard_backlight",
10480 .maxBrightness = 255,
10481 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10482 InputLightClass::KEYBOARD_BACKLIGHT,
10483 .path = ""};
10484 RawLightInfo infoGreen = {.id = 2,
10485 .name = "green_keyboard_backlight",
10486 .maxBrightness = 255,
10487 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10488 InputLightClass::KEYBOARD_BACKLIGHT,
10489 .path = ""};
10490 RawLightInfo infoBlue = {.id = 3,
10491 .name = "blue_keyboard_backlight",
10492 .maxBrightness = 255,
10493 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10494 InputLightClass::KEYBOARD_BACKLIGHT,
10495 .path = ""};
10496 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10497 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10498 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10499
10500 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10501 InputDeviceInfo info;
10502 controller.populateDeviceInfo(&info);
10503 std::vector<InputDeviceLightInfo> lights = info.getLights();
10504 ASSERT_EQ(1U, lights.size());
10505 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10506 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10507 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10508
10509 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10510 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10511}
10512
10513TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10514 RawLightInfo infoRed = {.id = 1,
10515 .name = "red",
10516 .maxBrightness = 255,
10517 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10518 .path = ""};
10519 RawLightInfo infoGreen = {.id = 2,
10520 .name = "green",
10521 .maxBrightness = 255,
10522 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10523 .path = ""};
10524 RawLightInfo infoBlue = {.id = 3,
10525 .name = "blue",
10526 .maxBrightness = 255,
10527 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10528 .path = ""};
10529 RawLightInfo infoGlobal = {.id = 3,
10530 .name = "global_keyboard_backlight",
10531 .maxBrightness = 255,
10532 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10533 InputLightClass::KEYBOARD_BACKLIGHT,
10534 .path = ""};
10535 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10536 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10537 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10538 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10539
10540 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10541 InputDeviceInfo info;
10542 controller.populateDeviceInfo(&info);
10543 std::vector<InputDeviceLightInfo> lights = info.getLights();
10544 ASSERT_EQ(1U, lights.size());
10545 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10546 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10547 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010548
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010549 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10550 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010551}
10552
10553TEST_F(LightControllerTest, MultiColorRGBLight) {
10554 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010555 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010556 .maxBrightness = 255,
10557 .flags = InputLightClass::BRIGHTNESS |
10558 InputLightClass::MULTI_INTENSITY |
10559 InputLightClass::MULTI_INDEX,
10560 .path = ""};
10561
10562 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10563
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010564 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010565 InputDeviceInfo info;
10566 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010567 std::vector<InputDeviceLightInfo> lights = info.getLights();
10568 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010569 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10570 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10571 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10572
10573 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10574 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10575}
10576
10577TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10578 RawLightInfo infoColor = {.id = 1,
10579 .name = "multi_color_keyboard_backlight",
10580 .maxBrightness = 255,
10581 .flags = InputLightClass::BRIGHTNESS |
10582 InputLightClass::MULTI_INTENSITY |
10583 InputLightClass::MULTI_INDEX |
10584 InputLightClass::KEYBOARD_BACKLIGHT,
10585 .path = ""};
10586
10587 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10588
10589 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10590 InputDeviceInfo info;
10591 controller.populateDeviceInfo(&info);
10592 std::vector<InputDeviceLightInfo> lights = info.getLights();
10593 ASSERT_EQ(1U, lights.size());
10594 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10595 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10596 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010597
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010598 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10599 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010600}
10601
Josep del Rioa1046a82023-08-24 19:57:27 +000010602TEST_F(LightControllerTest, SonyPlayerIdLight) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010603 RawLightInfo info1 = {.id = 1,
Josep del Rioa1046a82023-08-24 19:57:27 +000010604 .name = "sony1",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010605 .maxBrightness = 255,
10606 .flags = InputLightClass::BRIGHTNESS,
10607 .path = ""};
10608 RawLightInfo info2 = {.id = 2,
Josep del Rioa1046a82023-08-24 19:57:27 +000010609 .name = "sony2",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010610 .maxBrightness = 255,
10611 .flags = InputLightClass::BRIGHTNESS,
10612 .path = ""};
10613 RawLightInfo info3 = {.id = 3,
Josep del Rioa1046a82023-08-24 19:57:27 +000010614 .name = "sony3",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010615 .maxBrightness = 255,
10616 .flags = InputLightClass::BRIGHTNESS,
10617 .path = ""};
10618 RawLightInfo info4 = {.id = 4,
Josep del Rioa1046a82023-08-24 19:57:27 +000010619 .name = "sony4",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010620 .maxBrightness = 255,
10621 .flags = InputLightClass::BRIGHTNESS,
10622 .path = ""};
10623 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10624 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10625 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10626 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10627
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010628 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010629 InputDeviceInfo info;
10630 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010631 std::vector<InputDeviceLightInfo> lights = info.getLights();
10632 ASSERT_EQ(1U, lights.size());
Josep del Rioa1046a82023-08-24 19:57:27 +000010633 ASSERT_STREQ("sony", lights[0].name.c_str());
10634 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
10635 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10636 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10637
10638 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10639 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10640 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
10641 ASSERT_STREQ("sony", lights[0].name.c_str());
10642}
10643
10644TEST_F(LightControllerTest, PlayerIdLight) {
10645 RawLightInfo info1 = {.id = 1,
10646 .name = "player-1",
10647 .maxBrightness = 255,
10648 .flags = InputLightClass::BRIGHTNESS,
10649 .path = ""};
10650 RawLightInfo info2 = {.id = 2,
10651 .name = "player-2",
10652 .maxBrightness = 255,
10653 .flags = InputLightClass::BRIGHTNESS,
10654 .path = ""};
10655 RawLightInfo info3 = {.id = 3,
10656 .name = "player-3",
10657 .maxBrightness = 255,
10658 .flags = InputLightClass::BRIGHTNESS,
10659 .path = ""};
10660 RawLightInfo info4 = {.id = 4,
10661 .name = "player-4",
10662 .maxBrightness = 255,
10663 .flags = InputLightClass::BRIGHTNESS,
10664 .path = ""};
10665 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10666 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10667 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10668 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10669
10670 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10671 InputDeviceInfo info;
10672 controller.populateDeviceInfo(&info);
10673 std::vector<InputDeviceLightInfo> lights = info.getLights();
10674 ASSERT_EQ(1U, lights.size());
10675 ASSERT_STREQ("player", lights[0].name.c_str());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010676 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010677 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10678 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010679
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010680 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10681 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10682 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010683}
10684
Michael Wrightd02c5b62014-02-10 15:10:22 -080010685} // namespace android